Problem/Motivation

When using the entity reference selection with a configuration entity type, i.e. Views, it will return disabled configuration entities.

Steps to reproduce

  • Disable a View on the site
  • Create a new Entity Reference field on a content type, set it to reference Views
  • Try to create content and see in the field options, your disabled view is visible

Proposed resolution

Because there is no entity query alter for configuration entities, DefaultSelection adds a status condition for configuration entity types with a status key. A new include_disabled_config handler setting controls it: FALSE leaves disabled entities out, TRUE includes them, and NULL (the default, and what fields saved before this change have) keeps the current behavior. The field settings form shows an "Include disabled entities" checkbox for these entity types.

Remaining tasks

  • Tests
  • Decide whether the default should be FALSE instead of NULL, and whether that needs an update path.
  • Decide how existing references to an entity that is later disabled should be validated.
  • Change record for the new include_disabled_config setting.
  • Review

User interface changes

  • Entity reference fields to config entities with a status (such as views or text formats) no longer offer disabled entities once their field settings are saved without "Include disabled entities" checked.
  • Adds an "Include disabled entities" checkbox to the field's reference settings for those entity types.

API changes

  • New include_disabled_config setting (boolean, nullable) for the default entity reference selection handler.

Data model changes

None

Release notes snippet

Issue fork drupal-3367492

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

Dave Reid created an issue. See original summary.

dave reid’s picture

Status: Active » Needs review
smustgrave’s picture

Status: Needs review » Needs work

+1 to the feature.

Seems to have caused a number of failures though.

dave reid’s picture

I am starting to feel strongly that this should be the default behavior going forward and the default should be FALSE instead of NULL. I'm not sure what's best to get accepted though.

dave reid’s picture

Issue tags: +Needs tests

Sigh, I just had an error in the config schema.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

dave reid’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs tests +Needs change record

I rebased the MR onto main and added test coverage.

  • The Query.php cleanup (str_starts_with() and str_ends_with()) and the phpstan baseline change are gone: main already has both.
  • New kernel test, DefaultSelectionConfigEntityStatusTest, using config_test (which has a status key) and config_query_test (which doesn't).
  • The checkbox now names the entity type, e.g. "Include disabled text formats" instead of "Include disabled entities".

The test covers:

  • getReferenceableEntities(), countReferenceableEntities() and validateReferenceableEntities() leave out disabled entities when include_disabled_config is FALSE, and return them when it's TRUE.
  • Handlers without the setting, such as fields saved before this change, still return disabled entities.
  • Entity types without a status key aren't filtered.
  • The configuration form shows the checkbox, with its label, only for config entity types with a status key.
  • A field saved with the setting passes the config schema check.

The new assertions fail without the change to DefaultSelection and the schema.

The default stays NULL, so existing fields and code that builds a handler without the setting (such as entity_autocomplete elements) keep returning disabled entities, and no update path is needed. A field gets the new behavior once its settings form is saved, because the unchecked checkbox saves FALSE.

When a referenced config entity is disabled, content that already references it fails validation on its next save ("This entity cannot be referenced."), since ValidReferenceConstraintValidator only skips existing references the user can't view. I think that's right: a field set to leave out disabled entities shouldn't keep pointing at one.

Generated with the help of an LLM.

DRAFT, rewrite in your own words: I used Claude Code (Claude Opus 5.5) to rebase the MR onto main, write the new kernel test, and draft this comment. I wrote the original fix and chose its approach. I reviewed the test and checked that it fails without the fix.

dave reid’s picture

dave reid’s picture

Issue summary: View changes
needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new1.68 KB

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. 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.

dave reid’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs upgrade path

Think for the new key we will need an upgrade hook, ConfigEntityUpdater should be able to cover it I assume.

smustgrave’s picture

For the CR can we include a snippet in before for field.field.*.yml

dave reid’s picture

We can add support for the field types we know about, but entity selection plugins can be used by anything (and are also independently used by contrib modules), so I think the only way to reliably do it would we'd have to navigate every config schema, into every possible tree value, seeking for the entity selection config key. That's why I just left it as nullable and making the behavior work out of the box without it having to be updated. It would be weird to update some configs but not all configs, I would advocate for *not* having an update hook in this case for this reason but I'm willing to write it with the understanding it would only be for entity reference type fields.

smustgrave’s picture

Status: Needs work » Needs review
Issue tags: +Needs subsystem maintainer review

Let’s try and get feedback from the submaintainers