Completed
Push — 8.x-1.x ( 591469...d3c594 )
by Janez
02:00
created

Tabs   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getForm() 0 25 2
A submit() 0 7 4
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
}
60