Completed
Pull Request — 8.x-1.x (#138)
by
unknown
03:41
created

NewDisplay   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 7
Bugs 2 Features 0
Metric Value
wmc 6
c 7
b 2
f 0
lcom 0
cbo 1
dl 0
loc 83
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A defaultConfiguration() 0 6 1
B getForm() 0 40 2
A removeItemSubmit() 0 7 1
A submit() 0 8 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\SortArray;
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
    $selected_entities = $form_state->get(['entity_browser', 'selected_entities']);
39
40
    $form = [];
41
    $form['#attached']['library'][] = 'entity_browser/new_display';
42
    $form['selected'] = [
43
      '#theme_wrappers' => ['container'],
44
      '#attributes' => ['class' => ['selected-entities-list']],
45
      '#tree' => TRUE
46
    ];
47
    foreach ($selected_entities as $id => $entity) {
48
      $form['selected']['items_'. $entity->id()] = [
49
        '#theme_wrappers' => ['container'],
50
        '#attributes' => [
51
          'class' => ['selected-item-container'],
52
          'data-entity-id' => $entity->id()
53
        ],
54
        'display' => ['#markup' => $entity->label()],
55
        'remove_button' => [
56
          '#type' => 'submit',
57
          '#value' => $this->t('Remove'),
58
          '#submit' => [[get_class($this), 'removeItemSubmit']],
59
          '#name' => 'remove_' . $entity->id(),
60
          '#attributes' => ['data-row-id' => $id]
61
        ],
62
        'weight' => [
63
          '#type' => 'hidden',
64
          '#default_value' => $id,
65
          '#attributes' => ['class' => ['weight']]
66
        ]
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
   * @param array $form
82
   * @param \Drupal\Core\Form\FormStateInterface $form_state
83
   */
84
  public static function removeItemSubmit(array &$form, FormStateInterface $form_state) {
85
    $id = $form_state->getTriggeringElement()['#attributes']['data-row-id'];
86
    $selected_entities = $form_state->get(['entity_browser', 'selected_entities']);
87
    unset($selected_entities[$id]);
88
    $form_state->set(['entity_browser', 'selected_entities'], $selected_entities);
89
    $form_state->setRebuild();
90
  }
91
92
  /**
93
   * {@inheritdoc}
94
   */
95
  public function submit(array &$form, FormStateInterface $form_state) {
96
    $selected = $form_state->getValue('selected');
97
    uasort($selected, array(new SortArray(), "sortByWeightElement"));
98
    $form_state->setValue('selected', $selected);
99
    if ($form_state->getTriggeringElement()['#name'] == 'use_selected') {
100
      $this->selectionDone($form_state);
101
    }
102
  }
103
104
}
105