d5d32d5185cebd1deb5440c969f68a6d0f2872d8
[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     $element['widget'] = [
28       '#type' => 'select',
29       '#options' => $this->widget_ids,
30       '#default_value' => $this->getDefaultWidget(),
31       '#executes_submit_callback' => TRUE,
32       '#limit_validation_errors' => [['widget']],
33       // #limit_validation_errors only takes effect if #submit is present.
34       '#submit' => [],
35       '#ajax' => [
36         'callback' => [$this, 'changeWidgetCallback'],
37         'wrapper' => 'entity-browser-form',
38       ],
39     ];
40
41     $element['change'] = [
42       '#type' => 'submit',
43       '#name' => 'change',
44       '#value' => $this->t('Change'),
45       '#attributes' => ['class' => ['js-hide']],
46     ];
47
48     return $element;
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public function submit(array &$form, FormStateInterface $form_state) {
55     return $form_state->getValue('widget');
56   }
57
58   /**
59    * AJAX callback to refresh form.
60    *
61    * @param array $form
62    *   Form.
63    * @param FormStateInterface $form_state
64    *   Form state object.
65    *
66    * @return array
67    *   Form element to replace.
68    */
69   public function changeWidgetCallback(array &$form, FormStateInterface $form_state) {
70     return $form;
71   }
72
73 }