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

Issue fork drupal-2987159

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

tedbow created an issue. See original summary.

tedbow’s picture

Assigned: tedbow » Unassigned
Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new16.04 KB

I chatted with @amateescu and he said:

you can scratch the part about needing an upgrade path, I just checked manually and it's not needed because we have code which sets the selection plugin ID to `default:` when the field is created

So I have crossed of 4-5 from the proposed solution. Will delete when he confirms this is what he meant.

This patch does the other 3 items.

tedbow’s picture

Issue summary: View changes

Removing steps 4 & 5

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Postponed (maintainer needs more info)

Is this still relevant with Drupal9.5?

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)

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

If still an issue please reopen with an updated issue summary

Thanks!

amateescu’s picture

Status: Closed (outdated) » Needs work

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.

berdir’s picture

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.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

acbramley made their first commit to this issue’s fork.

acbramley’s picture

Rolled this into an MR and:

1. Reverted the test changes, not sure if we need any additional coverage
2. Added a deprecation to the query_alter hook
3. Swapped to attributes
4. Added ::validateReferenceableNewEntities to the new plugin, I don't think we need ::createNewEntity since reusable defaults to TRUE

acbramley’s picture

Status: Needs work » Needs review
smustgrave’s picture

Updated the version but not 100% how to best test this one.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Believe all threads/feedback has been addressed.

quietone’s picture

I updated credit and didn't find any unanswered questions.

needs-review-queue-bot’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new91 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

acbramley’s picture

Status: Needs work » Reviewed & tested by the community
catch’s picture

Status: Reviewed & tested by the community » Needs work

This needs a rebase.

The issue title makes it sound like we're adding a second entity reference selection handler, but we're just overriding the default entity one and including the filtering in it. I don't know if that's just me mis-reading the title or if there's a tweak that would make it a bit clearer. After understanding the actual change this all looks good to me.

acbramley’s picture

Status: Needs work » Reviewed & tested by the community

Rebased and updated the deprecation message.

catch’s picture

Version: 11.x-dev » 11.3.x-dev
Status: Reviewed & tested by the community » Fixed

We can still do this in 11.3 - updated the deprecation message for 11.3.0 instead of 11.4.

Committed/pushed to 11.x, and cherry-picked to 11.3.x, thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • catch committed 00d7504c on 11.3.x
    fix: #2987159 Create an entity reference selection plugin for custom...

  • catch committed 7f9570db on 11.x
    fix: #2987159 Create an entity reference selection plugin for custom...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.