Completed
Pull Request — 8.x-1.x (#123)
by
unknown
02:27
created

RenderedEntity::view()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * Contains \Drupal\entity_browser\Plugin\EntityBrowser\FieldWidgetDisplay\RenderedEntity.
5
 */
6
7
namespace Drupal\entity_browser\Plugin\EntityBrowser\FieldWidgetDisplay;
8
9
use Drupal\Core\Entity\EntityInterface;
10
use Drupal\Core\Entity\EntityManagerInterface;
11
use Drupal\Core\Entity\EntityTypeInterface;
12
use Drupal\Core\Form\FormStateInterface;
13
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
14
use Drupal\Core\Plugin\PluginBase;
15
use Drupal\entity_browser\FieldWidgetDisplayInterface;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
18
/**
19
 * Displays the fully rendered entity.
20
 *
21
 * @EntityBrowserFieldWidgetDisplay(
22
 *   id = "rendered_entity",
23
 *   label = @Translation("Rendered entity"),
24
 *   description = @Translation("Displays fully rendered entity.")
25
 * )
26
 */
27
class RenderedEntity extends PluginBase implements FieldWidgetDisplayInterface, ContainerFactoryPluginInterface {
28
29
  /**
30
   * Entity manager service.
31
   *
32
   * @var \Drupal\Core\Entity\EntityManagerInterface
33
   */
34
  protected $entityManager;
35
36
  /**
37
   * Constructs widget plugin.
38
   *
39
   * @param array $configuration
40
   *   A configuration array containing information about the plugin instance.
41
   * @param string $plugin_id
42
   *   The plugin_id for the plugin instance.
43
   * @param mixed $plugin_definition
44
   *   The plugin implementation definition.
45
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
46
   *   Entity manager service.
47
   */
48
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager) {
49
    parent::__construct($configuration, $plugin_id, $plugin_definition);
50
    $this->entityManager = $entity_manager;
51
  }
52
53
  /**
54
   * {@inheritdoc}
55
   */
56
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
57
    return new static(
58
      $configuration,
59
      $plugin_id,
60
      $plugin_definition,
61
      $container->get('entity.manager')
62
    );
63
  }
64
65
  /**
66
   * {@inheritdoc}
67
   */
68
  public function view(EntityInterface $entity) {
69
    return $this->entityManager->getViewBuilder($this->configuration['entity_type'])->view($entity, $this->configuration['view_mode']);
70
  }
71
72
  /**
73
   * {@inheritdoc}
74
   */
75
  public function settingsForm(array $form, FormStateInterface $form_state) {
76
    $options = [];
77
    foreach ($this->entityManager->getViewModes($this->configuration['entity_type']) as $id => $view_mode) {
78
      $options[$id] = $view_mode['label'];
79
    }
80
81
    return [
82
      'view_mode' => [
83
        '#type' => 'select',
84
        '#title' => t('View mode'),
85
        '#description' => t('Select view mode to be used when rendering entities.'),
86
        '#default_value' => $this->configuration['view_mode'],
87
        '#options' => $options,
88
      ],
89
    ];
90
  }
91
92
  /**
93
   * {@inheritdoc}
94
   */
95
  public function isApplicable(EntityTypeInterface $entity_type) {
96
    return TRUE;
97
  }
98
99
}
100