Version 1
[yaffs-website] / web / modules / contrib / linkit / src / Plugin / Linkit / Attribute / Target.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\linkit\Plugin\Linkit\Attribute\Target.
6  */
7
8 namespace Drupal\linkit\Plugin\Linkit\Attribute;
9
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\linkit\ConfigurableAttributeBase;
12
13 /**
14  * Target attribute.
15  *
16  * @Attribute(
17  *   id = "target",
18  *   label = @Translation("Target"),
19  *   html_name = "target"
20  * )
21  */
22 class Target extends ConfigurableAttributeBase {
23
24   const SELECT_LIST = 'select_list';
25   const SIMPLE_CHECKBOX = 'simple_checkbox';
26
27   /**
28    * {@inheritdoc}
29    */
30   public function buildFormElement($default_value) {
31     switch ($this->configuration['widget_type']) {
32       case self::SELECT_LIST:
33         return [
34           '#type' => 'select',
35           '#title' => t('Target'),
36           '#options' => [
37             '' => '',
38             '_blank' => t('New window (_blank)'),
39             '_top' => t('Top window (_top)'),
40             '_self' => t('Same window (_self)'),
41             '_parent' => t('Parent window (_parent)')
42           ],
43           '#default_value' => $default_value,
44         ];
45       case self::SIMPLE_CHECKBOX:
46         return [
47           '#type' => 'checkbox',
48           '#title' => t('Open in new window'),
49           '#default_value' => $default_value,
50           '#return_value' => '_blank',
51         ];
52     }
53
54     return [];
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public function defaultConfiguration() {
61     return parent::defaultConfiguration() + [
62       'widget_type' => self::SIMPLE_CHECKBOX,
63     ];
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
70     $form['widget_type'] = [
71       '#type' => 'radios',
72       '#title' => $this->t('Widget type'),
73       '#default_value' => $this->configuration['widget_type'],
74       '#options' =>  [
75         self::SELECT_LIST => $this->t('Selectlist with predefined targets.'),
76         self::SIMPLE_CHECKBOX => $this->t('Simple checkbox to allow links to be opened in a new browser window or tab.'),
77       ],
78     ];
79
80     return $form;
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
93     $this->configuration['widget_type'] = $form_state->getValue('widget_type');
94   }
95
96 }