Completed
Push — 8.x-1.x ( 610588...77e186 )
by Janez
03:43
created

View   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 95
Duplicated Lines 5.26 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 5
loc 95
rs 10
wmc 12
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A defaultConfiguration() 0 6 1
B getForm() 0 31 4
A submit() 0 5 2
A buildConfigurationForm() 0 22 3
A submitConfigurationForm() 5 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * Contains \Drupal\entity_browser\Plugin\EntityBrowser\SelectionDisplay\View.
5
 */
6
7
namespace Drupal\entity_browser\Plugin\EntityBrowser\SelectionDisplay;
8
9
use Drupal\Core\Entity\EntityInterface;
10
use Drupal\Core\Form\FormStateInterface;
11
use Drupal\entity_browser\SelectionDisplayBase;
12
use Drupal\views\Views;
13
14
/**
15
 * Displays current selection in a View.
16
 *
17
 * @EntityBrowserSelectionDisplay(
18
 *   id = "view",
19
 *   label = @Translation("View selection display"),
20
 *   description = @Translation("Displays current selection in a View.")
21
 * )
22
 */
23
class View extends SelectionDisplayBase {
24
25
  /**
26
   * {@inheritdoc}
27
   */
28
  public function defaultConfiguration() {
29
    return array(
30
      'view' => NULL,
31
      'view_display' => NULL,
32
    ) + parent::defaultConfiguration();
33
  }
34
35
  /**
36
   * {@inheritdoc}
37
   */
38
  public function getForm(array &$original_form, FormStateInterface $form_state) {
39
    $form = [];
40
41
    // TODO - do we need better error handling for view and view_display (in case
42
    // either of those is nonexistent or display not of correct type)?
43
    $storage = &$form_state->getStorage();
44
    if (empty($storage['selection_display_view']) || $form_state->isRebuilding()) {
45
      $storage['selection_display_view'] = $this->entityManager
46
        ->getStorage('view')
47
        ->load($this->configuration['view'])
48
        ->getExecutable();
49
    }
50
51
    // TODO - if there are entities that are selected multiple times this displays
52
    // them only once. Reason for that is how SQL and Views work and we probably
53
    // can't do much about it.
54
    if (!empty($this->selectedEntities)) {
55
      $ids = array_map(function(EntityInterface $item) {return $item->id();}, $this->selectedEntities);
56
      $storage['selection_display_view']->setArguments([implode(',', $ids)]);
57
    }
58
59
    $form['view'] = $storage['selection_display_view']->executeDisplay($this->configuration['view_display']);
60
61
    $form['use_selected'] = array(
62
      '#type' => 'submit',
63
      '#value' => t('Use selection'),
64
      '#name' => 'use_selected',
65
    );
66
67
    return $form;
68
  }
69
70
  /**
71
   * {@inheritdoc}
72
   */
73
  public function submit(array &$form, FormStateInterface $form_state) {
74
    if ($form_state->getTriggeringElement()['#name'] == 'use_selected') {
75
      $this->selectionDone($form_state);
76
    }
77
  }
78
79
  /**
80
   * {@inheritdoc}
81
   */
82
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
83
    $options = [];
84
85
    // Get all views displays.
86
    $views = Views::getAllViews();
87
    foreach ($views as $view_id => $view) {
88
      foreach ($view->get('display') as $display_id => $display)  {
89
        $options[$view_id . '.' . $display_id] = $this->t('@view : @display', array('@view' => $view->label(), '@display' => $display['display_title']));
90
      }
91
    }
92
93
    $form['view'] = [
94
      '#type' => 'select',
95
      '#title' => $this->t('View : View display'),
96
      '#default_value' => $this->configuration['view'] . '.' . $this->configuration['view_display'],
97
      '#options' => $options,
98
      '#required' => TRUE,
99
      '#description' => 'View display to use for displaying currently selected items. Do note that to get something usefull out of this display, its first contextual filter should be a filter on the primary identifier field of your entity type (e.g., Node ID, Media ID).',
100
    ];
101
102
    return $form;
103
  }
104
105
  /**
106
   * {@inheritdoc}
107
   */
108
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
109
    $values = $form_state->getValues();
110
111 View Code Duplication
    if (!empty($values['view'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
      list($view_id, $display_id) = explode('.', $values['view']);
113
      $this->configuration['view'] = $view_id;
114
      $this->configuration['view_display'] = $display_id;
115
    }
116
  }
117
}
118