Issue description

The documentation in Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface::getReferenceableEntities() doesn't specify the expected first level key when used on an entity type that does not contain bundles:

  /**
   * Gets the list of referenceable entities.
   *
   * @return array
   *   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.
   */
  public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0);

The expected first level key when no bundles are available is the entity type ID; example: 'role'.

Without that first level entity type id key, any many other functions using this method will throw PHP notices etc; example: core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem::getSettableOptions().

If this had been documented, I would have known that my implementation of getReferenceableEntities() for a custom entity type was incorrect rather than assuming getSettableOptions() was incorrect and writing a patch etc. Adding a note will likely save others time debugging the PHP notices from not returning the expected value in getReferenceableEntities().

Proposed text for @return documentation

A nested array of entities, the first level is keyed by the entity bundle (or the entity type ID if no bundles exist), which contains an array of entity labels (escaped), keyed by the entity ID.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jludwig created an issue. See original summary.

jludwig’s picture

Component: entity_reference.module » field system
Priority: Normal » Minor
FileSize
1.33 KB

Seems like this is not under the field system, so changing the component.

Also attaching a patch.

jludwig’s picture

Issue summary: View changes
Status: Active » Needs review

Changed to needs review

Status: Needs review » Needs work

The last submitted patch, 2: entity_reference_item_undefined_index-2863911-1.patch, failed testing.

jludwig’s picture

Woops, I deleted the line defining $target_type while making the patch against 8.4.x.

The attached patch actually works.

dawehner’s picture

Issue tags: +Needs tests

@jludwig
Do you mind trying to write some test for that? I could imagining looking into \Drupal\KernelTests\Core\Entity\EntityReferenceFieldTest would help here.

jludwig’s picture

Status: Needs review » Active
Issue tags: +Documentation

Upon further investigation, it looks like core handles this correctly.

If the entity type returns no bundles, getReferenceableEntities() should return the entity type as the top level key:

 /**
   * Gets the list of referenceable entities.
   *
   * @return array
   *   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.
   */
  public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0);

It doesn't say it specifically here, but that is how Drupal actually handles it.

It was the module I was working with that returned a flat array with getReferenceableEntities that was the problem.

Perhaps this should be turned into a documentation issue instead, with the @return documentation in the interface specifying that if the entity type has no bundles, the entity type id should be the first level key?

jludwig’s picture

Component: field system » documentation
Category: Bug report » Task
Issue tags: -PHP warnings (duplicate tag), -Needs tests

Does anyone else agree that this needs some more documentation?

Something as simple as

A nested array of entities, the first level is keyed by the entity bundle (or the entity type ID if no bundles exist), which contains an array of entity labels (escaped), keyed by the entity ID.

jludwig’s picture

Title: Undefined index in EntityReferenceItem::getSettableOptions() on entity references to entities without bundles » Document the return value expected by SelectionInterface::getReferenceableEntities() when used on an entity type with no bundles.
Issue summary: View changes
prash_98’s picture

So the documentation needs to be done for the file which has the below mentioned section ryt?

/**
* Gets the list of referenceable entities.
*
* @return array
* 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.
*/
public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0);

Or did I get it wrong? :)

prash_98’s picture

Any updates with these with this issue?

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

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.

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.