2009-10-17 00:51:53 +00:00
< ? php
/**
* @ file
* Administrative page callbacks for the shortcut module .
*/
Issue #1668866 by ParisLiakos, aspilicious, tim.plunkett, pdrake, g.oechsler, dawehner, Berdir, corvus_ch, damiankloip, disasm, marcingy, neclimdul: Replace drupal_goto() with RedirectResponse.
2013-06-19 16:07:30 +00:00
use Symfony\Component\HttpFoundation\RedirectResponse ;
2012-06-04 12:06:09 +00:00
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException ;
2009-10-17 00:51:53 +00:00
/**
2010-02-25 15:58:54 +00:00
* Form callback : builds the form for switching shortcut sets .
2009-10-17 00:51:53 +00:00
*
* @ param $form
* An associative array containing the structure of the form .
* @ param $form_state
* An associative array containing the current state of the form .
* @ param $account
* ( optional ) The user account whose shortcuts will be switched . Defaults to
* the current logged - in user .
2010-02-25 15:58:54 +00:00
*
2009-10-17 00:51:53 +00:00
* @ return
* An array representing the form definition .
*
* @ ingroup forms
2010-06-30 15:12:59 +00:00
* @ see shortcut_set_switch_validate ()
2009-10-17 00:51:53 +00:00
* @ see shortcut_set_switch_submit ()
2013-09-21 11:42:43 +00:00
*
* @ deprecated Use \Drupal\shortcut\Form\ShortcutForm :: overview ()
2009-10-17 00:51:53 +00:00
*/
function shortcut_set_switch ( $form , & $form_state , $account = NULL ) {
2013-09-16 03:58:06 +00:00
$user = \Drupal :: currentUser ();
2013-09-04 21:18:30 +00:00
2009-10-17 00:51:53 +00:00
if ( ! isset ( $account )) {
$account = $user ;
}
// Prepare the list of shortcut sets.
2013-07-24 20:45:10 +00:00
$sets = entity_load_multiple ( 'shortcut_set' );
2009-10-17 00:51:53 +00:00
$current_set = shortcut_current_displayed_set ( $account );
2010-02-25 15:58:54 +00:00
2009-10-17 00:51:53 +00:00
$options = array ();
foreach ( $sets as $name => $set ) {
2013-01-19 05:26:12 +00:00
$options [ $name ] = check_plain ( $set -> label ());
2009-10-17 00:51:53 +00:00
}
// Only administrators can add shortcut sets.
2013-09-04 21:18:30 +00:00
$add_access = $user -> hasPermission ( 'administer shortcuts' );
2009-10-17 00:51:53 +00:00
if ( $add_access ) {
$options [ 'new' ] = t ( 'New set' );
}
2010-02-25 15:58:54 +00:00
if ( count ( $options ) > 1 ) {
$form [ 'account' ] = array (
'#type' => 'value' ,
'#value' => $account ,
);
2009-10-17 00:51:53 +00:00
2010-02-25 15:58:54 +00:00
$form [ 'set' ] = array (
'#type' => 'radios' ,
2013-07-11 17:29:02 +00:00
'#title' => $user -> id () == $account -> id () ? t ( 'Choose a set of shortcuts to use' ) : t ( 'Choose a set of shortcuts for this user' ),
2010-02-25 15:58:54 +00:00
'#options' => $options ,
2013-01-19 05:26:12 +00:00
'#default_value' => $current_set -> id (),
2010-02-25 15:58:54 +00:00
);
2009-10-17 00:51:53 +00:00
2013-01-19 05:26:12 +00:00
$form [ 'label' ] = array (
2010-02-25 15:58:54 +00:00
'#type' => 'textfield' ,
2013-01-19 05:26:12 +00:00
'#title' => t ( 'Label' ),
2010-10-20 01:31:07 +00:00
'#title_display' => 'invisible' ,
2010-02-25 15:58:54 +00:00
'#description' => t ( 'The new set is created by copying items from your default shortcut set.' ),
'#access' => $add_access ,
);
2013-01-19 05:26:12 +00:00
$form [ 'id' ] = array (
'#type' => 'machine_name' ,
'#machine_name' => array (
'exists' => 'shortcut_set_load' ,
'source' => array ( 'label' ),
'replace_pattern' => '[^a-z0-9-]+' ,
'replace' => '-' ,
),
// This id could be used for menu name.
'#maxlength' => 23 ,
'#states' => array (
'required' => array (
':input[name="set"]' => array ( 'value' => 'new' ),
),
),
'#required' => FALSE ,
);
2009-10-17 00:51:53 +00:00
2013-07-11 17:29:02 +00:00
if ( $user -> id () != $account -> id ()) {
2010-02-25 15:58:54 +00:00
$default_set = shortcut_default_set ( $account );
2013-01-19 05:26:12 +00:00
$form [ 'new' ][ '#description' ] = t ( 'The new set is created by copying items from the %default set.' , array ( '%default' => $default_set -> label ()));
2010-02-25 15:58:54 +00:00
}
2009-10-17 00:51:53 +00:00
2010-02-25 15:58:54 +00:00
$form [ '#attached' ] = array (
2012-08-30 19:24:38 +00:00
'library' => array ( array ( 'shortcut' , 'drupal.shortcut.admin' )),
2010-02-25 15:58:54 +00:00
);
2010-04-24 14:49:14 +00:00
$form [ 'actions' ] = array ( '#type' => 'actions' );
2010-02-25 15:58:54 +00:00
$form [ 'actions' ][ 'submit' ] = array (
'#type' => 'submit' ,
'#value' => t ( 'Change set' ),
);
}
else {
// There is only 1 option, so output a message in the $form array.
$form [ 'info' ] = array (
2013-01-19 05:26:12 +00:00
'#markup' => '<p>' . t ( 'You are currently using the %set-name shortcut set.' , array ( '%set-name' => $current_set -> label ())) . '</p>' ,
2010-02-25 15:58:54 +00:00
);
}
2009-10-17 00:51:53 +00:00
return $form ;
}
2010-06-30 15:12:59 +00:00
/**
* Validation handler for shortcut_set_switch () .
*/
function shortcut_set_switch_validate ( $form , & $form_state ) {
if ( $form_state [ 'values' ][ 'set' ] == 'new' ) {
// Check to prevent creating a shortcut set with an empty title.
2013-01-19 05:26:12 +00:00
if ( trim ( $form_state [ 'values' ][ 'label' ]) == '' ) {
form_set_error ( 'new' , t ( 'The new set label is required.' ));
2010-06-30 15:12:59 +00:00
}
// Check to prevent a duplicate title.
2013-01-19 05:26:12 +00:00
if ( shortcut_set_title_exists ( $form_state [ 'values' ][ 'label' ])) {
form_set_error ( 'label' , t ( 'The shortcut set %name already exists. Choose another name.' , array ( '%name' => $form_state [ 'values' ][ 'label' ])));
2010-06-30 15:12:59 +00:00
}
}
}
2009-10-17 00:51:53 +00:00
/**
2010-02-25 15:58:54 +00:00
* Submit handler for shortcut_set_switch () .
2009-10-17 00:51:53 +00:00
*/
function shortcut_set_switch_submit ( $form , & $form_state ) {
2013-10-02 21:42:16 +00:00
$user = \Drupal :: currentUser ();
2009-10-17 00:51:53 +00:00
$account = $form_state [ 'values' ][ 'account' ];
if ( $form_state [ 'values' ][ 'set' ] == 'new' ) {
2010-02-25 15:58:54 +00:00
// Save a new shortcut set with links copied from the user's default set.
$default_set = shortcut_default_set ( $account );
2013-07-24 20:45:10 +00:00
$set = entity_create ( 'shortcut_set' , array (
2013-01-19 05:26:12 +00:00
'id' => $form_state [ 'values' ][ 'id' ],
'label' => $form_state [ 'values' ][ 'label' ],
2013-02-08 23:55:25 +00:00
'links' => $default_set -> links ,
2013-01-19 05:26:12 +00:00
));
$set -> save ();
2009-10-17 00:51:53 +00:00
$replacements = array (
2013-07-24 19:40:03 +00:00
'%user' => $account -> getUsername (),
2013-01-19 05:26:12 +00:00
'%set_name' => $set -> label (),
2010-02-25 15:58:54 +00:00
'@switch-url' => url ( current_path ()),
2009-10-17 00:51:53 +00:00
);
2013-07-11 17:29:02 +00:00
if ( $account -> id () == $user -> id ()) {
2009-10-17 00:51:53 +00:00
// Only administrators can create new shortcut sets, so we know they have
// access to switch back.
2010-01-14 04:04:30 +00:00
drupal_set_message ( t ( 'You are now using the new %set_name shortcut set. You can edit it from this page or <a href="@switch-url">switch back to a different one.</a>' , $replacements ));
2009-10-17 00:51:53 +00:00
}
else {
2010-01-14 04:04:30 +00:00
drupal_set_message ( t ( '%user is now using a new shortcut set called %set_name. You can edit it from this page.' , $replacements ));
2009-10-17 00:51:53 +00:00
}
2013-01-19 05:26:12 +00:00
$form_state [ 'redirect' ] = 'admin/config/user-interface/shortcut/manage/' . $set -> id ();
2009-10-17 00:51:53 +00:00
}
else {
// Switch to a different shortcut set.
$set = shortcut_set_load ( $form_state [ 'values' ][ 'set' ]);
$replacements = array (
2013-07-24 19:40:03 +00:00
'%user' => $account -> getUsername (),
2013-01-19 05:26:12 +00:00
'%set_name' => $set -> label (),
2009-10-17 00:51:53 +00:00
);
2013-07-11 17:29:02 +00:00
drupal_set_message ( $account -> id () == $user -> id () ? t ( 'You are now using the %set_name shortcut set.' , $replacements ) : t ( '%user is now using the %set_name shortcut set.' , $replacements ));
2009-10-17 00:51:53 +00:00
}
// Assign the shortcut set to the provided user account.
shortcut_set_assign_user ( $set , $account );
}
/**
2010-02-25 15:58:54 +00:00
* Form callback : builds the form for adding a new shortcut link .
2009-10-17 00:51:53 +00:00
*
* @ param $form
* An associative array containing the structure of the form .
* @ param $form_state
* An associative array containing the current state of the form .
2013-08-18 21:16:19 +00:00
* @ param $shortcut_set Drupal\shortcut\Entity\Shortcut
2009-10-17 00:51:53 +00:00
* An object representing the shortcut set to which the link will be added .
2010-02-25 15:58:54 +00:00
*
2009-10-17 00:51:53 +00:00
* @ return
* An array representing the form definition .
*
* @ ingroup forms
* @ see shortcut_link_edit_validate ()
* @ see shortcut_link_add_submit ()
2013-09-21 11:42:43 +00:00
*
* @ deprecated Use \Drupal\shortcut\Form\ShortcutForm :: add ()
2009-10-17 00:51:53 +00:00
*/
function shortcut_link_add ( $form , & $form_state , $shortcut_set ) {
drupal_set_title ( t ( 'Add new shortcut' ));
$form [ 'shortcut_set' ] = array (
'#type' => 'value' ,
'#value' => $shortcut_set ,
);
$form += _shortcut_link_form_elements ();
return $form ;
}
/**
2010-02-25 15:58:54 +00:00
* Form callback : builds the form for editing a shortcut link .
2009-10-17 00:51:53 +00:00
*
* @ param $form
* An associative array containing the structure of the form .
* @ param $form_state
* An associative array containing the current state of the form .
* @ param $shortcut_link
* An array representing the link that is being edited .
2010-02-25 15:58:54 +00:00
*
2009-10-17 00:51:53 +00:00
* @ return
* An array representing the form definition .
*
* @ ingroup forms
* @ see shortcut_link_edit_validate ()
* @ see shortcut_link_edit_submit ()
2013-09-21 11:42:43 +00:00
*
* @ deprecated Use \Drupal\shortcut\Form\ShortcutForm :: edit ()
2009-10-17 00:51:53 +00:00
*/
function shortcut_link_edit ( $form , & $form_state , $shortcut_link ) {
drupal_set_title ( t ( 'Editing @shortcut' , array ( '@shortcut' => $shortcut_link [ 'link_title' ])));
$form [ 'original_shortcut_link' ] = array (
'#type' => 'value' ,
'#value' => $shortcut_link ,
);
$form += _shortcut_link_form_elements ( $shortcut_link );
return $form ;
}
/**
* Helper function for building a form for adding or editing shortcut links .
*
* @ param $shortcut_link
* ( optional ) An array representing the shortcut link that will be edited . If
* not provided , a new link will be created .
2010-02-25 15:58:54 +00:00
*
2009-10-17 00:51:53 +00:00
* @ return
* An array of form elements .
*/
function _shortcut_link_form_elements ( $shortcut_link = NULL ) {
if ( ! isset ( $shortcut_link )) {
2013-02-08 23:55:25 +00:00
$shortcut_link = entity_create ( 'menu_link' , array (
2009-10-17 00:51:53 +00:00
'link_title' => '' ,
'link_path' => ''
2013-02-08 23:55:25 +00:00
));
2009-10-17 00:51:53 +00:00
}
2010-10-15 04:21:02 +00:00
else {
2013-09-16 03:58:06 +00:00
$shortcut_link [ 'link_path' ] = ( $shortcut_link [ 'link_path' ] == '<front>' ) ? '' : \Drupal :: service ( 'path.alias_manager' ) -> getPathAlias ( $shortcut_link [ 'link_path' ]);
2010-10-15 04:21:02 +00:00
}
2009-10-17 00:51:53 +00:00
$form [ 'shortcut_link' ][ '#tree' ] = TRUE ;
$form [ 'shortcut_link' ][ 'link_title' ] = array (
'#type' => 'textfield' ,
'#title' => t ( 'Name' ),
'#size' => 40 ,
'#maxlength' => 255 ,
'#default_value' => $shortcut_link [ 'link_title' ],
2011-09-24 19:58:05 +00:00
'#required' => TRUE ,
2009-10-17 00:51:53 +00:00
);
$form [ 'shortcut_link' ][ 'link_path' ] = array (
'#type' => 'textfield' ,
'#title' => t ( 'Path' ),
'#size' => 40 ,
'#maxlength' => 255 ,
2012-04-29 15:16:27 +00:00
'#field_prefix' => url ( NULL , array ( 'absolute' => TRUE )),
2009-10-17 00:51:53 +00:00
'#default_value' => $shortcut_link [ 'link_path' ],
);
$form [ '#validate' ][] = 'shortcut_link_edit_validate' ;
2010-04-24 14:49:14 +00:00
$form [ 'actions' ] = array ( '#type' => 'actions' );
2010-02-25 15:58:54 +00:00
$form [ 'actions' ][ 'submit' ] = array (
2009-10-17 00:51:53 +00:00
'#type' => 'submit' ,
'#value' => t ( 'Save' ),
);
return $form ;
}
/**
* Validation handler for the shortcut link add and edit forms .
*/
function shortcut_link_edit_validate ( $form , & $form_state ) {
if ( ! shortcut_valid_link ( $form_state [ 'values' ][ 'shortcut_link' ][ 'link_path' ])) {
form_set_error ( 'shortcut_link][link_path' , t ( 'The link must correspond to a valid path on the site.' ));
}
}
/**
2010-02-25 15:58:54 +00:00
* Submit handler for shortcut_link_edit () .
2009-10-17 00:51:53 +00:00
*/
function shortcut_link_edit_submit ( $form , & $form_state ) {
2010-10-15 04:21:02 +00:00
// Normalize the path in case it is an alias.
2013-09-16 03:58:06 +00:00
$shortcut_path = \Drupal :: service ( 'path.alias_manager' ) -> getSystemPath ( $form_state [ 'values' ][ 'shortcut_link' ][ 'link_path' ]);
2012-09-02 05:38:28 +00:00
if ( empty ( $shortcut_path )) {
$shortcut_path = '<front>' ;
}
$form_state [ 'values' ][ 'shortcut_link' ][ 'link_path' ] = $shortcut_path ;
2010-10-15 04:21:02 +00:00
2013-02-08 23:55:25 +00:00
$shortcut_link = $form_state [ 'values' ][ 'original_shortcut_link' ];
foreach ( $form_state [ 'values' ][ 'shortcut_link' ] as $key => $value ) {
$shortcut_link [ $key ] = $value ;
}
2010-10-15 04:21:02 +00:00
2009-10-17 00:51:53 +00:00
menu_link_save ( $shortcut_link );
2013-01-19 05:26:12 +00:00
$set_name = str_replace ( 'shortcut-' , '' , $shortcut_link [ 'menu_name' ]);
$form_state [ 'redirect' ] = 'admin/config/user-interface/shortcut/manage/' . $set_name ;
2009-10-17 00:51:53 +00:00
drupal_set_message ( t ( 'The shortcut %link has been updated.' , array ( '%link' => $shortcut_link [ 'link_title' ])));
}
/**
2010-02-25 15:58:54 +00:00
* Submit handler for shortcut_link_add () .
2009-10-17 00:51:53 +00:00
*/
function shortcut_link_add_submit ( $form , & $form_state ) {
// Add the shortcut link to the set.
$shortcut_set = $form_state [ 'values' ][ 'shortcut_set' ];
$shortcut_link = $form_state [ 'values' ][ 'shortcut_link' ];
2013-01-19 05:26:12 +00:00
$shortcut_link [ 'menu_name' ] = $shortcut_set -> id ();
2012-07-15 12:58:14 +00:00
shortcut_admin_add_link ( $shortcut_link , $shortcut_set );
2013-01-19 05:26:12 +00:00
$shortcut_set -> save ();
$form_state [ 'redirect' ] = 'admin/config/user-interface/shortcut/manage/' . $shortcut_set -> id ();
2009-10-17 00:51:53 +00:00
drupal_set_message ( t ( 'Added a shortcut for %title.' , array ( '%title' => $shortcut_link [ 'link_title' ])));
}
/**
2010-02-25 15:58:54 +00:00
* Adds a link to the end of a shortcut set , keeping within a prescribed limit .
2009-10-17 00:51:53 +00:00
*
* @ param $link
* An array representing a shortcut link .
2013-08-18 21:16:19 +00:00
* @ param $shortcut_set Drupal\shortcut\Entity\Shortcut
2009-10-17 00:51:53 +00:00
* An object representing the shortcut set which the link will be added to .
* The links in the shortcut set will be re - weighted so that the new link is
* at the end , and some existing links may be disabled ( if the $limit
* parameter is provided ) .
*/
2012-07-15 12:58:14 +00:00
function shortcut_admin_add_link ( $shortcut_link , & $shortcut_set ) {
2010-10-15 04:21:02 +00:00
// Normalize the path in case it is an alias.
2013-09-16 03:58:06 +00:00
$shortcut_link [ 'link_path' ] = \Drupal :: service ( 'path.alias_manager' ) -> getSystemPath ( $shortcut_link [ 'link_path' ]);
2012-09-02 05:38:28 +00:00
if ( empty ( $shortcut_link [ 'link_path' ])) {
$shortcut_link [ 'link_path' ] = '<front>' ;
}
2013-02-08 23:55:25 +00:00
$menu_link = entity_create ( 'menu_link' , $shortcut_link );
$menu_link -> save ();
2010-10-15 04:21:02 +00:00
2009-10-17 00:51:53 +00:00
// Add the link to the end of the list.
2013-02-08 23:55:25 +00:00
$shortcut_set -> links [ $menu_link -> uuid ()] = $menu_link ;
2009-10-17 00:51:53 +00:00
shortcut_set_reset_link_weights ( $shortcut_set );
}