Completed
Push — 8.x-1.x ( 386052...3342d2 )
by Janez
02:50
created

Tabs::getForm()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 1
Metric Value
cc 2
eloc 16
c 7
b 0
f 1
nc 2
nop 2
dl 0
loc 22
rs 9.2
1
<?php
2
3
/**
4
 * @file
5
 * Contains \Drupal\entity_browser\Plugin\EntityBrowser\WidgetSelector\Tabs.
6
 */
7
8
namespace Drupal\entity_browser\Plugin\EntityBrowser\WidgetSelector;
9
10
use Drupal\entity_browser\WidgetSelectorBase;
11
use Drupal\Core\Form\FormStateInterface;
12
13
/**
14
 * Displays entity browser widgets as tabs.
15
 *
16
 * @EntityBrowserWidgetSelector(
17
 *   id = "tabs",
18
 *   label = @Translation("Tabs"),
19
 *   description = @Translation("Displays entity browser widgets as tabs.")
20
 * )
21
 */
22
class Tabs extends WidgetSelectorBase {
23
24
  /**
25
   * {@inheritdoc}
26
   */
27
  public function getForm(array &$form = array(), FormStateInterface &$form_state = NULL) {
28
    $element = [];
29
    foreach ($this->widget_ids as $id => $label) {
30
      $name = 'tab_selector_' . $id;
31
      $element[$name] = array(
32
        '#type' => 'button',
33
        '#attributes' => ['class' => ['tab']],
34
        '#value' => $label,
35
        '#disabled' => $id == $this->getDefaultWidget(),
36
        '#executes_submit_callback' => TRUE,
37
        '#limit_validation_errors' => array(array($id)),
38
        // #limit_validation_errors only takes effect if #submit is present.
39
        '#submit' => array(),
40
        '#name' => $name,
41
        '#widget_id' => $id,
42
      );
43
    }
44
45
    $element['#attached']['library'][] = 'entity_browser/tabs';
46
47
    return $element;
48
  }
49
50
  /**
51
   * {@inheritdoc}
52
   */
53
  public function submit(array &$form, FormStateInterface $form_state) {
54
    if (($trigger = $form_state->getTriggeringElement()) && strpos($trigger['#name'], 'tab_selector_') === 0) {
55
      if (!empty($this->widget_ids[$trigger['#widget_id']])) {
56
        return $trigger['#widget_id'];
57
      }
58
    }
59
  }
60
61
}
62