4e0f081fb5c840a14560cbcac263131b79f2e19d
[yaffs-website] / web / core / modules / outside_in / src / Block / BlockEntityOffCanvasForm.php
1 <?php
2
3 namespace Drupal\outside_in\Block;
4
5 use Drupal\block\BlockForm;
6 use Drupal\block\BlockInterface;
7 use Drupal\Core\Block\BlockPluginInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Plugin\PluginWithFormsInterface;
10
11 /**
12  * Provides form for block instance forms when used in the off-canvas dialog.
13  *
14  * This form removes advanced sections of regular block form such as the
15  * visibility settings, machine ID and region.
16  */
17 class BlockEntityOffCanvasForm extends BlockForm {
18
19   /**
20    * Provides a title callback to get the block's admin label.
21    *
22    * @param \Drupal\block\BlockInterface $block
23    *   The block entity.
24    *
25    * @return \Drupal\Core\StringTranslation\TranslatableMarkup
26    *   The title.
27    */
28   public function title(BlockInterface $block) {
29     // @todo Wrap "Configure " in <span class="visually-hidden"></span> once
30     //   https://www.drupal.org/node/2359901 is fixed.
31     return $this->t('Configure @block', ['@block' => $block->getPlugin()->getPluginDefinition()['admin_label']]);
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public function form(array $form, FormStateInterface $form_state) {
38     $form = parent::form($form, $form_state);
39
40     // Create link to full block form.
41     $query = [];
42     if ($destination = $this->getRequest()->query->get('destination')) {
43       $query['destination'] = $destination;
44     }
45     $form['advanced_link'] = [
46       '#type' => 'link',
47       '#title' => $this->t('Advanced options'),
48       '#url' => $this->entity->toUrl('edit-form', ['query' => $query]),
49       '#weight' => 1000,
50     ];
51
52     // Remove the ID and region elements.
53     unset($form['id'], $form['region'], $form['settings']['admin_label']);
54
55     return $form;
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   protected function actions(array $form, FormStateInterface $form_state) {
62     $actions = parent::actions($form, $form_state);
63     $actions['submit']['#value'] = $this->t('Save @block', ['@block' => $this->entity->getPlugin()->getPluginDefinition()['admin_label']]);
64     $actions['delete']['#access'] = FALSE;
65     return $actions;
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   protected function buildVisibilityInterface(array $form, FormStateInterface $form_state) {
72     // Do not display the visibility.
73     return [];
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   protected function validateVisibility(array $form, FormStateInterface $form_state) {
80     // Intentionally empty.
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   protected function submitVisibility(array $form, FormStateInterface $form_state) {
87     // Intentionally empty.
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   protected function getPluginForm(BlockPluginInterface $block) {
94     if ($block instanceof PluginWithFormsInterface) {
95       return $this->pluginFormFactory->createInstance($block, 'off_canvas', 'configure');
96     }
97     return $block;
98   }
99
100 }