DropDown   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getForm() 0 38 3
A submit() 0 3 1
A changeWidgetCallback() 0 3 1
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
    /** @var \Drupal\entity_browser\EntityBrowserInterface $browser */
28
    $browser = $form_state->getFormObject()->getEntityBrowser();
29
30
    $widget_ids = [];
31
    foreach ($this->widget_ids as $widget_id => $widget_name) {
32
      if ($browser->getWidget($widget_id)->access()->isAllowed()) {
33
        $widget_ids[$widget_id] = $widget_name;
34
      }
35
    }
36
37
    $element['widget'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$element was never initialized. Although not strictly required by PHP, it is generally a good practice to add $element = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
38
      '#type' => 'select',
39
      '#options' => $widget_ids,
40
      '#default_value' => $this->getDefaultWidget(),
41
      '#executes_submit_callback' => TRUE,
42
      '#limit_validation_errors' => [['widget']],
43
      // #limit_validation_errors only takes effect if #submit is present.
44
      '#submit' => [],
45
      '#ajax' => [
46
        'callback' => [$this, 'changeWidgetCallback'],
47
        'wrapper' => 'entity-browser-form',
48
      ],
49
    ];
50
51
    $element['change'] = [
52
      '#type' => 'submit',
53
      '#name' => 'change',
54
      '#value' => $this->t('Change'),
55
      '#attributes' => ['class' => ['js-hide']],
56
    ];
57
58
    return $element;
59
  }
60
61
  /**
62
   * {@inheritdoc}
63
   */
64
  public function submit(array &$form, FormStateInterface $form_state) {
65
    return $form_state->getValue('widget');
66
  }
67
68
  /**
69
   * AJAX callback to refresh form.
70
   *
71
   * @param array $form
72
   *   Form.
73
   * @param FormStateInterface $form_state
74
   *   Form state object.
75
   *
76
   * @return array
77
   *   Form element to replace.
78
   */
79
  public function changeWidgetCallback(array &$form, FormStateInterface $form_state) {
80
    return $form;
81
  }
82
83
}
84