b0c1f606aafa63a3d3ddfa379663b6429fc783ad
[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 = array(), FormStateInterface &$form_state = NULL) {
23     $element = [];
24     foreach ($this->widget_ids as $id => $label) {
25       $name = 'tab_selector_' . $id;
26       $element[$name] = array(
27         '#type' => 'button',
28         '#attributes' => ['class' => ['tab']],
29         '#value' => $label,
30         '#disabled' => $id == $this->getDefaultWidget(),
31         '#executes_submit_callback' => TRUE,
32         '#limit_validation_errors' => array(array($id)),
33         // #limit_validation_errors only takes effect if #submit is present.
34         '#submit' => array(),
35         '#name' => $name,
36         '#widget_id' => $id,
37       );
38     }
39
40     $element['#attached']['library'][] = 'entity_browser/tabs';
41
42     return $element;
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public function submit(array &$form, FormStateInterface $form_state) {
49     if (($trigger = $form_state->getTriggeringElement()) && strpos($trigger['#name'], 'tab_selector_') === 0) {
50       if (!empty($this->widget_ids[$trigger['#widget_id']])) {
51         return $trigger['#widget_id'];
52       }
53     }
54   }
55
56 }