Problem/Motivation

If no eligible referenceable entity exists, the user won't have anything to choose from, nor may they create an entity, nor may they do anything about it. It's just clutter and a distraction, in some cases amounting to a WTF, particularly if the field is marked required, so the form can't be submitted at all.

non-required required
Non-required autocomplete Required autocomplete
Non-required select Required select
Non-required radios Required radios

Steps to reproduce

This scenario can be reproduced in two ways:

  1. When the target bundle that the entity reference field is referring to has no entities.
  2. When the target bundle that the entity reference field is referring to does not exist.

While the first situation sounds a bit irrational (yet likely to occur), the second situation is a possible edge case. In both the scenarios the user is left with no referenceable entities.

Proposed resolution

Providing a message that describes the problem to the user would help but there is a broader issue that needs discussion for a more complete solution.

Remaining tasks

Define the scope of this issue.
Come up with a complete solution.
Tests

User interface changes

Added descriptive text to ER widgets when no referencable entities are available.

Issue fork drupal-3038261

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

Pancho created an issue. See original summary.

pancho’s picture

Title: Don't render entity reference widget at all if there's no eligible target bundle » Don't render entity reference widget at all if there's no referenceable entity
Issue summary: View changes

Unfortunately this is less easy and less straightforward than I hoped it would be, so we probably shouldn't do this step-by-step but should think it through to come up with a complete solution. Finally, the case of target bundles being set up correctly, but no entity being referenceable is the slightly more relevant situation.

Also, we might not want to mess with #required fields.

So if for any reason there's no referenceable entity available, and the field is non-required, we should:

  1. check if target bundles are missing => then show a helpful message for admin users, skip rendering for all others
  2. otherwise there's simply no referenceable entity available, so for autocomplete widgets check if autocreate is allowed => then everything's fine
  3. otherwise show a helpful message for users who may add one, for the others skip rendering.

For required fields, we shouldn't skip rendering the field for non-admin users, but instead show an sufficiently precise error message. The user might still not be able to change anything about it, so can't submit the form, but at least it is no longer a WTF experience, if properly explained.

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.

seanb’s picture

This has been discussed for the media library widget in the UX meeting today: https://www.youtube.com/watch?v=MG0SgDMWsH0

I think the general consensus was we should try to show a helpful message to users instead of hiding the widget. So for editor users without permissions to edit the field or create new bundles of the referenced entity type it should be something like:
There are no [entity type] types available. Please contact a site administrator.

For users with permissions to create new bundles of the entity type, it should be something like:
There are no [entity type] types available. Please <a href=":url">add a [entity type] type</a> and enable it for this field.

Maybe we could change the scope of the issue to implement it here?

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.

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.

acbramley’s picture

Title: Don't render entity reference widget at all if there's no referenceable entity » Provide help text on Entity Reference widgets if there's no referenceable entity
Category: Bug report » Feature request
Issue tags: +Bug Smash Initiative

I think the comments in #4 make more sense and would be easier to achieve than hiding the widget, therefore marking this as a feature.

sakthi_dev’s picture

Assigned: Unassigned » sakthi_dev

sakthi_dev’s picture

Assigned: sakthi_dev » Unassigned
Status: Active » Needs review

Please review.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new2.09 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 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.

sakthi_dev’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Will need test coverage

kunal.sachdev’s picture

Assigned: Unassigned » kunal.sachdev
kunal.sachdev’s picture

Assigned: kunal.sachdev » Unassigned
srishtiiee’s picture

Assigned: Unassigned » srishtiiee
lauriii’s picture

Issue tags: +Field UX
tim.plunkett’s picture

Issue summary: View changes
Issue tags: +Needs issue summary update
srishtiiee’s picture

Assigned: srishtiiee » Unassigned
Issue summary: View changes
Issue tags: -Needs issue summary update

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

acbramley’s picture

Issue summary: View changes

Provided some feedback, we still need tests and we haven't got any changes to the autocomplete widget which seems like this issue is targeted at from the IS.

Updating IS wording also.

omkar.podey made their first commit to this issue’s fork.

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.

quietone’s picture

Component: entity_reference.module » field system

entity_reference module is no longer in core.