Create an entity reference selection plugin for custom blocks that filters out non-reusable blocks

Created on 20 July 2018, almost 6 years ago
Updated 17 February 2023, over 1 year ago

Problem/Motivation

in #2976334: Allow Custom blocks to be set as non-reusable adding access restriction based on where it was used. β†’ we introduced non-reusable blocks.
We don't want them to be selectable as entity reference items(or they would be being reused.)
block_content_query_entity_reference_alter() stops them from being selected. This function is needed because there may have been selection plugins written before this issue. they should not select non-reusable blocks.

but it would be clearer if we also had a default entity reference selection plugin for Custom block entities that did not allow selecting non-reusable blocks.

Proposed resolution

  1. introduce \Drupal\block_content\Plugin\EntityReferenceSelection\BlockContentSelection
  2. update block_content_query_entity_reference_alter() to not alter the query if this class is used
  3. Move testing of this to \Drupal\Tests\system\Functional\Entity\EntityReferenceSelection\EntityReferenceSelectionAccessTest
  4. πŸŽ‰

Remaining tasks

  1. Write patch
  2. review

User interface changes

None

API changes

None

Data model changes

None

πŸ“Œ Task
Status

Needs work

Version

10.1 ✨

Component
Block contentΒ  β†’

Last updated 4 days ago

Created by

πŸ‡ΊπŸ‡ΈUnited States tedbow Ithaca, NY, USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Closing as outdated as additional info was requested 6 months ago.

    If still an issue please reopen with an updated issue summary

    Thanks!

  • Status changed to Needs work over 1 year ago
  • πŸ‡·πŸ‡΄Romania amateescu

    Just checked Drupal 10.1 and we don't have a CustomBlockSelection plugin, so this is still relevant, as well as the issue summary.

    Here's a review for the patch:

    1. +++ b/core/modules/block_content/block_content.module
      @@ -127,6 +128,12 @@ function block_content_add_body_field($block_type_id, $label = 'Body') {
         if ($query instanceof SelectInterface && $query->getMetaData('entity_type') === 'block_content' && $query->hasTag('block_content_access')) {
      

      We should add a deprecation here to make developers aware of the new selection plugin, so we can remove this hook implementation in Drupal 11.

    2. +++ b/core/modules/block_content/src/Plugin/EntityReferenceSelection/BlockContentSelection.php
      @@ -0,0 +1,30 @@
      +class BlockContentSelection extends DefaultSelection {
      

      This new selection plugin also needs to implement the two methods from SelectionWithAutocreateInterface: createNewEntity() and validateReferenceableNewEntities().

    3. +++ b/core/modules/system/tests/src/Functional/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php
      @@ -507,4 +511,100 @@ public function testCommentHandler() {
      +   * Test the comment-specific overrides of the entity handler.
      

      Copy/paste comment needs to be fixed.

  • πŸ‡¨πŸ‡­Switzerland Berdir Switzerland

    1. Don't you get the per entity type selection plugin by default if you currently have that selected too? It wouldn't apply to a custom plugin, but then you probably know what you're doing and are filtering it out too? What I mean is, do we still need that hook at all, even now?

    It's not clear to me why we're moving test coverage into system module, the logic is still in block_content, we could just keep the test, would be much easier to confirm that it doesn't change.

    That it tests comment and other handlers is for historical reasons, it used to be in the entity_reference module and tests were moved into core/system.module when that was deprecated. If anything we should have separate issues to split that up and move it to where each handler is.

Production build 0.69.0 2024