Restrict bundles for "Add new" (taxonomy term, etc) when "Add existing" is powered by an entity reference view

Created on 9 June 2024, 5 months ago
Updated 9 July 2024, 4 months ago

Problem/Motivation

IEF provides a powerful way to add new entities for entity reference fields. Creating an entity reference view provides a powerful way to select existing entities. They do not yet play well together.

It is possible to configure an entity reference field to have autocompletion powered by an entity reference view (i'd have linked to official documentation but couldn't find it). It is possible and even likely that this view only allows terms of one vocabulary, or content of one content type, or whatever entity of only one bundle— but Drupal and therefore IEF don't know that, and so IEF makes people choose the bundle before adding a new entity.

This is a follow-up on #2629836: Respect the entity reference view in "Add existing" where as bojanz noted "We'll still want some kind that will restrict the bundles shown for "Add new" based on the view, but fixing "Add existing" is a good first step."

Steps to reproduce

Create a an entity reference view and select it in an entity reference field's settings.
Configure that field in the form display to use Inline Entity Form. Simple won't even be available, and complex will have an unwanted extra step of choosing a bundle.

Proposed resolution

Would be nice if core would allow restricting even an entity reference view powered field to a single bundle, and simply enforce anything that slips in with a validation error when the (parent) content is submitted. That is unlikely to happen so whenever IEF sees the possibility of multiple bundles, it should provide a setting to restrict that to a subset of possible bundles. After all, it is certainly possible someone would want to select from multiple bundles but only allow creation of one kind.

Remaining tasks

User interface changes

API changes

Data model changes

Feature request
Status

Active

Version

3.0

Component

Code

Created by

🇺🇸United States mlncn Minneapolis, MN, USA

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

Comments & Activities

  • Issue created by @mlncn
  • 🇺🇸United States mlncn Minneapolis, MN, USA

    i think this is code i was starting to add related to this, throwing it here for now will delete if it doesn't make sense.

    /**
     * Gets the bundles for which the current user has create access.
     *
     * @return string[]
     *   The list of bundles.
     */
    protected function getCreateBundles() {
      $create_bundles = [];
      foreach ($this->getTargetBundles() as $bundle) {
        if ($this->getAccessHandler()->createAccess($bundle)) {
          $create_bundles[] = $bundle;
        }
      }
      $allowed_bundles = ['sources'];
      if ($allowed_bundles) {
        $create_bundles = array_intersect($allowed_bundles, $create_bundles);
      }
    
      return $create_bundles;
    }
    
  • 🇺🇸United States mlncn Minneapolis, MN, USA

    This is implemented in IEF Complex Open widget for the time being, and here is how, this same change could work for IEF itself:

    https://git.drupalcode.org/project/ief_complex_open/-/commit/6863903b015...

Production build 0.71.5 2024