From 7076c704ad5f81e6e0a744454a57778f632c7abe Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org> Date: Fri, 28 Oct 2016 13:45:27 +0100 Subject: [PATCH] Issue #2632434 by yongt9412, EclipseGc, ivanjaros, Berdir: Conditions are not limited by available contexts --- core/modules/block/src/BlockForm.php | 2 +- core/modules/block/src/Tests/BlockUiTest.php | 7 +++- .../Condition/ConditionTestNoExistingType.php | 34 +++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 core/modules/system/tests/modules/condition_test/src/Plugin/Condition/ConditionTestNoExistingType.php diff --git a/core/modules/block/src/BlockForm.php b/core/modules/block/src/BlockForm.php index 269a98b39224..42dfbc3b2f6e 100644 --- a/core/modules/block/src/BlockForm.php +++ b/core/modules/block/src/BlockForm.php @@ -237,7 +237,7 @@ protected function buildVisibilityInterface(array $form, FormStateInterface $for // @todo Allow list of conditions to be configured in // https://www.drupal.org/node/2284687. $visibility = $this->entity->getVisibility(); - foreach ($this->manager->getDefinitions() as $condition_id => $definition) { + foreach ($this->manager->getDefinitionsForContexts($form_state->getTemporaryValue('gathered_contexts')) as $condition_id => $definition) { // Don't display the current theme condition. if ($condition_id == 'current_theme') { continue; diff --git a/core/modules/block/src/Tests/BlockUiTest.php b/core/modules/block/src/Tests/BlockUiTest.php index f2bcaae2dbff..65183cdce629 100644 --- a/core/modules/block/src/Tests/BlockUiTest.php +++ b/core/modules/block/src/Tests/BlockUiTest.php @@ -17,7 +17,7 @@ class BlockUiTest extends WebTestBase { * * @var array */ - public static $modules = array('block', 'block_test', 'help'); + public static $modules = array('block', 'block_test', 'help', 'condition_test'); protected $regions; @@ -248,6 +248,11 @@ public function testContextAwareBlocks() { $this->drupalGet(''); $this->assertText('No context mapping selected.'); $this->assertNoText('User context found.'); + + // Tests that conditions with missing context are not displayed. + $this->drupalGet('admin/structure/block/manage/testcontextawareblock'); + $this->assertNoRaw('No existing type'); + $this->assertNoFieldByXPath('//*[@name="visibility[condition_test_no_existing_type][negate]"]'); } /** diff --git a/core/modules/system/tests/modules/condition_test/src/Plugin/Condition/ConditionTestNoExistingType.php b/core/modules/system/tests/modules/condition_test/src/Plugin/Condition/ConditionTestNoExistingType.php new file mode 100644 index 000000000000..ef00053fe3b5 --- /dev/null +++ b/core/modules/system/tests/modules/condition_test/src/Plugin/Condition/ConditionTestNoExistingType.php @@ -0,0 +1,34 @@ +<?php + +namespace Drupal\condition_test\Plugin\Condition; + +use Drupal\Core\Condition\ConditionPluginBase; + +/** + * Provides a condition that has a no existing context. + * + * @Condition( + * id = "condition_test_no_existing_type", + * label = @Translation("No existing type"), + * context = { + * "no_existing_type" = @ContextDefinition("no_existing_type", label = @Translation("No existing type")), + * } + * ) + */ +class ConditionTestNoExistingType extends ConditionPluginBase { + + /** + * {@inheritdoc} + */ + public function evaluate() { + return TRUE; + } + + /** + * {@inheritdoc} + */ + public function summary() { + return $this->t('Condition that requires a non-existent context.'); + } + +} -- GitLab