EntityReferenceRenderedDisabled::formElement()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 3
b 0
f 0
nc 3
nop 5
dl 0
loc 18
rs 9.8666
1
<?php
2
3
namespace Drupal\entityreference_rendered_widget\Plugin\Field\FieldWidget;
4
5
use Drupal\Core\Field\FieldItemListInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Field\FieldItemListInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Drupal\Core\Form\FormStateInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Form\FormStateInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
/**
9
 * Plugin implementation of the 'entity_reference_rendered_disabled' widget.
10
 *
11
 * @FieldWidget(
12
 *   id = "entity_reference_rendered_disabled",
13
 *   label = @Translation("Entity reference rendered disabled"),
14
 *   field_types = {
15
 *     "entity_reference"
16
 *   }
17
 * )
18
 */
19
class EntityReferenceRenderedDisabled extends EntityReferenceRenderedBase {
20
21
  /**
22
   * {@inheritdoc}
23
   */
24
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
25
    if (empty($items->get($delta)->getValue()['target_id'])) {
26
      $element['#markup'] = '<div class="label">' . $element['#title'] . '</div>';
27
      $element['#markup'] .= '<div>' . $this->t('Value creation for this field is disabled.') . '</div>';
28
      return $element;
29
    }
30
31
    $target = $this->entityTypeManager->getStorage($this->targetEntityType)->load($items->get($delta)->getValue()['target_id']);
32
    if (!$target) {
33
      $element['#markup'] = '<div class="label">' . $element['#title'] . '</div>';
34
      $element['#markup'] .= '<div>' . $this->t('Removed.') . '</div>';
35
      return $element;
36
    }
37
38
    $view_builder = $this->entityTypeManager->getViewBuilder($target->getEntityTypeId());
39
    $element['entity'] = $view_builder->view($target, $this->getSetting('display_mode'), $target->language()->getId());
40
41
    return $element;
42
  }
43
44
}
45