Problem/Motivation

The default entity reference selection method requires to choose at least one bundle of the target entity type.

screenshot of the entity type reference field settings with default selection method

This adds the following limitations:

  1. It is not possible to create a new entity reference field (with the default selection method) to entity types with not yet configured bundles. See the related issue #3033652: Be more helpful if there's no bundle to choose from
  2. When a new bundle is created, site builders must review all entity reference fields to such entity type in order of adding the new bundle in the configuration.
  3. Modules that provides reference fields as part of their installation configuration must declare dependency on any other module that add one or more bundles on the target entity type.

I try to figure out why the bundle selection is required, but I didn't found a clear reason. Current implementation works for entity types that do not have support for bundles, so it should not be hard to add this feature.

Proposed resolution

Make the bundle setting optional. An option to select "all except" the selected bundles could also be useful.

Remaining tasks

TBD.

User interface changes

As described.

API changes

None.

Data model changes

None.

Release notes snippet

TBD.

CommentFileSizeAuthor
#7 Screenshot 2021-02-27 at 14.04.33.png61.37 KBartemboiko

Comments

manuel.adan created an issue. See original summary.

amateescu’s picture

There is a long history with this bundle selection element and why it's required without any bundle selected by default. If you're keen to read up on all the old discussion about this, you can start with #1847596-119: Remove Taxonomy term reference field in favor of Entity reference (point 8. from @xjm's comment) and #1953444: Make 'target bundles' required on the Entity reference instance settings form which actually implemented the change.

To answer the three points from the current IS:

It is not possible to create a new entity reference field (with the default selection method) to entity types with not yet configured bundles.

That sounds like a problem on its own, and should be fixed by the reference issue.

When a new bundle is created, site builders must review all entity reference fields to such entity type in order of adding the new bundle in the configuration.

That's actually a good thing, because the entities from the new bundle shouldn't be exposed automatically to (potentially) anonymous users of the site.

Modules that provides reference fields as part of their installation configuration must declare dependency on any other module that add one or more bundles on the target entity type.

That's also a good thing because it enforces consistency in the (config) data model of the site.

As for the proposed resolution, making the bundle selection optional will introduce a usability regression on this form, so all we can do is provide the "Select all" feature, mentioned in #1953444-11: Make 'target bundles' required on the Entity reference instance settings form as well.

manuel.adan’s picture

That's actually a good thing, because the entities from the new bundle shouldn't be exposed automatically to (potentially) anonymous users of the site.

Despite opening an entity form with entity reference fields to anonymous users is possible, I think it is not the most common situation. Anyway, this change would not reduce security, since any new bundle would not be added automatically to existing reference fields that already have some bundle selected, working as they did before the new bundle was added.

That's also a good thing because it enforces consistency in the (config) data model of the site.

If one module just requires an entity type, regardless the existing bundles, developer is forced to introduce dependencies on each module that defines any bundle just to satisfy the field reference restrictions. IMOH, it has no sense since it is not a real dependency.

I finally created a view with a reference display that list any node type, and I used the view selection mode in the entity reference field. I got fine control on the reference field results as a plus for the extra work. Other option could be implement a custom selection method plugin.

At the end, site builders will find a way to do what the site needs. I think that by cutting options with no good reason for it we just only make things hard.

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.

artemboiko’s picture

StatusFileSize
new61.37 KB

Hi there,

There are a possibility just to set $form['target_bundles']['#required'] to FALSE or add new option Select all with NULL key. In web/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php other logic for work with NULL is existed here.

But fully agree with @amateescu with all points, and seems like no need to add this.

In case if we need Select all option we can create view entity reference without filtering by bundle.

And for this:

An option to select "all except" the selected bundles could also be useful.

We can use Is not one of filter parameter in view filter by type.

screenshot view filter

andypost’s picture

It looks more like documentation issue, ERSelection plugins needs better examples (when better to create own selection plugin)

Other issues with views selection could benefit more attention if it will be proposed in some official docs

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.

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.

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.

jonathanshaw’s picture

Status: Active » Closed (duplicate)
Related issues: +#3031637: Entity reference: allow specifiying that every bundle is allowed

We have another issue focused on 'select all': #3031637: Entity reference: allow specifiying that every bundle is allowed.

So this is a combination of duplicate/won't fix.