Problem/Motivation

Upgrading from 10.0 to 10.6 and trying to use an entity reference field on a node that references a config term we always get a validation error like:
"field_hazards.0.target_id: This entity (config_terms_term: hazards_uncharacterized_unknown) cannot be referenced.",

The entity reference field has this for the selection handler:

settings:
  handler: 'default:config_terms_term'
  handler_settings:
    target_bundles: {  }
    auto_create: false
    target_vocab: chemical_profile_hazards

this is also generating a warning in a DSM and log messages:
Deprecated function: mb_strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in Drupal\Core\Config\Entity\Query\Condition->compile() (line 39 of core/lib/Drupal/Core/Config/Entity/Query/Condition.php).

This appears to come from the logic in \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection::buildEntityQuery() that sets the ID to null if the target bundles is an empty arry:

    // If 'target_bundles' is NULL, all bundles are referenceable, no further
    // conditions are needed.
    if (is_array($configuration['target_bundles'])) {
      // If 'target_bundles' is an empty array, no bundle is referenceable,
      // force the query to never return anything and bail out early.
      if ($configuration['target_bundles'] === []) {
        $query->condition($entity_type->getKey('id'), NULL, '=');
        return $query;
      }

Proposed resolution

override TermSelection::buildEntityQuery() to filter by vid against target_vocab

We already do this in a way in \Drupal\config_terms\Plugin\EntityReferenceSelection\TermSelection::getReferenceableEntities()

Remaining tasks

write fix add tests

User interface changes

n/a

API changes

n/a

Data model changes

n/a

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

pwolanin created an issue. See original summary.

pwolanin’s picture

Version: 8.x-1.x-dev » 2.x-dev

fixing in 2.x only for now

pwolanin’s picture

Adding a bunch of cleanup, new tests, and handling broken permission strings

pwolanin’s picture

Status: Active » Needs review
heddn’s picture

Status: Needs review » Reviewed & tested by the community

LGTM

  • pwolanin committed 6782307b on 2.x
    fix: #3589028 Unable to reference config terms in jsonapi upgrading from...
pwolanin’s picture

Status: Reviewed & tested by the community » Fixed

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.

Status: Fixed » Closed (fixed)

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