Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
* Administrative page callbacks for the shortcut module.
*/
use Drupal\Component\Utility\String;
use Drupal\shortcut\Entity\ShortcutSet;
Dries Buytaert
committed
/**
Dries Buytaert
committed
* Form callback: builds the form for switching shortcut sets.
Dries Buytaert
committed
*
* @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.
Dries Buytaert
committed
*
Dries Buytaert
committed
* @return
* An array representing the form definition.
*
Dries Buytaert
committed
* @see shortcut_set_switch_validate()
Dries Buytaert
committed
* @see shortcut_set_switch_submit()
Alex Pott
committed
*
* @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
* Use \Drupal\shortcut\Form\ShortcutForm::overview().
Dries Buytaert
committed
*/
function shortcut_set_switch($form, &$form_state, $account = NULL) {
$user = \Drupal::currentUser();
Dries Buytaert
committed
if (!isset($account)) {
$account = $user;
}
// Prepare the list of shortcut sets.
Dries Buytaert
committed
$sets = entity_load_multiple('shortcut_set');
Dries Buytaert
committed
$current_set = shortcut_current_displayed_set($account);
Dries Buytaert
committed
Dries Buytaert
committed
$options = array();
foreach ($sets as $name => $set) {
$options[$name] = String::checkPlain($set->label());
Dries Buytaert
committed
}
// Only administrators can add shortcut sets.
$add_access = $user->hasPermission('administer shortcuts');
Dries Buytaert
committed
if ($add_access) {
$options['new'] = t('New set');
}
Dries Buytaert
committed
if (count($options) > 1) {
$form['account'] = array(
'#type' => 'value',
'#value' => $account,
);
Dries Buytaert
committed
Dries Buytaert
committed
$form['set'] = array(
'#type' => 'radios',
Dries Buytaert
committed
'#title' => $user->id() == $account->id() ? t('Choose a set of shortcuts to use') : t('Choose a set of shortcuts for this user'),
Dries Buytaert
committed
'#options' => $options,
Angie Byron
committed
'#default_value' => $current_set->id(),
Dries Buytaert
committed
);
Dries Buytaert
committed
Angie Byron
committed
$form['label'] = array(
Dries Buytaert
committed
'#type' => 'textfield',
Angie Byron
committed
'#title' => t('Label'),
Dries Buytaert
committed
'#title_display' => 'invisible',
Dries Buytaert
committed
'#description' => t('The new set is created by copying items from your default shortcut set.'),
'#access' => $add_access,
);
Angie Byron
committed
$form['id'] = array(
'#type' => 'machine_name',
'#machine_name' => array(
'exists' => '\Drupal\shortcut\Entity\ShortcutSet::load',
Angie Byron
committed
'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,
);
Dries Buytaert
committed
Dries Buytaert
committed
if ($user->id() != $account->id()) {
Dries Buytaert
committed
$default_set = shortcut_default_set($account);
Angie Byron
committed
$form['new']['#description'] = t('The new set is created by copying items from the %default set.', array('%default' => $default_set->label()));
Dries Buytaert
committed
}
Dries Buytaert
committed
Dries Buytaert
committed
$form['#attached'] = array(
'library' => array('shortcut/drupal.shortcut.admin'),
Dries Buytaert
committed
);
Dries Buytaert
committed
$form['actions'] = array('#type' => 'actions');
Dries Buytaert
committed
$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(
Angie Byron
committed
'#markup' => '<p>' . t('You are currently using the %set-name shortcut set.', array('%set-name' => $current_set->label())) . '</p>',
Dries Buytaert
committed
);
}
Dries Buytaert
committed
return $form;
}
Dries Buytaert
committed
/**
* 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.
Angie Byron
committed
if (trim($form_state['values']['label']) == '') {
form_set_error('label', $form_state, t('The new set label is required.'));
Dries Buytaert
committed
}
// Check to prevent a duplicate title.
Angie Byron
committed
if (shortcut_set_title_exists($form_state['values']['label'])) {
Angie Byron
committed
form_set_error('label', $form_state, t('The shortcut set %name already exists. Choose another name.', array('%name' => $form_state['values']['label'])));
Dries Buytaert
committed
}
}
}
Dries Buytaert
committed
/**
Dries Buytaert
committed
* Submit handler for shortcut_set_switch().
Dries Buytaert
committed
*/
function shortcut_set_switch_submit($form, &$form_state) {
Alex Pott
committed
$user = \Drupal::currentUser();
Dries Buytaert
committed
$account = $form_state['values']['account'];
if ($form_state['values']['set'] == 'new') {
Dries Buytaert
committed
// Save a new shortcut set with links copied from the user's default set.
Angie Byron
committed
$set = ShortcutSet::create(array(
Angie Byron
committed
'id' => $form_state['values']['id'],
'label' => $form_state['values']['label'],
));
$set->save();
Dries Buytaert
committed
$replacements = array(
'%user' => $account->getUsername(),
Angie Byron
committed
'%set_name' => $set->label(),
Dries Buytaert
committed
'@switch-url' => url(current_path()),
Dries Buytaert
committed
);
Dries Buytaert
committed
if ($account->id() == $user->id()) {
Dries Buytaert
committed
// Only administrators can create new shortcut sets, so we know they have
// access to switch back.
Angie Byron
committed
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));
Dries Buytaert
committed
}
else {
Angie Byron
committed
drupal_set_message(t('%user is now using a new shortcut set called %set_name. You can edit it from this page.', $replacements));
Dries Buytaert
committed
}
Angie Byron
committed
$form_state['redirect_route'] = array(
'route_name' => 'shortcut.set_customize',
'route_parameters' => array(
'shortcut_set' => $set->id(),
),
);
Dries Buytaert
committed
}
else {
// Switch to a different shortcut set.
$set = ShortcutSet::load($form_state['values']['set']);
Dries Buytaert
committed
$replacements = array(
'%user' => $account->getUsername(),
Angie Byron
committed
'%set_name' => $set->label(),
Dries Buytaert
committed
);
Dries Buytaert
committed
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));
Dries Buytaert
committed
}
// Assign the shortcut set to the provided user account.
shortcut_set_assign_user($set, $account);
}