Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / entity_browser / src / Plugin / EntityBrowser / WidgetSelector / Tabs.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 entity browser widgets as tabs.
10  *
11  * @EntityBrowserWidgetSelector(
12  *   id = "tabs",
13  *   label = @Translation("Tabs"),
14  *   description = @Translation("Creates horizontal tabs on the top of the entity browser, each tab representing one available widget.")
15  * )
16  */
17 class Tabs extends WidgetSelectorBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   public function getForm(array &$form = [], FormStateInterface &$form_state = NULL) {
23     $element = [];
24     /** @var \Drupal\entity_browser\EntityBrowserInterface $browser */
25     $browser = $form_state->getFormObject()->getEntityBrowser();
26     foreach ($this->widget_ids as $id => $label) {
27       $name = 'tab_selector_' . $id;
28       $element[$name] = [
29         '#type' => 'button',
30         '#attributes' => ['class' => ['tab']],
31         '#value' => $label,
32         '#disabled' => $id == $this->getDefaultWidget(),
33         '#executes_submit_callback' => TRUE,
34         '#limit_validation_errors' => [[$id]],
35         // #limit_validation_errors only takes effect if #submit is present.
36         '#submit' => [],
37         '#name' => $name,
38         '#widget_id' => $id,
39         '#access' => $browser->getWidget($id)->access(),
40       ];
41     }
42
43     $element['#attached']['library'][] = 'entity_browser/tabs';
44
45     return $element;
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public function submit(array &$form, FormStateInterface $form_state) {
52     if (($trigger = $form_state->getTriggeringElement()) && strpos($trigger['#name'], 'tab_selector_') === 0) {
53       if (!empty($this->widget_ids[$trigger['#widget_id']])) {
54         return $trigger['#widget_id'];
55       }
56     }
57   }
58
59 }