PageRenderTime 46ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/geeta7/drupal
PHP | 236 lines | 128 code | 31 blank | 77 comment | 12 complexity | 639c37843c9ab3161b5dd6b3df00578f MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\views\Plugin\views\argument_validator\Entity.
  5. */
  6. namespace Drupal\views\Plugin\views\argument_validator;
  7. use Drupal\Core\Entity\EntityInterface;
  8. use Drupal\Core\Entity\EntityManagerInterface;
  9. use Drupal\Core\Form\FormStateInterface;
  10. use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. /**
  13. * Defines a argument validator plugin for each entity type.
  14. *
  15. * @ViewsArgumentValidator(
  16. * id = "entity",
  17. * deriver = "Drupal\views\Plugin\Derivative\ViewsEntityArgumentValidator"
  18. * )
  19. *
  20. * @see \Drupal\views\Plugin\Derivative\ViewsEntityArgumentValidator
  21. */
  22. class Entity extends ArgumentValidatorPluginBase {
  23. /**
  24. * The entity manager.
  25. *
  26. * @var \Drupal\Core\Entity\EntityManagerInterface
  27. */
  28. protected $entityManager;
  29. /**
  30. * If this validator can handle multiple arguments.
  31. *
  32. * @var bool
  33. */
  34. protected $multipleCapable = TRUE;
  35. /**
  36. * Constructs an \Drupal\views\Plugin\views\argument_validator\Entity object.
  37. *
  38. * @param array $configuration
  39. * A configuration array containing information about the plugin instance.
  40. * @param string $plugin_id
  41. * The plugin_id for the plugin instance.
  42. * @param mixed $plugin_definition
  43. * The plugin implementation definition.
  44. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
  45. * The entity manager.
  46. */
  47. public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager) {
  48. parent::__construct($configuration, $plugin_id, $plugin_definition);
  49. $this->entityManager = $entity_manager;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  55. return new static(
  56. $configuration,
  57. $plugin_id,
  58. $plugin_definition,
  59. $container->get('entity.manager')
  60. );
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. protected function defineOptions() {
  66. $options = parent::defineOptions();
  67. $options['bundles'] = array('default' => array());
  68. $options['access'] = array('default' => FALSE);
  69. $options['operation'] = array('default' => 'view');
  70. $options['multiple'] = array('default' => FALSE);
  71. return $options;
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  77. parent::buildOptionsForm($form, $form_state);
  78. $entity_type_id = $this->definition['entity_type'];
  79. // Derivative IDs are all entity:entity_type. Sanitized for js.
  80. // The ID is converted back on submission.
  81. $sanitized_id = ArgumentPluginBase::encodeValidatorId($this->definition['id']);
  82. $entity_type = $this->entityManager->getDefinition($entity_type_id);
  83. // If the entity has bundles, allow option to restrict to bundle(s).
  84. if ($entity_type->hasKey('bundle')) {
  85. $bundle_options = array();
  86. foreach ($this->entityManager->getBundleInfo($entity_type_id) as $bundle_id => $bundle_info) {
  87. $bundle_options[$bundle_id] = $bundle_info['label'];
  88. }
  89. $form['bundles'] = array(
  90. '#title' => $entity_type->getBundleLabel() ?: $this->t('Bundles'),
  91. '#default_value' => $this->options['bundles'],
  92. '#type' => 'checkboxes',
  93. '#options' => $bundle_options,
  94. '#description' => $this->t('If none are selected, all are allowed.'),
  95. );
  96. }
  97. // Offer the option to filter by access to the entity in the argument.
  98. $form['access'] = array(
  99. '#type' => 'checkbox',
  100. '#title' => $this->t('Validate user has access to the %name', array('%name' => $entity_type->getLabel())),
  101. '#default_value' => $this->options['access'],
  102. );
  103. $form['operation'] = array(
  104. '#type' => 'radios',
  105. '#title' => $this->t('Access operation to check'),
  106. '#options' => array(
  107. 'view' => $this->t('View'),
  108. 'update' => $this->t('Edit'),
  109. 'delete' => $this->t('Delete'),
  110. ),
  111. '#default_value' => $this->options['operation'],
  112. '#states' => array(
  113. 'visible' => array(
  114. ':input[name="options[validate][options][' . $sanitized_id . '][access]"]' => array('checked' => TRUE),
  115. ),
  116. ),
  117. );
  118. // If class is multiple capable give the option to validate single/multiple.
  119. if ($this->multipleCapable) {
  120. $form['multiple'] = array(
  121. '#type' => 'radios',
  122. '#title' => $this->t('Multiple arguments'),
  123. '#options' => array(
  124. 0 => $this->t('Single ID', array('%type' => $entity_type->getLabel())),
  125. 1 => $this->t('One or more IDs separated by , or +', array('%type' => $entity_type->getLabel())),
  126. ),
  127. '#default_value' => (string) $this->options['multiple'],
  128. );
  129. }
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. public function submitOptionsForm(&$form, FormStateInterface $form_state, &$options = array()) {
  135. // Filter out unused options so we don't store giant unnecessary arrays.
  136. $options['bundles'] = array_filter($options['bundles']);
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function validateArgument($argument) {
  142. $entity_type = $this->definition['entity_type'];
  143. if ($this->multipleCapable && $this->options['multiple']) {
  144. // At this point only interested in individual IDs no matter what type,
  145. // just splitting by the allowed delimiters.
  146. $ids = array_filter(preg_split('/[,+ ]/', $argument));
  147. }
  148. elseif ($argument) {
  149. $ids = array($argument);
  150. }
  151. // No specified argument should be invalid.
  152. else {
  153. return FALSE;
  154. }
  155. $entities = $this->entityManager->getStorage($entity_type)->loadMultiple($ids);
  156. // Validate each id => entity. If any fails break out and return false.
  157. foreach ($ids as $id) {
  158. // There is no entity for this ID.
  159. if (!isset($entities[$id])) {
  160. return FALSE;
  161. }
  162. if (!$this->validateEntity($entities[$id])) {
  163. return FALSE;
  164. }
  165. }
  166. return TRUE;
  167. }
  168. /**
  169. * Validates an individual entity against class access settings.
  170. *
  171. * @param \Drupal\Core\Entity\EntityInterface $entity
  172. *
  173. * @return bool
  174. * True if validated.
  175. */
  176. protected function validateEntity(EntityInterface $entity) {
  177. // If access restricted by entity operation.
  178. if ($this->options['access'] && !$entity->access($this->options['operation'])) {
  179. return FALSE;
  180. }
  181. // If restricted by bundle.
  182. $bundles = $this->options['bundles'];
  183. if (count($bundles) && empty($bundles[$entity->bundle()])) {
  184. return FALSE;
  185. }
  186. return TRUE;
  187. }
  188. /**
  189. * {@inheritdoc}
  190. */
  191. public function calculateDependencies() {
  192. $dependencies = parent::calculateDependencies();
  193. $entity_type_id = $this->definition['entity_type'];
  194. $bundle_entity_type = $this->entityManager->getDefinition($entity_type_id)->getBundleEntityType();
  195. // The bundle entity type might not exist. For example, users do not have
  196. // bundles.
  197. if ($this->entityManager->hasHandler($bundle_entity_type, 'storage')) {
  198. $bundle_entity_storage = $this->entityManager->getStorage($bundle_entity_type);
  199. foreach ($bundle_entity_storage->loadMultiple(array_keys($this->options['bundles'])) as $bundle_entity) {
  200. $dependencies[$bundle_entity->getConfigDependencyKey()][] = $bundle_entity->getConfigDependencyName();
  201. }
  202. }
  203. return $dependencies;
  204. }
  205. }