diff --git a/core/lib/Drupal/Core/Access/AccessDependentInterface.php b/core/lib/Drupal/Core/Access/AccessDependentInterface.php index 7afc93d4ef..6cb3f899c3 100644 --- a/core/lib/Drupal/Core/Access/AccessDependentInterface.php +++ b/core/lib/Drupal/Core/Access/AccessDependentInterface.php @@ -3,24 +3,39 @@ namespace Drupal\Core\Access; /** - * Interface for objects that are dependent on other accessible objects. + * Interface for AccessibleInterface objects that have an access dependency. + * + * Objects should implement this interface when their access depends on access + * to another object that implements \Drupal\Core\Access\AccessibleInterface. + * This interface simply provides the getter and setter methods for the access + * dependency object. Objects that implement this interface are responsible for + * checking access of the access dependency because the dependency may not take + * effect in all cases. For instance an entity may only need the access + * dependency set when it is embedded within another entity and its access + * should be dependent on access to the entity in which it is embedded. + * + * To check the access to the dependency the object implementing this interface + * can use code like this: + * @code + * $accessible->getAccessDependee()->access($op, $account, TRUE); + * @endcode */ -interface AccessDependentInterface { +interface AccessDependentInterface extends AccessibleInterface { /** - * Sets the access dependee. + * Sets the access dependency. * - * @param \Drupal\Core\Access\AccessibleInterface $access_dependee - * The access dependee. + * @param \Drupal\Core\Access\AccessibleInterface $access_dependency + * The access dependency. */ - public function setAccessDependee(AccessibleInterface $access_dependee); + public function setAccessDependency(AccessibleInterface $access_dependency); /** - * Gets the access dependee. + * Gets the access dependency. * - * @return \Drupal\Core\Access\AccessibleInterface - * The access dependee. + * @return \Drupal\Core\Access\AccessibleInterface|null + * The access dependency or NULL if none has been set. */ - public function getAccessDependee(); + public function getAccessDependency(); } diff --git a/core/lib/Drupal/Core/Access/AccessDependentTrait.php b/core/lib/Drupal/Core/Access/AccessDependentTrait.php index 58b2e17ad1..0401736953 100644 --- a/core/lib/Drupal/Core/Access/AccessDependentTrait.php +++ b/core/lib/Drupal/Core/Access/AccessDependentTrait.php @@ -8,25 +8,25 @@ trait AccessDependentTrait { /** - * The object that is depended on. + * The access dependency. * * @var \Drupal\Core\Access\AccessibleInterface */ - protected $accessDependee; + protected $accessDependency; /** * {@inheritdoc} */ - public function setAccessDependee(AccessibleInterface $access_dependee) { - $this->accessDependee = $access_dependee; + public function setAccessDependency(AccessibleInterface $access_dependency) { + $this->accessDependency = $access_dependency; } /** * {@inheritdoc} */ - public function getAccessDependee() { - return $this->accessDependee; + public function getAccessDependency() { + return $this->accessDependency; } } diff --git a/core/modules/block_content/block_content.install b/core/modules/block_content/block_content.install index a2bbceae84..fd5d71f944 100644 --- a/core/modules/block_content/block_content.install +++ b/core/modules/block_content/block_content.install @@ -157,12 +157,12 @@ function block_content_update_8600() { * Update 'block_content' view to filter on new 'reusable' field. */ function block_content_update_8601() { - $config_factory = \Drupal::configFactory(); - $view = $config_factory->getEditable('views.view.block_content'); + $view = \Drupal::configFactory()->getEditable('views.view.block_content'); $base_table = 'block_content_field_data'; - // Make sure the view's 'base_table' is correct. The default view could have - // been deleted and the view ID used for a different view. - if ($view->get('base_table') !== $base_table) { + // Make sure the view's 'base_table' is correct and the View is not new. The + // default view could have been deleted and possibly the view ID could be used + // for a different view. + if ($view->get('base_table') !== $base_table or $view->isNew()) { return; } foreach ($view->get('display') as $display_name => $display) { @@ -171,13 +171,13 @@ function block_content_update_8601() { ($display_name === 'default' || isset($display['display_options']['filters']))) { // Save off the base part of the config path we are updating. $base = "display.$display_name.display_options.filters.reusable"; - $view->set("$base.id", 'reusable'); - $view->set("$base.plugin_id", 'boolean'); - $view->set("$base.table", $base_table); - $view->set("$base.field", "reusable"); - $view->set("$base.value", "1"); - $view->set("$base.entity_type", "block_content"); - $view->set("$base.entity_field", "reusable"); + $view->set("$base.id", 'reusable') + ->set("$base.plugin_id", 'boolean') + ->set("$base.table", $base_table) + ->set("$base.field", "reusable") + ->set("$base.value", "1") + ->set("$base.entity_type", "block_content") + ->set("$base.entity_field", "reusable"); } } $view->save(); diff --git a/core/modules/block_content/block_content.module b/core/modules/block_content/block_content.module index e867b32a8b..2478ef752d 100644 --- a/core/modules/block_content/block_content.module +++ b/core/modules/block_content/block_content.module @@ -10,7 +10,7 @@ use Drupal\field\Entity\FieldStorageConfig; use Drupal\Core\Database\Query\SelectInterface; use Drupal\Core\Database\Query\AlterableInterface; -use Drupal\Core\Database\Query\Condition; +use Drupal\Core\Condition\ConditionInterface; /** * Implements hook_help(). @@ -112,7 +112,7 @@ function block_content_add_body_field($block_type_id, $label = 'Body') { /** * Implements hook_query_TAG_alter(). * - * Alter any query with all tags: + * Alters any query with all tags: * entity_reference, entity_query_block_content, entity_query_block_content. * * These queries should only return reusable blocks unless a condition on @@ -128,17 +128,18 @@ function block_content_add_body_field($block_type_id, $label = 'Body') { * @see \Drupal\block_content\BlockContentAccessControlHandler */ function block_content_query_block_content_access_alter(AlterableInterface $query) { - if ($query instanceof SelectInterface && $query->hasAllTags('entity_query_block_content', 'entity_reference') && array_key_exists('block_content_field_data', $query->getTables())) { + $data_table = \Drupal::entityTypeManager()->getDefinition('block_content')->getDataTable(); + if ($query instanceof SelectInterface && $query->hasAllTags('entity_query_block_content', 'entity_reference') && array_key_exists($data_table, $query->getTables())) { $reusable_conditions = array_filter($query->conditions(), '_block_content_has_reusable_condition'); if (empty($reusable_conditions)) { - // If no reusable condition set to one. - $query->condition('block_content_field_data.reusable', TRUE); + // If no reusable condition create a condition set to TRUE. + $query->condition("$data_table.reusable", TRUE); } } } /** - * Utility function find nested conditions using the reusable field. + * Utility function to find nested conditions using the reusable field. * * @param mixed $condition * The conditions to check. @@ -151,7 +152,7 @@ function _block_content_has_reusable_condition($condition) { return FALSE; } $field = isset($condition['field']) ? $condition['field'] : NULL; - if (is_object($field) && $field instanceof Condition) { + if (is_object($field) && $field instanceof ConditionInterface) { return _block_content_has_reusable_condition($field->conditions()); } if (isset($condition['#conjunction'])) { diff --git a/core/modules/block_content/src/BlockContentAccessControlHandler.php b/core/modules/block_content/src/BlockContentAccessControlHandler.php index 9f5487710d..cc2e9e5cfc 100644 --- a/core/modules/block_content/src/BlockContentAccessControlHandler.php +++ b/core/modules/block_content/src/BlockContentAccessControlHandler.php @@ -29,9 +29,9 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter /** @var \Drupal\block_content\BlockContentInterface $entity */ if ($entity->isReusable() === FALSE) { if (!$entity instanceof AccessDependentInterface) { - throw new \Exception("Non-reusable block entities must implement \Drupal\Core\Access\AccessDependentInterface for access control."); + throw new \LogicException("Non-reusable block entities must implement \Drupal\Core\Access\AccessDependentInterface for access control."); } - $dependee = $entity->getAccessDependee(); + $dependee = $entity->getAccessDependency(); if (empty($dependee)) { return AccessResult::forbidden("Non-reusable blocks must set an access dependee for access control.")->addCacheableDependency($dependee); } diff --git a/core/modules/block_content/src/Entity/BlockContent.php b/core/modules/block_content/src/Entity/BlockContent.php index 3e12cf9253..e382811106 100644 --- a/core/modules/block_content/src/Entity/BlockContent.php +++ b/core/modules/block_content/src/Entity/BlockContent.php @@ -207,7 +207,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { $fields['reusable'] = BaseFieldDefinition::create('boolean') ->setLabel(t('Reusable')) ->setDescription(t('A boolean indicating whether this block is reusable.')) - ->setDefaultValue(TRUE); + ->setDefaultValue(TRUE) + ->setInitialValue(TRUE); return $fields; } @@ -302,8 +303,7 @@ public function isReusable() { * {@inheritdoc} */ public function setReusable($reusable = TRUE) { - $this->set('reusable', $reusable); - return $this; + return $this->set('reusable', $reusable); } /**