diff --git a/core/lib/Drupal/Core/Entity/Plugin/Condition/EntityBundle.php b/core/lib/Drupal/Core/Entity/Plugin/Condition/EntityBundle.php index b0c77c40aa..a485f7eeee 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/Condition/EntityBundle.php +++ b/core/lib/Drupal/Core/Entity/Plugin/Condition/EntityBundle.php @@ -81,15 +81,11 @@ public static function create(ContainerInterface $container, array $configuratio * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { - $options = []; $bundles = $this->entityTypeBundleInfo->getBundleInfo($this->entityType->id()); - foreach ($bundles as $id => $info) { - $options[$id] = $info['label']; - } $form['bundles'] = [ '#title' => $this->pluginDefinition['label'], '#type' => 'checkboxes', - '#options' => $options, + '#options' => array_combine(array_keys($bundles), array_column($bundles, 'label')), '#default_value' => $this->configuration['bundles'], ]; return parent::buildConfigurationForm($form, $form_state); @@ -132,17 +128,37 @@ public function summary() { $bundles = $this->configuration['bundles']; $last = array_pop($bundles); $bundles = implode(', ', $bundles); - return $this->t('@bundle_type is @bundles or @last', [ + + if (empty($this->configuration['negate'])) { + return $this->t('@bundle_type is @bundles or @last', [ + '@bundle_type' => $bundle_type, + '@bundles' => $bundles, + '@last' => $last, + ]); + } + else { + return $this->t('@bundle_type is not @bundles or @last', [ + '@bundle_type' => $bundle_type, + '@bundles' => $bundles, + '@last' => $last, + ]); + } + + } + $bundle = reset($this->configuration['bundles']); + + if (empty($this->configuration['negate'])) { + return $this->t('@bundle_type is @bundle', [ '@bundle_type' => $bundle_type, - '@bundles' => $bundles, - '@last' => $last, + '@bundle' => $bundle, + ]); + } + else { + return $this->t('@bundle_type is not @bundle', [ + '@bundle_type' => $bundle_type, + '@bundle' => $bundle, ]); } - $bundle = reset($this->configuration['bundles']); - return $this->t('@bundle_type is @bundle', [ - '@bundle_type' => $bundle_type, - '@bundle' => $bundle, - ]); } /** diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityBundleConditionTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityBundleConditionTest.php index 59a5e1c830..8da2c743a0 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityBundleConditionTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityBundleConditionTest.php @@ -13,11 +13,14 @@ */ class EntityBundleConditionTest extends EntityKernelTestBase { + /** + * {@inheritdoc} + */ protected function setUp() { parent::setUp(); $this->installEntitySchema('entity_test_with_bundle'); - // Create the node bundles required for testing. + // Create the entity bundles required for testing. $bundle = EntityTestBundle::create(['id' => 'page', 'label' => 'page']); $bundle->save(); $bundle = EntityTestBundle::create(['id' => 'article', 'label' => 'article']); @@ -32,7 +35,7 @@ protected function setUp() { public function testConditions() { $manager = $this->container->get('plugin.manager.condition'); $this->createUser(); - // Get some nodes of various types to check against. + // Get some entities of various bundles to check against. $page = EntityTestWithBundle::create(['type' => 'page', 'name' => $this->randomMachineName()]); $page->save(); $article = EntityTestWithBundle::create(['type' => 'article', 'name' => $this->randomMachineName()]); @@ -40,38 +43,42 @@ public function testConditions() { $test = EntityTestWithBundle::create(['type' => 'test', 'name' => $this->randomMachineName()]); $test->save(); - // Grab the node type condition and configure it to check against node type - // of 'article' and set the context to the page type node. + // Grab the bundle condition and configure it to check against bundle of + // 'article' and set the context to the page type entity. /* @var \Drupal\Core\Entity\Plugin\Condition\EntityBundle $condition */ $condition = $manager->createInstance('entity_bundle:entity_test_with_bundle') ->setConfig('bundles', ['article' => 'article']) ->setContextValue('entity_test_with_bundle', $page); - $this->assertFalse($condition->execute(), 'Page type nodes fail node type checks for articles.'); + $this->assertFalse($condition->execute(), 'Page type entities fail bundle checks for articles.'); // Check for the proper summary. $this->assertEquals($condition->summary(), 'Test entity bundle is article'); - // Set the node type check to page. + // Set the bundle check to page. $condition->setConfig('bundles', ['page' => 'page']); - $this->assertTrue($condition->execute(), 'Page type nodes pass node type checks for pages'); + $this->assertTrue($condition->execute(), 'Page type entities pass bundle checks for pages'); // Check for the proper summary. $this->assertEquals($condition->summary(), 'Test entity bundle is page'); - // Set the node type check to page or article. + // Set the bundle check to page or article. $condition->setConfig('bundles', ['page' => 'page', 'article' => 'article']); - $this->assertTrue($condition->execute(), 'Page type nodes pass node type checks for pages or articles'); + $this->assertTrue($condition->execute(), 'Page type entities pass bundle checks for pages or articles'); // Check for the proper summary. $this->assertEquals($condition->summary(), 'Test entity bundle is page or article'); - // Set the context to the article node. + // Set the context to the article entity. $condition->setContextValue('entity_test_with_bundle', $article); - $this->assertTrue($condition->execute(), 'Article type nodes pass node type checks for pages or articles'); + $this->assertTrue($condition->execute(), 'Article type entities pass bundle checks for pages or articles'); - // Set the context to the test node. + // Set the context to the test entity. $condition->setContextValue('entity_test_with_bundle', $test); - $this->assertFalse($condition->execute(), 'Test type nodes pass node type checks for pages or articles'); + $this->assertFalse($condition->execute(), 'Test type entities pass bundle checks for pages or articles'); // Check a greater than 2 bundles summary scenario. - $condition->setConfig('bundles', ['page' => 'page', 'article' => 'article', 'test' => 'test']); + $condition->setConfig('bundles', [ + 'page' => 'page', + 'article' => 'article', + 'test' => 'test', + ]); $this->assertEquals($condition->summary(), 'Test entity bundle is page, article or test'); // Test Constructor injection.