3 namespace Drupal\shortcut\Form;
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Render\Element;
10 * Builds the shortcut set customize form.
14 class SetCustomize extends EntityForm {
17 * The entity being used by this form.
19 * @var \Drupal\shortcut\ShortcutSetInterface
26 public function form(array $form, FormStateInterface $form_state) {
27 $form = parent::form($form, $form_state);
28 $form['shortcuts'] = [
33 $form['shortcuts']['links'] = [
35 '#header' => [t('Name'), t('Weight'), t('Operations')],
36 '#empty' => $this->t('No shortcuts available. <a href=":link">Add a shortcut</a>', [':link' => $this->url('shortcut.link_add', ['shortcut_set' => $this->entity->id()])]),
37 '#attributes' => ['id' => 'shortcuts'],
41 'relationship' => 'sibling',
42 'group' => 'shortcut-weight',
47 foreach ($this->entity->getShortcuts() as $shortcut) {
48 $id = $shortcut->id();
49 $url = $shortcut->getUrl();
50 if (!$url->access()) {
53 $form['shortcuts']['links'][$id]['#attributes']['class'][] = 'draggable';
54 $form['shortcuts']['links'][$id]['name'] = [
56 '#title' => $shortcut->getTitle(),
57 ] + $url->toRenderArray();
58 unset($form['shortcuts']['links'][$id]['name']['#access_callback']);
59 $form['shortcuts']['links'][$id]['#weight'] = $shortcut->getWeight();
60 $form['shortcuts']['links'][$id]['weight'] = [
62 '#title' => t('Weight for @title', ['@title' => $shortcut->getTitle()]),
63 '#title_display' => 'invisible',
64 '#default_value' => $shortcut->getWeight(),
65 '#attributes' => ['class' => ['shortcut-weight']],
70 'url' => $shortcut->urlInfo(),
73 'title' => t('Delete'),
74 'url' => $shortcut->urlInfo('delete-form'),
76 $form['shortcuts']['links'][$id]['operations'] = [
77 '#type' => 'operations',
79 '#access' => $url->access(),
88 protected function actions(array $form, FormStateInterface $form_state) {
89 // Only includes a Save action for the entity, no direct Delete button.
93 '#value' => t('Save'),
94 '#access' => (bool) Element::getVisibleChildren($form['shortcuts']['links']),
95 '#submit' => ['::submitForm', '::save'],
103 public function save(array $form, FormStateInterface $form_state) {
104 foreach ($this->entity->getShortcuts() as $shortcut) {
105 $weight = $form_state->getValue(['shortcuts', 'links', $shortcut->id(), 'weight']);
106 $shortcut->setWeight($weight);
109 drupal_set_message(t('The shortcut set has been updated.'));