Completed
Push — 8.x-1.x ( a890cc...7bd151 )
by Janez
02:20
created

RenderedEntity::settingsForm()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 2
dl 16
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\entity_browser\Plugin\EntityBrowser\FieldWidgetDisplay;
4
5
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
6
use Drupal\Core\Entity\EntityInterface;
7
use Drupal\Core\Entity\EntityTypeManagerInterface;
8
use Drupal\Core\Form\FormStateInterface;
9
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
use Drupal\entity_browser\FieldWidgetDisplayBase;
12
13
/**
14
 * Displays the fully rendered entity.
15
 *
16
 * @EntityBrowserFieldWidgetDisplay(
17
 *   id = "rendered_entity",
18
 *   label = @Translation("Rendered entity"),
19
 *   description = @Translation("Displays fully rendered entity.")
20
 * )
21
 */
22
class RenderedEntity extends FieldWidgetDisplayBase implements ContainerFactoryPluginInterface {
23
24
  /**
25
   * Entity manager service.
26
   *
27
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
28
   */
29
  protected $entityTypeManager;
30
31
  /**
32
   * Entity display repository.
33
   *
34
   * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
35
   */
36
  protected $entityDisplayRepository;
37
38
  /**
39
   * Constructs widget plugin.
40
   *
41
   * @param array $configuration
42
   *   A configuration array containing information about the plugin instance.
43
   * @param string $plugin_id
44
   *   The plugin_id for the plugin instance.
45
   * @param mixed $plugin_definition
46
   *   The plugin implementation definition.
47
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
48
   *   Entity type manager service.
49
   * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
50
   *   Entity display repository service.
51
   */
52
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityDisplayRepositoryInterface $entity_display_repository) {
53
    parent::__construct($configuration, $plugin_id, $plugin_definition);
54
    $this->entityTypeManager = $entity_type_manager;
55
    $this->entityDisplayRepository = $entity_display_repository;
56
  }
57
58
  /**
59
   * {@inheritdoc}
60
   */
61
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
62
    return new static(
63
      $configuration,
64
      $plugin_id,
65
      $plugin_definition,
66
      $container->get('entity_type.manager'),
67
      $container->get('entity_display.repository')
68
    );
69
  }
70
71
  /**
72
   * {@inheritdoc}
73
   */
74
  public function view(EntityInterface $entity) {
75
    return $this->entityTypeManager->getViewBuilder($this->configuration['entity_type'])->view($entity, $this->configuration['view_mode']);
76
  }
77
78
  /**
79
   * {@inheritdoc}
80
   */
81 View Code Duplication
  public function settingsForm(array $form, FormStateInterface $form_state) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
82
    $options = [];
83
    foreach ($this->entityDisplayRepository->getViewModeOptions($this->configuration['entity_type']) as $id => $view_mode_label) {
84
      $options[$id] = $view_mode_label;
85
    }
86
87
    return [
88
      'view_mode' => [
89
        '#type' => 'select',
90
        '#title' => $this->t('View mode'),
91
        '#description' => $this->t('Select view mode to be used when rendering entities.'),
92
        '#default_value' => $this->configuration['view_mode'],
93
        '#options' => $options,
94
      ],
95
    ];
96
  }
97
98
  /**
99
   * {@inheritdoc}
100
   */
101
  public function defaultConfiguration() {
102
    return [
103
      'view_mode' => 'default',
104
    ] + parent::defaultConfiguration();
105
  }
106
107
  /**
108
   * {@inheritdoc}
109
   */
110
  public function calculateDependencies() {
111
    $dependencies = parent::calculateDependencies();
112
    if ($view_mode = $this->entityTypeManager->getStorage('entity_view_mode')->load($this->configuration['entity_type'] . '.' . $this->configuration['view_mode'])) {
113
      $dependencies[$view_mode->getConfigDependencyKey()][] = $view_mode->getConfigDependencyName();
114
    }
115
    return $dependencies;
116
  }
117
118
}
119