diff --git a/core/modules/taxonomy/config/schema/taxonomy.schema.yml b/core/modules/taxonomy/config/schema/taxonomy.schema.yml index c3d0bd4..81810f0 100644 --- a/core/modules/taxonomy/config/schema/taxonomy.schema.yml +++ b/core/modules/taxonomy/config/schema/taxonomy.schema.yml @@ -112,3 +112,10 @@ entity_form_display.field.taxonomy_autocomplete: placeholder: type: label label: 'Placeholder' +condition.plugin.vocabulary: + type: condition.plugin + mapping: + bundles: + type: sequence + sequence: + - type: string diff --git a/core/modules/taxonomy/src/Plugin/Condition/VocabularyCondition.php b/core/modules/taxonomy/src/Plugin/Condition/VocabularyCondition.php new file mode 100644 index 0000000..17cc0b3 --- /dev/null +++ b/core/modules/taxonomy/src/Plugin/Condition/VocabularyCondition.php @@ -0,0 +1,135 @@ +vocabularyStorage = $entity_storage; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $container->get('entity.manager')->getStorage('taxonomy_vocabulary'), + $configuration, + $plugin_id, + $plugin_definition + ); + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, array &$form_state) { + $form = parent::buildConfigurationForm($form, $form_state); + $options = array(); + /* @var \Drupal\taxonomy\VocabularyInterface[] $vocabularies */ + $vocabularies = $this->vocabularyStorage->loadMultiple(); + foreach ($vocabularies as $vocabulary) { + $options[$vocabulary->id()] = $vocabulary->label(); + } + $form['bundles'] = array( + '#title' => $this->t('Vocabularies'), + '#type' => 'checkboxes', + '#options' => $options, + '#default_value' => $this->configuration['bundles'], + ); + return $form; + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return array( + 'bundles' => array(), + ) + parent::defaultConfiguration(); + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, array &$form_state) { + parent::submitConfigurationForm($form, $form_state); + $this->configuration['bundles'] = array_filter($form_state['values']['bundles']); + } + + /** + * {@inheritdoc} + */ + public function summary() { + if (count($this->configuration['bundles']) > 1) { + $bundles = $this->configuration['bundles']; + $last = array_pop($bundles); + $bundles = implode(', ', $bundles); + return $this->t('The vocabulary bundle is @bundles or @last', array( + '@bundles' => $bundles, + '@last' => $last, + )); + } + $bundle = reset($this->configuration['bundles']); + return $this->t('The vocabulary bundle is @bundle', array('@bundle' => $bundle)); + } + + /** + * {@inheritdoc} + */ + public function evaluate() { + if (empty($this->configuration['bundles']) && !$this->isNegated()) { + return TRUE; + } + /* @var \Drupal\taxonomy\TermInterface $term */ + $term = $this->getContextValue('taxonomy_term'); + return !empty($this->configuration['bundles'][$term->getVocabularyId()]); + } + +} diff --git a/core/modules/taxonomy/src/Tests/Plugin/Condition/VocabularyConditionTest.php b/core/modules/taxonomy/src/Tests/Plugin/Condition/VocabularyConditionTest.php new file mode 100644 index 0000000..7606f2a --- /dev/null +++ b/core/modules/taxonomy/src/Tests/Plugin/Condition/VocabularyConditionTest.php @@ -0,0 +1,132 @@ + 'Vocabulary Condition Plugins', + 'description' => 'Tests conditions provided by the taxonomy module.', + 'group' => 'Condition API', + ); + } + + /** + * {@inheritdoc} + */ + public function setUp() { + parent::setUp(); + $this->installEntitySchema('taxonomy_term'); + } + + /** + * Tests the vocabulary conditions. + */ + public function testConditions() { + + /* @var \Drupal\Component\Plugin\PluginManagerInterface $manager */ + $manager = $this->container->get('plugin.manager.condition'); + + // Create the vocabularies required for testing. + $vocab1 = $this->createVocabulary(); + $vocab2 = $this->createVocabulary(); + + $term1 = $this->createTerm($vocab1); + $term2 = $this->createTerm($vocab2); + + // Assert term1 is in vocabulary1. + /* @var \Drupal\taxonomy\Plugin\Condition\VocabularyCondition $condition */ + $condition = $manager->createInstance('vocabulary') + ->setConfig('bundles', array($vocab1->id() => $vocab1->label())) + ->setContextValue('taxonomy_term', $term1); + + $this->assertTrue($condition->execute(), String::format('The term @term is in vocabulary @vocab', array( + '@term' => $term1->label(), + '@vocab' => $vocab1->label(), + ))); + + // Assert the summary text is correct. + $this->assertEqual($condition->summary(), String::format('The vocabulary bundle is @vocab', array('@vocab' => $vocab1->label()))); + + // Assert a term not in a vocabulary does not match. + $condition = $manager->createInstance('vocabulary') + ->setConfig('bundles', array($vocab1->id() => $vocab1->label())) + ->setContextValue('taxonomy_term', $term2); + + $this->assertFalse($condition->execute(), String::format('The term @term is NOT in vocabulary @vocab', array( + '@term' => $term1->label(), + '@vocab' => $vocab2->label(), + ))); + + // Assert term1 is in in either vocab1 or vocab2. + /* @var \Drupal\taxonomy\Plugin\Condition\VocabularyCondition $condition */ + $condition = $manager->createInstance('vocabulary') + ->setConfig('bundles', array( + $vocab1->id() => $vocab1->label(), + $vocab2->id() => $vocab2->label(), + )) + ->setContextValue('taxonomy_term', $term1); + + $this->assertTrue($condition->execute(), String::format('The term @term is in vocabulary @vocab', array( + '@term' => $term1->label(), + '@vocab' => $vocab1->label(), + ))); + + // Assert the summary text is correct. + $this->assertEqual($condition->summary(), String::format('The vocabulary bundle is @vocab1 or @vocab2', array( + '@vocab1' => $vocab1->label(), + '@vocab2' => $vocab2->label(), + ))); + + } + + /** + * Returns a new vocabulary with random properties. + */ + protected function createVocabulary() { + // Create a vocabulary. + $vocabulary = Vocabulary::create(array( + 'name' => $this->randomName(), + 'description' => $this->randomName(), + 'vid' => Unicode::strtolower($this->randomName()), + 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, + )); + $vocabulary->save(); + return $vocabulary; + } + + /** + * Returns a new term with random properties in vocabulary $vid. + */ + protected function createTerm(VocabularyInterface $vocabulary) { + $term = Term::create(array( + 'name' => $this->randomName(), + 'vid' => $vocabulary->id(), + 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, + )); + $term->save(); + return $term; + } +}