- Issue created by @mlncn
- ๐บ๐ธUnited States mlncn Minneapolis, MN, USA
OK this is actually a bug, that it does not already work the way i expect:
if (!$allow_new && $allow_existing) { // Only count referencable entities if existing entities are allowed // to be referenced otherwise we set the variable to false. /** @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface $handler */ $handler = $this->selectionManager->getInstance($options); $have_multiple_existing_entities = count($handler->getReferenceableEntities(NULL, 'CONTAINS', 2)) > 1; } else { $have_multiple_existing_entities = FALSE; }
// Determine if a reference may be removed. // Unless the user has permission to delete the entity, then they should // not be able to remove it if that will lead to its deletion. $may_remove_existing = $settings['removed_reference'] !== self::REMOVED_DELETE || $entity->access('delete'); // Don't allow a user to remove the only entity if an entity is required // and the user cannot replace the entity if they remove it, because // this would put the form in an unrecoverable state. $can_replace_last_reference = $allow_new || ($allow_existing && $have_multiple_existing_entities); $reference_is_not_required = !$element['#required'] || $entities_count > 1 || $can_replace_last_reference; // Unsaved entities may always be removed. $may_remove = empty($entity_id) || ($may_remove_existing && $reference_is_not_required);
The reason the count seems to be off, at least in my case, is because getReferenceableEntities() returns an array of bundles, and inside that the entities, so in the case of the user reference it counts the one bundleโ not the two entities that it should count. (There are many more than two, but the code limits the maximum results to two, because it only cares if it is zero, one, or more than one.)
This is indeed exactly what the getReferenceableEntities method says it will return:
* A nested array of entities, the first level is keyed by the * entity bundle, which contains an array of entity labels (escaped), * keyed by the entity ID.
We can probably save ourselves a step, and fix this bug, by using the countReferenceableEntities() method.
- ๐บ๐ธUnited States mlncn Minneapolis, MN, USA
@TODO offer same patch to IEF