PageRenderTime 37ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/core/modules/views/src/Plugin/views/area/Entity.php

https://gitlab.com/reasonat/test8
PHP | 198 lines | 101 code | 26 blank | 71 comment | 15 complexity | 431705f7f8b3bd4c999bc4ede4cd8f64 MD5 | raw file
  1. <?php
  2. namespace Drupal\views\Plugin\views\area;
  3. use Drupal\Core\Entity\EntityManagerInterface;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\views\Plugin\views\display\DisplayPluginBase;
  6. use Drupal\views\ViewExecutable;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. /**
  9. * Provides an area handler which renders an entity in a certain view mode.
  10. *
  11. * @ingroup views_area_handlers
  12. *
  13. * @ViewsArea("entity")
  14. */
  15. class Entity extends TokenizeAreaPluginBase {
  16. /**
  17. * Stores the entity type of the result entities.
  18. *
  19. * @var string
  20. */
  21. protected $entityType;
  22. /**
  23. * The entity manager.
  24. *
  25. * @var \Drupal\Core\Entity\EntityManagerInterface
  26. */
  27. protected $entityManager;
  28. /**
  29. * Constructs a new Entity instance.
  30. *
  31. * @param array $configuration
  32. * A configuration array containing information about the plugin instance.
  33. * @param string $plugin_id
  34. * The plugin_id for the plugin instance.
  35. * @param mixed $plugin_definition
  36. * The plugin implementation definition.
  37. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
  38. * The entity manager.
  39. */
  40. public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager) {
  41. parent::__construct($configuration, $plugin_id, $plugin_definition);
  42. $this->entityManager = $entity_manager;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  48. return new static(
  49. $configuration,
  50. $plugin_id,
  51. $plugin_definition,
  52. $container->get('entity.manager')
  53. );
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
  59. parent::init($view, $display, $options);
  60. $this->entityType = $this->definition['entity_type'];
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. protected function defineOptions() {
  66. $options = parent::defineOptions();
  67. // Per default we enable tokenize, as this is the most common use case for
  68. // this handler.
  69. $options['tokenize']['default'] = TRUE;
  70. // Contains the config target identifier for the entity.
  71. $options['target'] = ['default' => ''];
  72. $options['view_mode'] = ['default' => 'default'];
  73. $options['bypass_access'] = ['default' => FALSE];
  74. return $options;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  80. parent::buildOptionsForm($form, $form_state);
  81. $form['view_mode'] = array(
  82. '#type' => 'select',
  83. '#options' => $this->entityManager->getViewModeOptions($this->entityType),
  84. '#title' => $this->t('View mode'),
  85. '#default_value' => $this->options['view_mode'],
  86. );
  87. $label = $this->entityManager->getDefinition($this->entityType)->getLabel();
  88. $target = $this->options['target'];
  89. // If the target does not contain tokens, try to load the entity and
  90. // display the entity ID to the admin form user.
  91. // @todo Use a method to check for tokens in
  92. // https://www.drupal.org/node/2396607.
  93. if (strpos($this->options['target'], '{{') === FALSE) {
  94. // @todo If the entity does not exist, this will will show the config
  95. // target identifier. Decide if this is the correct behavior in
  96. // https://www.drupal.org/node/2415391.
  97. if ($target_entity = $this->entityManager->loadEntityByConfigTarget($this->entityType, $this->options['target'])) {
  98. $target = $target_entity->id();
  99. }
  100. }
  101. $form['target'] = [
  102. '#title' => $this->t('@entity_type_label ID', ['@entity_type_label' => $label]),
  103. '#type' => 'textfield',
  104. '#default_value' => $target,
  105. ];
  106. $form['bypass_access'] = array(
  107. '#type' => 'checkbox',
  108. '#title' => $this->t('Bypass access checks'),
  109. '#description' => $this->t('If enabled, access permissions for rendering the entity are not checked.'),
  110. '#default_value' => !empty($this->options['bypass_access']),
  111. );
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function submitOptionsForm(&$form, FormStateInterface $form_state) {
  117. parent::submitOptionsForm($form, $form_state);
  118. // Load the referenced entity and store its config target identifier if
  119. // the target does not contains tokens.
  120. // @todo Use a method to check for tokens in
  121. // https://www.drupal.org/node/2396607.
  122. $options = $form_state->getValue('options');
  123. if (strpos($options['target'], '{{') === FALSE) {
  124. if ($entity = $this->entityManager->getStorage($this->entityType)->load($options['target'])) {
  125. $options['target'] = $entity->getConfigTarget();
  126. }
  127. $form_state->setValue('options', $options);
  128. }
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function render($empty = FALSE) {
  134. if (!$empty || !empty($this->options['empty'])) {
  135. // @todo Use a method to check for tokens in
  136. // https://www.drupal.org/node/2396607.
  137. if (strpos($this->options['target'], '{{') !== FALSE) {
  138. // We cast as we need the integer/string value provided by the
  139. // ::tokenizeValue() call.
  140. $target_id = (string) $this->tokenizeValue($this->options['target']);
  141. if ($entity = $this->entityManager->getStorage($this->entityType)->load($target_id)) {
  142. $target_entity = $entity;
  143. }
  144. }
  145. else {
  146. if ($entity = $this->entityManager->loadEntityByConfigTarget($this->entityType, $this->options['target'])) {
  147. $target_entity = $entity;
  148. }
  149. }
  150. if (isset($target_entity) && (!empty($this->options['bypass_access']) || $target_entity->access('view'))) {
  151. $view_builder = $this->entityManager->getViewBuilder($this->entityType);
  152. return $view_builder->view($target_entity, $this->options['view_mode']);
  153. }
  154. }
  155. return [];
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. public function calculateDependencies() {
  161. $dependencies = parent::calculateDependencies();
  162. // Ensure that we don't add dependencies for placeholders.
  163. // @todo Use a method to check for tokens in
  164. // https://www.drupal.org/node/2396607.
  165. if (strpos($this->options['target'], '{{') === FALSE) {
  166. if ($entity = $this->entityManager->loadEntityByConfigTarget($this->entityType, $this->options['target'])) {
  167. $dependencies[$this->entityManager->getDefinition($this->entityType)->getConfigDependencyKey()][] = $entity->getConfigDependencyName();
  168. }
  169. }
  170. return $dependencies;
  171. }
  172. }