diff --git a/core/modules/block/src/BlockAccessControlHandler.php b/core/modules/block/src/BlockAccessControlHandler.php index d5c72dc..b815c41 100644 --- a/core/modules/block/src/BlockAccessControlHandler.php +++ b/core/modules/block/src/BlockAccessControlHandler.php @@ -85,11 +85,9 @@ protected function checkAccess(EntityInterface $entity, $operation, $langcode, A return AccessResult::forbidden()->cacheUntilEntityChanges($entity); } else { - $visibility = $entity->getVisibility(); $contexts = $entity->getAvailableContexts(); $conditions = []; - foreach ($visibility as $condition_id => $configuration) { - $condition = $this->manager->createInstance($condition_id, $configuration); + foreach ($entity->getVisibilityConditions() as $condition_id => $condition) { if ($condition instanceof ContextAwarePluginInterface) { try { $this->contextHandler->applyContextMapping($condition, $contexts); diff --git a/core/modules/block/src/BlockForm.php b/core/modules/block/src/BlockForm.php index 2755b39..380b930 100644 --- a/core/modules/block/src/BlockForm.php +++ b/core/modules/block/src/BlockForm.php @@ -339,7 +339,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { } } - $entity->setVisibility($form_state->getValue('visibility')); + $entity->set('visibility', $form_state->getValue('visibility')); // Save the settings of the plugin. $entity->save(); diff --git a/core/modules/block/src/BlockInterface.php b/core/modules/block/src/BlockInterface.php index 9604f08..09ca0db 100644 --- a/core/modules/block/src/BlockInterface.php +++ b/core/modules/block/src/BlockInterface.php @@ -41,14 +41,23 @@ public function getPlugin(); public function getVisibility(); /** - * Sets the visibility configurations for specified conditions. + * Gets conditions for this block. * - * @param array $visibility - * An array of plugin_id keys and configuration values. - * - * @return $this + * @return \Drupal\Core\Condition\ConditionInterface[]|\Drupal\Core\Condition\ConditionPluginCollection + * An array or collection of configured condition plugins. */ - public function setVisibility(array $visibility); + public function getVisibilityConditions(); + + /** + * Gets a visibility condition plugin instance. + * + * @param string $instance_id + * The condition plugin instance ID. + * + * @return \Drupal\Core\Condition\ConditionInterface + * A condition plugin. + */ + public function getVisibilityCondition($instance_id); /** * Sets the visibility condition configuration. diff --git a/core/modules/block/src/Entity/Block.php b/core/modules/block/src/Entity/Block.php index 0df7b2b..2444df8 100644 --- a/core/modules/block/src/Entity/Block.php +++ b/core/modules/block/src/Entity/Block.php @@ -8,6 +8,7 @@ namespace Drupal\block\Entity; use Drupal\Component\Plugin\ContextAwarePluginInterface; +use Drupal\Core\Condition\ConditionPluginCollection; use Drupal\Core\Config\Entity\ConfigEntityBase; use Drupal\block\BlockPluginCollection; use Drupal\block\BlockInterface; @@ -103,6 +104,20 @@ class Block extends ConfigEntityBase implements BlockInterface, EntityWithPlugin protected $contexts = array(); /** + * The visibility collection. + * + * @var \Drupal\Core\Condition\ConditionPluginCollection + */ + protected $visibilityCollection; + + /** + * The condition plugin manager. + * + * @var \Drupal\Core\Executable\ExecutableManagerInterface + */ + protected $conditionPluginManager; + + /** * {@inheritdoc} */ public function getPlugin() { @@ -126,7 +141,10 @@ protected function getPluginCollection() { * {@inheritdoc} */ public function getPluginCollections() { - return array('settings' => $this->getPluginCollection()); + return array( + 'settings' => $this->getPluginCollection(), + 'visibility' => $this->getVisibilityConditions(), + ); } /** @@ -219,23 +237,53 @@ public function getAvailableContexts() { * {@inheritdoc} */ public function getVisibility() { - return $this->visibility; - } - - /** - * {@inheritdoc} - */ - public function setVisibility(array $visibility) { - $this->visibility = $visibility; - return $this; + return $this->getVisibilityConditions()->getConfiguration(); } /** * {@inheritdoc} */ public function setVisibilityConfig($instance_id, array $configuration) { - $this->visibility[$instance_id] = $configuration; + $conditions = $this->getVisibilityConditions(); + if (!$conditions->has($instance_id)) { + $configuration['id'] = $instance_id; + $conditions->addInstanceId($instance_id, $configuration); + } + else { + $conditions->setInstanceConfiguration($instance_id, $configuration); + } return $this; } + /** + * {@inheritdoc} + */ + public function getVisibilityConditions() { + if (!isset($this->visibilityCollection)) { + $this->visibilityCollection = new ConditionPluginCollection($this->conditionPluginManager(), $this->get('visibility')); + } + return $this->visibilityCollection; + } + + /** + * {@inheritdoc} + */ + public function getVisibilityCondition($instance_id) { + return $this->getVisibilityConditions()->get($instance_id); + } + + /** + * Gets the condition plugin manager. + * + * @return \Drupal\Core\Executable\ExecutableManagerInterface + * The condition plugin manager. + */ + protected function conditionPluginManager() { + $this->conditionPluginManager; + if (!isset($this->conditionPluginManager)) { + $this->conditionPluginManager = \Drupal::service('plugin.manager.condition'); + } + return $this->conditionPluginManager; + } + }