Completed
Push — 8.x-1.x ( 0b3da9...370b51 )
by Janez
01:48
created

EntityBrowserListBuilder::buildHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\entity_browser\Controllers;
4
5
use Drupal\Core\Entity\EntityInterface;
6
use Drupal\Core\Entity\EntityListBuilder;
7
8
/**
9
 * Provides a list controller for entity browser.
10
 *
11
 * @ingroup entity_browser
12
 */
13
class EntityBrowserListBuilder extends EntityListBuilder {
14
15
  /**
16
   * {@inheritdoc}
17
   *
18
   * Building the header and content lines for the entity browser list.
19
   *
20
   * Calling the parent::buildHeader() adds a column for the possible actions
21
   * and inserts the 'edit' and 'delete' links as defined for the entity type.
22
   */
23
  public function buildHeader() {
24
    $header['id'] = $this->t('ID');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$header was never initialized. Although not strictly required by PHP, it is generally a good practice to add $header = 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...
25
    $header['name'] = $this->t('Name');
26
    return $header + parent::buildHeader();
27
  }
28
29
  /**
30
   * {@inheritdoc}
31
   */
32
  public function buildRow(EntityInterface $entity) {
33
    /* @var $entity \Drupal\entity_browser\Entity\EntityBrowser */
34
    $row['id'] = $entity->id();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$row was never initialized. Although not strictly required by PHP, it is generally a good practice to add $row = 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...
35
    $row['name'] = $entity->label();
36
    return $row + parent::buildRow($entity);
37
  }
38
39
  /**
40
   * {@inheritdoc}
41
   */
42
  protected function getDefaultOperations(EntityInterface $entity) {
43
    $operations = parent::getDefaultOperations($entity);
44
45
    // Destination parameter messes up with the entity form wizard redirects.
46
    $options = $operations['edit']['url']->getOptions();
47
    if (!empty($options['query']['destination'])) {
48
      unset($options['query']['destination']);
49
    }
50
    $operations['edit']['url']->setOptions($options);
51
52
    return $operations;
53
  }
54
55
}
56