Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. <?php
  2.  
  3. namespace DrupalmymodulePluginCondition;
  4.  
  5. use DrupalCoreConditionConditionPluginBase;
  6. use DrupalCoreFormFormStateInterface;
  7. use DrupalCoreEntityEntityTypeManagerInterface;
  8. use DrupalCorePluginContainerFactoryPluginInterface;
  9. use SymfonyComponentDependencyInjectionContainerInterface;
  10.  
  11. /**
  12. * Provides an 'Entity type' condition.
  13. *
  14. * Note that this condition does not work in node_preview and possibly in
  15. * node_revision contexts without the patch on core issue:
  16. * https://www.drupal.org/i/2890758
  17. *
  18. * @Condition(
  19. * id = "entity_type",
  20. * label = @Translation("Entity type"),
  21. * )
  22. */
  23. class EntityType extends ConditionPluginBase implements ContainerFactoryPluginInterface {
  24.  
  25. /**
  26. * An array of plugin definitions (empty array if no definitions were found). Keys are plugin IDs.
  27. *
  28. * @var mixed[]
  29. */
  30. protected $entityTypeDefinitions;
  31.  
  32. /**
  33. * Creates a new EntityType instance.
  34. *
  35. * @param DrupalCoreEntityEntityTypeManagerInterface $entity_type_manager
  36. * The entity type manager.
  37. * @param array $configuration
  38. * The plugin configuration, i.e. an array with configuration values keyed
  39. * by configuration option name. The special key 'context' may be used to
  40. * initialize the defined contexts by setting it to an array of context
  41. * values keyed by context names.
  42. * @param string $plugin_id
  43. * The plugin_id for the plugin instance.
  44. * @param mixed $plugin_definition
  45. * The plugin implementation definition.
  46. */
  47. public function __construct(EntityTypeManagerInterface $entity_type_manager, array $configuration, $plugin_id, $plugin_definition) {
  48. parent::__construct($configuration, $plugin_id, $plugin_definition);
  49. $this->entityTypeDefinitions = $entity_type_manager->getDefinitions();
  50. }
  51.  
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  56. return new static(
  57. $container->get('entity_type.manager'),
  58. $configuration,
  59. $plugin_id,
  60. $plugin_definition
  61. );
  62. }
  63.  
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  68. foreach ($this->entityTypeDefinitions as $type) {
  69. $options[$type->id()] = $type->getLabel();
  70. }
  71.  
  72. $form['types'] = [
  73. '#title' => $this->pluginDefinition['label'],
  74. '#type' => 'checkboxes',
  75. '#options' => $options,
  76. '#default_value' => $this->configuration['types'],
  77. ];
  78. return parent::buildConfigurationForm($form, $form_state);
  79. }
  80.  
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
  85. $this->configuration['types'] = array_filter($form_state->getValue('types'));
  86. parent::submitConfigurationForm($form, $form_state);
  87. }
  88.  
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function defaultConfiguration() {
  93. return ['types' => []] + parent::defaultConfiguration();
  94. }
  95.  
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function evaluate() {
  100. if (empty($this->configuration['types']) && !$this->isNegated()) {
  101. return TRUE;
  102. }
  103.  
  104. foreach ($this->configuration['types'] as $type) {
  105. if ($entity = $this->getContextValue($type)) {
  106. var_dump("found $type entity in context");
  107. return TRUE;
  108. }
  109. elseif($entity_id = Drupal::routeMatch()->getParameter($type)) {
  110. var_dump("found $type entity in route parameter");
  111. return TRUE;
  112. }
  113. else {
  114. var_dump("couldn't find $type entity in context or route parameter");
  115. }
  116. }
  117. return FALSE;
  118. }
  119.  
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function summary() {
  124. $types = $this->configuration['types'];
  125. if (empty($types)) {
  126. return;
  127. }
  128.  
  129. foreach ($this->entityTypeDefinitions as $type) {
  130. if (in_array($type->id(), $types)) {
  131. $labels[] = $type->getLabel();
  132. }
  133. }
  134.  
  135. if (count($labels) > 1) {
  136. $last = array_pop($labels);
  137. $labels = implode(', ', $labels);
  138.  
  139. if (!empty($this->configuration['negate'])) {
  140. return $this->t('Entity type is not @types or @last', array('@types' => $labels, '@last' => $last));
  141. }
  142. return $this->t('Entity type is @types or @last', array('@types' => $labels, '@last' => $last));
  143. }
  144. $label = reset($labels);
  145.  
  146. if (!empty($this->configuration['negate'])) {
  147. return $this->t('Entity type is not @type', array('@type' => $label));
  148. }
  149. return $this->t('Entity type is @type', array('@type' => $label));
  150. }
  151.  
  152. /**
  153. * {@inheritdoc}
  154. */
  155. public function getCacheContexts() {
  156. $contexts = parent::getCacheContexts();
  157. $contexts[] = 'url';
  158. return $contexts;
  159. }
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement