Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / entity_browser / src / Plugin / EntityBrowser / WidgetSelector / DropDown.php
1 <?php
2
3 namespace Drupal\entity_browser\Plugin\EntityBrowser\WidgetSelector;
4
5 use Drupal\entity_browser\WidgetSelectorBase;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Displays widgets in a select list.
10  *
11  * @EntityBrowserWidgetSelector(
12  *   id = "drop_down",
13  *   label = @Translation("Drop down widget"),
14  *   description = @Translation("Displays the widgets in a drop down.")
15  * )
16  */
17 class DropDown extends WidgetSelectorBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   public function getForm(array &$form = [], FormStateInterface &$form_state = NULL) {
23     // Set a wrapper container for us to replace the form on ajax call.
24     $form['#prefix'] = '<div id="entity-browser-form">';
25     $form['#suffix'] = '</div>';
26
27     /** @var \Drupal\entity_browser\EntityBrowserInterface $browser */
28     $browser = $form_state->getFormObject()->getEntityBrowser();
29
30     $widget_ids = [];
31     foreach ($this->widget_ids as $widget_id => $widget_name) {
32       if ($browser->getWidget($widget_id)->access()->isAllowed()) {
33         $widget_ids[$widget_id] = $widget_name;
34       }
35     }
36
37     $element['widget'] = [
38       '#type' => 'select',
39       '#options' => $widget_ids,
40       '#default_value' => $this->getDefaultWidget(),
41       '#executes_submit_callback' => TRUE,
42       '#limit_validation_errors' => [['widget']],
43       // #limit_validation_errors only takes effect if #submit is present.
44       '#submit' => [],
45       '#ajax' => [
46         'callback' => [$this, 'changeWidgetCallback'],
47         'wrapper' => 'entity-browser-form',
48       ],
49     ];
50
51     $element['change'] = [
52       '#type' => 'submit',
53       '#name' => 'change',
54       '#value' => $this->t('Change'),
55       '#attributes' => ['class' => ['js-hide']],
56     ];
57
58     return $element;
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function submit(array &$form, FormStateInterface $form_state) {
65     return $form_state->getValue('widget');
66   }
67
68   /**
69    * AJAX callback to refresh form.
70    *
71    * @param array $form
72    *   Form.
73    * @param FormStateInterface $form_state
74    *   Form state object.
75    *
76    * @return array
77    *   Form element to replace.
78    */
79   public function changeWidgetCallback(array &$form, FormStateInterface $form_state) {
80     return $form;
81   }
82
83 }