Duplicate entity titles cause entity_embed_dialog_form_validate to select incorrect entity

Created on 16 March 2016, over 8 years ago
Updated 3 April 2024, 3 months ago

Problem/Motivation

When selecting an entity using an Entity Embed button, the entity_embed_dialog_form_validate function will sometimes use just the node title to select the entity. If there are two entities with the same node title, this can cause the wrong entity to be selected. The function also doesn't take the selected bundles into account when choosing the entity.

The issue is around line 324 in entity_embed.admin.inc

        // The entity ID may be either the ID (integer) of the entity or the
        // entity's title (string).
        if (is_numeric($title)) {
          $entities = entity_load($entity_type, array($title));
        }
        else {
          $entities = entity_load($entity_type, FALSE, array($entity_info['entity keys']['label'] => array($title)));
        }

Compounding this, the api suggests to use EFQ instead of entity_load if conditions are needed.

Proposed resolution

In my specific case, I've broken each bundle out into its own button. This code seems to fix the issue, but will only work if a single bundle is set for the button.

        // The entity ID may be either the ID (integer) of the entity or the
        // entity's title (string).
        if (is_numeric($title)) {
          $entities = entity_load($entity_type, array($title));
        }
        else {
          $bundle = reset($form_state['build_info']['args'][1]->entity_type_bundles);
          $entities = entity_load($entity_type, FALSE, array($entity_info['entity keys']['label'] => array($title), $entity_info['entity keys']['bundle'] => array($bundle)));
        }

I don't have time to abstract this for all cases now, but I'm guessing that the entity_load will have to be changed to and EFQ call that takes all of entity_type_bundles as conditions. Furthermore, it's possible that a site has two entities of the same bundle and the same title, in which case this would be even more random. This might require changing how the autocomplete works, possibly adding the nid somewhere in the autocomplete results.

Remaining tasks

Do stuff.

πŸ› Bug report
Status

Closed: outdated

Version

3.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States froboy Chicago, IL

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024