Completed
Pull Request — 8.x-1.x (#138)
by
unknown
21:45 queued 23s
created

NewDisplay::getForm()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 40
rs 8.8571
cc 1
eloc 29
nc 1
nop 2
1
<?php
2
3
/**
4
 * Contains \Drupal\entity_browser\Plugin\EntityBrowser\SelectionDisplay\NewDisplay.
5
 */
6
7
namespace Drupal\entity_browser\Plugin\EntityBrowser\SelectionDisplay;
8
9
use Drupal\Component\Utility\NestedArray;
10
use Drupal\Core\Form\FormStateInterface;
11
use Drupal\entity_browser\SelectionDisplayBase;
12
13
/**
14
 * Show current selection and delivers selected entities.
15
 *
16
 * @EntityBrowserSelectionDisplay(
17
 *   id = "new_display",
18
 *   label = @Translation("New selection display"),
19
 *   description = @Translation("Show current selection display and delivers selected entities.")
20
 * )
21
 */
22
class NewDisplay extends SelectionDisplayBase {
23
24
  /**
25
   * {@inheritdoc}
26
   */
27
  public function defaultConfiguration() {
28
    return array(
29
      'view' => NULL,
30
      'view_display' => NULL,
31
    ) + parent::defaultConfiguration();
32
  }
33
34
  /**
35
   * {@inheritdoc}
36
   */
37
  public function getForm(array &$original_form, FormStateInterface $form_state) {
38
    $form = [];
39
    $form['#attached']['library'][] = 'entity_browser/entity_reference';
40
41
    $selected_entities = &$form_state->getStorage()['entity_browser']['selected_entities'];
42
    $ids = array_keys($selected_entities);
43
44
    $form['current'] = [
45
      '#theme_wrappers' => ['container'],
46
      '#attributes' => ['class' => ['entities-list']],
47
      'items' => array_map(
48
        function($id) use ($selected_entities) {
49
          $entity = $selected_entities[$id];
50
          return [
51
            '#theme_wrappers' => ['container'],
52
            '#attributes' => [
53
              'class' => ['item-container'],
54
              'data-entity-id' => $entity->id()
55
            ],
56
            'display' => ['#markup' => $entity->label()],
57
            'remove_button' => [
58
              '#type' => 'submit',
59
              '#value' => $this->t('Remove'),
60
              '#submit' => [[get_class($this), 'removeItemSubmit']],
61
              '#name' => 'remove_' . $id,
62
              '#attributes' => ['data-entity-id' => $id]
63
            ],
64
          ];
65
        },
66
        $ids
67
      ),
68
    ];
69
    $form['use_selected'] = array(
70
      '#type' => 'submit',
71
      '#value' => t('Use selected'),
72
      '#name' => 'use_selected',
73
    );
74
75
    return $form;
76
  }
77
78
  /**
79
   * Submit callback for remove buttons.
80
   */
81
  public static function removeItemSubmit(&$form, FormStateInterface $form_state) {
82
    $triggering_element = $form_state->getTriggeringElement();
83
    if (!empty($triggering_element['#attributes']['data-entity-id'])) {
84
      $id = $triggering_element['#attributes']['data-entity-id'];
85
      $parents = array_slice($triggering_element['#parents'], 0, -4);
86
      $array_parents = array_slice($triggering_element['#array_parents'], 0, -4);
87
88
      // Find and remove correct entity.
89
      $values = explode(' ', $form_state->getValue(array_merge($parents, ['target_id'])));
90
      $values = array_filter(
91
        $values,
92
        function($item) use ($id) { return $item != $id; }
93
      );
94
      $values = implode(' ', $values);
95
96
      // Set new value for this widget.
97
      $target_id_element = &NestedArray::getValue($form, array_merge($array_parents, ['target_id']));
98
      $form_state->setValueForElement($target_id_element, $values);
99
      NestedArray::setValue($form_state->getUserInput(), $target_id_element['#parents'], $values);
100
101
      // Rebuild form.
102
      $form_state->setRebuild();
103
    }
104
  }
105
106
  /**
107
   * {@inheritdoc}
108
   */
109
  public function submit(array &$form, FormStateInterface $form_state) {
110
    if ($form_state->getTriggeringElement()['#name'] == 'use_selected') {
111
      $this->selectionDone($form_state);
112
    }
113
  }
114
115
}
116