PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/node/src/Plugin/Condition/NodeType.php

https://gitlab.com/geeta7/drupal
PHP | 127 lines | 60 code | 13 blank | 54 comment | 3 complexity | d2bad00a6b9525c0d0a05be98b0e220c MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\node\Plugin\Condition\NodeType.
  5. */
  6. namespace Drupal\node\Plugin\Condition;
  7. use Drupal\Core\Condition\ConditionPluginBase;
  8. use Drupal\Core\Entity\EntityStorageInterface;
  9. use Drupal\Core\Form\FormStateInterface;
  10. use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. /**
  13. * Provides a 'Node Type' condition.
  14. *
  15. * @Condition(
  16. * id = "node_type",
  17. * label = @Translation("Node Bundle"),
  18. * context = {
  19. * "node" = @ContextDefinition("entity:node", label = @Translation("Node"))
  20. * }
  21. * )
  22. *
  23. */
  24. class NodeType extends ConditionPluginBase implements ContainerFactoryPluginInterface {
  25. /**
  26. * The entity storage.
  27. *
  28. * @var \Drupal\Core\Entity\EntityStorageInterface
  29. */
  30. protected $entityStorage;
  31. /**
  32. * Creates a new NodeType instance.
  33. *
  34. * @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
  35. * The entity storage.
  36. * @param array $configuration
  37. * The plugin configuration, i.e. an array with configuration values keyed
  38. * by configuration option name. The special key 'context' may be used to
  39. * initialize the defined contexts by setting it to an array of context
  40. * values keyed by context names.
  41. * @param string $plugin_id
  42. * The plugin_id for the plugin instance.
  43. * @param mixed $plugin_definition
  44. * The plugin implementation definition.
  45. */
  46. public function __construct(EntityStorageInterface $entity_storage, array $configuration, $plugin_id, $plugin_definition) {
  47. parent::__construct($configuration, $plugin_id, $plugin_definition);
  48. $this->entityStorage = $entity_storage;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  54. return new static(
  55. $container->get('entity.manager')->getStorage('node_type'),
  56. $configuration,
  57. $plugin_id,
  58. $plugin_definition
  59. );
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  65. $options = array();
  66. $node_types = $this->entityStorage->loadMultiple();
  67. foreach ($node_types as $type) {
  68. $options[$type->id()] = $type->label();
  69. }
  70. $form['bundles'] = array(
  71. '#title' => $this->t('Node types'),
  72. '#type' => 'checkboxes',
  73. '#options' => $options,
  74. '#default_value' => $this->configuration['bundles'],
  75. );
  76. return parent::buildConfigurationForm($form, $form_state);
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
  82. $this->configuration['bundles'] = array_filter($form_state->getValue('bundles'));
  83. parent::submitConfigurationForm($form, $form_state);
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function summary() {
  89. if (count($this->configuration['bundles']) > 1) {
  90. $bundles = $this->configuration['bundles'];
  91. $last = array_pop($bundles);
  92. $bundles = implode(', ', $bundles);
  93. return $this->t('The node bundle is @bundles or @last', array('@bundles' => $bundles, '@last' => $last));
  94. }
  95. $bundle = reset($this->configuration['bundles']);
  96. return $this->t('The node bundle is @bundle', array('@bundle' => $bundle));
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function evaluate() {
  102. if (empty($this->configuration['bundles']) && !$this->isNegated()) {
  103. return TRUE;
  104. }
  105. $node = $this->getContextValue('node');
  106. return !empty($this->configuration['bundles'][$node->getType()]);
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function defaultConfiguration() {
  112. return array('bundles' => array()) + parent::defaultConfiguration();
  113. }
  114. }