Completed
Push — 8.x-1.x ( b1a253...362da1 )
by Janez
03:13
created

SelectForm   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getRowId() 0 4 1
A render() 0 3 1
A preRender() 0 12 3
A viewsForm() 0 20 3
A query() 0 1 1
A clickSortable() 0 1 1
1
<?php
2
3
/**
4
 * @file
5
 * Contains \Drupal\entity_browser\Plugin\views\field\SelectForm.
6
 */
7
8
namespace Drupal\entity_browser\Plugin\views\field;
9
10
use Drupal\views\Plugin\views\field\FieldPluginBase;
11
use Drupal\views\Plugin\views\style\Table;
12
use Drupal\views\ResultRow;
13
use Drupal\views\Render\ViewsRenderPipelineMarkup;
14
15
/**
16
 * Defines a bulk operation form element that works with entity browser.
17
 *
18
 * @ViewsField("entity_browser_select")
19
 */
20
class SelectForm extends FieldPluginBase {
21
22
  /**
23
   * Returns the ID for a result row.
24
   *
25
   * @param \Drupal\views\ResultRow $row
26
   *   The result row.
27
   *
28
   * @return string
29
   *   The row ID, in the form ENTITY_TYPE:ENTITY_ID.
30
   */
31
  public function getRowId(ResultRow $row) {
32
    $entity = $row->_entity;
33
    return $entity->getEntityTypeId() . ':' . $entity->id();
34
  }
35
36
  /**
37
   * {@inheritdoc}
38
   */
39
  public function render(ResultRow $values) {
40
    return ViewsRenderPipelineMarkup::create('<!--form-item-' . $this->options['id'] . '--' . $this->getRowId($values) . '-->');
41
  }
42
43
  /**
44
   * {@inheritdoc}
45
   */
46
  public function preRender(&$values) {
47
    parent::preRender($values);
48
49
    // If the view is using a table style, provide a placeholder for a
50
    // "select all" checkbox.
51
    if (!empty($this->view->style_plugin) && $this->view->style_plugin instanceof Table) {
0 ignored issues
show
Bug introduced by
The class Drupal\views\Plugin\views\style\Table does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
52
      // Add the tableselect css classes.
53
      $this->options['element_label_class'] .= 'select-all';
54
      // Hide the actual label of the field on the table header.
55
      $this->options['label'] = '';
56
    }
57
  }
58
59
  /**
60
   * Form constructor for the bulk form.
61
   *
62
   * @param array $render
63
   *   An associative array containing the structure of the form.
64
   */
65
  public function viewsForm(&$render) {
66
    // Only add the bulk form options and buttons if there are results.
67
    if (!empty($this->view->result)) {
68
      // Render checkboxes for all rows.
69
      $render[$this->options['id']]['#tree'] = TRUE;
70
      $render[$this->options['id']]['#printed'] = TRUE;
71
      foreach ($this->view->result as $row) {
72
        $value = $this->getRowId($row);
73
74
        $render[$this->options['id']][$value] = [
75
          '#type' => 'checkbox',
76
          '#title' => $this->t('Select this item'),
77
          '#title_display' => 'invisible',
78
          '#return_value' => $value,
79
          '#attributes' => ['name' => "entity_browser_select[$value]"],
80
          '#default_value' => NULL,
81
        ];
82
      }
83
    }
84
  }
85
86
  /**
87
   * {@inheritdoc}
88
   */
89
  public function query() {}
90
91
  /**
92
   * {@inheritdoc}
93
   */
94
  public function clickSortable() { return FALSE; }
95
96
}
97