EntityReferenceRenderedBase::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 7
dl 0
loc 22
rs 9.8666
1
<?php
2
3
namespace Drupal\entityreference_rendered_widget\Plugin\Field\FieldWidget;
4
5
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Entity\Entit...playRepositoryInterface 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\Entity\EntityTypeManagerInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Entity\EntityTypeManagerInterface 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
use Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsButtonsWidget;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Field\Plugin...et\OptionsButtonsWidget 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...
8
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...
9
use Drupal\Core\Field\FieldDefinitionInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Field\FieldDefinitionInterface 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...
10
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Plugin\ContainerFactoryPluginInterface 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...
11
use Symfony\Component\DependencyInjection\ContainerInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Depend...tion\ContainerInterface 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...
12
13
/**
14
 * Base class for widgets provided by this module.
15
 */
16
abstract class EntityReferenceRenderedBase extends OptionsButtonsWidget implements ContainerFactoryPluginInterface {
17
18
  /**
19
   * Display modes available for target entity type.
20
   *
21
   * @var array
22
   */
23
  protected $displayModes;
24
25
  /**
26
   * Label display options.
27
   *
28
   * @var array
29
   */
30
  protected $labelOptions;
31
32
  /**
33
   * Referenced entity type.
34
   *
35
   * @var string
36
   */
37
  protected $targetEntityType;
38
39
  /**
40
   * Referenced entity type.
41
   *
42
   * @var array
43
   */
44
  protected $fieldSettings;
45
46
  /**
47
   * Entity Display Repository.
48
   *
49
   * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
50
   */
51
  protected $entityDisplayRepository;
52
53
  /**
54
   * Entity Type Manager.
55
   *
56
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
57
   */
58
  protected $entityTypeManager;
59
60
  /**
61
   * {@inheritdoc}
62
   */
63
  public function __construct($plugin_id,
64
    $plugin_definition,
65
    FieldDefinitionInterface $field_definition,
66
    array $settings,
67
    array $third_party_settings,
68
    EntityDisplayRepositoryInterface $entityDisplayRepository,
69
    EntityTypeManagerInterface $entityTypeManager) {
70
71
    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
72
    $this->entityDisplayRepository = $entityDisplayRepository;
73
    $this->entityTypeManager = $entityTypeManager;
74
75
    $this->fieldSettings = $this->getFieldSettings();
76
    $this->targetEntityType = $this->getFieldSetting('target_type');
77
    $this->displayModes = $this->entityDisplayRepository->getViewModes($this->targetEntityType);
78
    $this->displayModes['default'] = [
79
      'label' => 'Default',
80
    ];
81
    $this->labelOptions = [
82
      'before' => $this->t('Before rendered element'),
83
      'after' => $this->t('After rendered element'),
84
      'hidden' => $this->t('Hidden'),
85
    ];
86
87
  }
88
89
  /**
90
   * {@inheritdoc}
91
   */
92
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
93
    return new static(
94
      $plugin_id,
95
      $plugin_definition,
96
      $configuration['field_definition'],
97
      $configuration['settings'],
98
      $configuration['third_party_settings'],
99
      $container->get('entity_display.repository'),
100
      $container->get('entity_type.manager')
101
    );
102
  }
103
104
  /**
105
   * {@inheritdoc}
106
   */
107
  public static function defaultSettings() {
108
    return [
109
      'display_mode' => 'default',
110
      'label_display' => 'before',
111
    ] + parent::defaultSettings();
112
  }
113
114
  /**
115
   * {@inheritdoc}
116
   */
117
  public function settingsForm(array $form, FormStateInterface $form_state) {
118
    $elements = [];
119
    $settings = $this->settings;
120
121
    $modes = [];
122
    foreach ($this->displayModes as $mode_name => $mode) {
123
      $modes[$mode_name] = $mode['label'];
124
    }
125
126
    $elements['display_mode'] = [
127
      '#type' => 'select',
128
      '#title' => $this->t('Display mode used'),
129
      '#options' => $modes,
130
      '#default_value' => isset($settings['display_mode']) ? $settings['display_mode'] : 'default',
131
    ];
132
    $elements['label_display'] = [
133
      '#type' => 'select',
134
      '#title' => $this->t('Label display'),
135
      '#options' => $this->labelOptions,
136
      '#default_value' => isset($settings['label_display']) ? $settings['label_display'] : 'before',
137
    ];
138
139
    return $elements;
140
  }
141
142
  /**
143
   * {@inheritdoc}
144
   */
145
  public function settingsSummary() {
146
    $summary = [];
147
148
    $settings = $this->getSettings();
149
    $display_mode = $settings['display_mode'];
150
    $label_display = $settings['label_display'];
151
152
    $summary[] = $this->t('Display mode: @mode', ['@mode' => $this->displayModes[$display_mode]['label']]);
153
    $summary[] = $this->t('Label display: @label_display', ['@label_display' => $this->labelOptions[$label_display]]);
154
155
    return $summary;
156
  }
157
158
}
159