$set) { $options[$name] = check_plain($set->label()); } // Only administrators can add shortcut sets. $add_access = user_access('administer shortcuts'); if ($add_access) { $options['new'] = t('New set'); } if (count($options) > 1) { $form['account'] = array( '#type' => 'value', '#value' => $account, ); $form['set'] = array( '#type' => 'radios', '#title' => $user->uid == $account->uid ? t('Choose a set of shortcuts to use') : t('Choose a set of shortcuts for this user'), '#options' => $options, '#default_value' => $current_set->id(), ); $form['label'] = array( '#type' => 'textfield', '#title' => t('Label'), '#title_display' => 'invisible', '#description' => t('The new set is created by copying items from your default shortcut set.'), '#access' => $add_access, ); $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, ); if ($user->uid != $account->uid) { $default_set = shortcut_default_set($account); $form['new']['#description'] = t('The new set is created by copying items from the %default set.', array('%default' => $default_set->label())); } $form['#attached'] = array( 'library' => array(array('shortcut', 'drupal.shortcut.admin')), ); $form['actions'] = array('#type' => 'actions'); $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( '#markup' => '
' . t('You are currently using the %set-name shortcut set.', array('%set-name' => $current_set->label())) . '
', ); } return $form; } /** * 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. if (trim($form_state['values']['label']) == '') { form_set_error('new', t('The new set label is required.')); } // Check to prevent a duplicate title. 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']))); } } } /** * Submit handler for shortcut_set_switch(). */ function shortcut_set_switch_submit($form, &$form_state) { global $user; $account = $form_state['values']['account']; if ($form_state['values']['set'] == 'new') { // Save a new shortcut set with links copied from the user's default set. $default_set = shortcut_default_set($account); $set = entity_create('shortcut', array( 'id' => $form_state['values']['id'], 'label' => $form_state['values']['label'], 'links' => $default_set->links, )); $set->save(); $replacements = array( '%user' => $account->name, '%set_name' => $set->label(), '@switch-url' => url(current_path()), ); if ($account->uid == $user->uid) { // Only administrators can create new shortcut sets, so we know they have // access to switch back. drupal_set_message(t('You are now using the new %set_name shortcut set. You can edit it from this page or switch back to a different one.', $replacements)); } else { drupal_set_message(t('%user is now using a new shortcut set called %set_name. You can edit it from this page.', $replacements)); } $form_state['redirect'] = 'admin/config/user-interface/shortcut/manage/' . $set->id(); } else { // Switch to a different shortcut set. $set = shortcut_set_load($form_state['values']['set']); $replacements = array( '%user' => $account->name, '%set_name' => $set->label(), ); drupal_set_message($account->uid == $user->uid ? t('You are now using the %set_name shortcut set.', $replacements) : t('%user is now using the %set_name shortcut set.', $replacements)); } // Assign the shortcut set to the provided user account. shortcut_set_assign_user($set, $account); } /** * Menu page callback: builds the page for administering shortcut sets. */ function shortcut_set_admin() { return entity_list_controller('shortcut')->render(); } /** * Page callback: provides the shortcut set creation form. */ function shortcut_set_add() { $entity = entity_create('shortcut', array()); return entity_get_form($entity); } /** * Form callback: builds the form for customizing shortcut sets. * * @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_set Drupal\shortcut\Plugin\Core\Entity\Shortcut * An object representing the shortcut set which is being edited. * * @return * An array representing the form definition. * * @ingroup forms * @see shortcut_set_customize_submit() */ function shortcut_set_customize($form, &$form_state, $shortcut_set) { $form['#shortcut_set_name'] = $shortcut_set->id(); $form['shortcuts'] = array( '#tree' => TRUE, '#weight' => -20, 'links' => array(), ); foreach ($shortcut_set->links as $uuid => $link) { $mlid = $link['mlid']; $form['shortcuts']['links'][$mlid]['name']['#markup'] = l($link['link_title'], $link['link_path']); $form['shortcuts']['links'][$mlid]['weight'] = array( '#type' => 'weight', '#title' => t('Weight for @title', array('@title' => $link['link_title'])), '#title_display' => 'invisible', '#delta' => 50, '#default_value' => $link['weight'], '#attributes' => array('class' => array('shortcut-weight')), ); $links['edit'] = array( 'title' => t('Edit'), 'href' => "admin/config/user-interface/shortcut/link/$mlid", ); $links['delete'] = array( 'title' => t('Delete'), 'href' => "admin/config/user-interface/shortcut/link/$mlid/delete", ); $form['shortcuts']['links'][$mlid]['operations'] = array( '#type' => 'operations', '#links' => $links, ); } $form['actions'] = array( '#type' => 'actions', '#access' => !empty($shortcut_set->links), ); $form['actions']['submit'] = array( '#type' => 'submit', '#value' => t('Save changes'), ); return $form; } /** * Submit handler for shortcut_set_customize(). */ function shortcut_set_customize_submit($form, &$form_state) { foreach ($form_state['values']['shortcuts']['links'] as $mlid => $data) { $link = menu_link_load($mlid); $link['weight'] = $data['weight']; menu_link_save($link); } drupal_set_message(t('The shortcut set has been updated.')); } /** * Returns HTML for a shortcut set customization form. * * @param $variables * An associative array containing: * - form: A render element representing the form. * * @see shortcut_set_customize() * @ingroup themeable */ function theme_shortcut_set_customize($variables) { $form = $variables['form']; // Do not add any rows to the table if there are no shortcuts to display. $statuses = empty($shortcuts_by_status['enabled']) && empty($shortcuts_by_status['disabled']) ? array() : array_keys($shortcuts_by_status); $rows = array(); drupal_add_tabledrag('shortcuts', 'order', 'sibling', 'shortcut-weight'); foreach (element_children($form['shortcuts']['links']) as $key) { $shortcut = &$form['shortcuts']['links'][$key]; $row = array(); $row[] = drupal_render($shortcut['name']); $row[] = drupal_render($shortcut['weight']); $row[] = drupal_render($shortcut['operations']); $rows[] = array( 'data' => $row, 'class' => array('draggable'), ); } $header = array(t('Name'), t('Weight'), t('Operations')); $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'shortcuts'), 'empty' => t('No shortcuts available. Add a shortcut.', array('@link' => url('admin/config/user-interface/shortcut/' . $form['#shortcut_set_name'] . '/add-link'))))); $output .= drupal_render($form['actions']); $output = drupal_render_children($form) . $output; return $output; } /** * Form callback: builds the form for adding a new shortcut link. * * @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_set Drupal\shortcut\Plugin\Core\Entity\Shortcut * An object representing the shortcut set to which the link will be added. * * @return * An array representing the form definition. * * @ingroup forms * @see shortcut_link_edit_validate() * @see shortcut_link_add_submit() */ 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; } /** * Form callback: builds the form for editing a shortcut link. * * @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. * * @return * An array representing the form definition. * * @ingroup forms * @see shortcut_link_edit_validate() * @see shortcut_link_edit_submit() */ 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. * * @return * An array of form elements. */ function _shortcut_link_form_elements($shortcut_link = NULL) { if (!isset($shortcut_link)) { $shortcut_link = entity_create('menu_link', array( 'link_title' => '', 'link_path' => '' )); } else { $shortcut_link['link_path'] = ($shortcut_link['link_path'] == '' . format_plural($number, '1 user has chosen or been assigned to this shortcut set.', '@count users have chosen or been assigned to this shortcut set.') . '
'; } // Also, if a module implements hook_shortcut_default_set(), it's possible // that this set is being used as a default set. Add a message about that too. if (count(module_implements('shortcut_default_set')) > 0) { $info .= '' . t('If you have chosen this shortcut set as the default for some or all users, they may also be affected by deleting it.') . '
'; } $form['info'] = array( '#markup' => $info, ); return confirm_form( $form, t('Are you sure you want to delete the shortcut set %title?', array('%title' => $shortcut_set->label())), 'admin/config/user-interface/shortcut/manage/' . $shortcut_set->id(), t('This action cannot be undone.'), t('Delete'), t('Cancel') ); } /** * Submit handler for shortcut_set_delete_form(). */ function shortcut_set_delete_form_submit($form, &$form_state) { $shortcut_set = shortcut_set_load($form_state['values']['shortcut_set']); $label = $shortcut_set->label(); $shortcut_set->delete(); $form_state['redirect'] = 'admin/config/user-interface/shortcut'; drupal_set_message(t('The shortcut set %title has been deleted.', array('%title' => $label))); } /** * Form callback: builds the confirmation form for deleting a shortcut link. * * @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 will be deleted. * * @return * An array representing the form definition. * * @ingroup forms * @see shortcut_link_delete_submit() */ function shortcut_link_delete($form, &$form_state, $shortcut_link) { $form['shortcut_link'] = array( '#type' => 'value', '#value' => $shortcut_link, ); return confirm_form( $form, t('Are you sure you want to delete the shortcut %title?', array('%title' => $shortcut_link['link_title'])), 'admin/config/user-interface/shortcut/manage/' . $shortcut_link['menu_name'], t('This action cannot be undone.'), t('Delete'), t('Cancel') ); } /** * Submit handler for shortcut_link_delete_submit(). */ function shortcut_link_delete_submit($form, &$form_state) { $shortcut_link = $form_state['values']['shortcut_link']; menu_link_delete($shortcut_link['mlid']); $set_name = str_replace('shortcut-', '' , $shortcut_link['menu_name']); $form_state['redirect'] = 'admin/config/user-interface/shortcut/manage/' . $set_name; drupal_set_message(t('The shortcut %title has been deleted.', array('%title' => $shortcut_link['link_title']))); } /** * Menu page callback: creates a new link in the provided shortcut set. * * After completion, redirects the user back to where they came from. * * @param $shortcut_set Drupal\shortcut\Plugin\Core\Entity\Shortcut * Returned from shortcut_set_load(). */ function shortcut_link_add_inline($shortcut_set) { if (isset($_REQUEST['token']) && drupal_valid_token($_REQUEST['token'], 'shortcut-add-link') && shortcut_valid_link($_GET['link'])) { $item = menu_get_item($_GET['link']); $title = ($item && $item['title']) ? $item['title'] : $_GET['name']; $link = array( 'link_title' => $title, 'link_path' => $_GET['link'], ); shortcut_admin_add_link($link, $shortcut_set); if ($shortcut_set->save() == SAVED_UPDATED) { drupal_set_message(t('Added a shortcut for %title.', array('%title' => $link['link_title']))); } else { drupal_set_message(t('Unable to add a shortcut for %title.', array('%title' => $link['link_title']))); } drupal_goto(); } throw new AccessDeniedHttpException(); }