Currently entity_autocomplete field type is not supported.

Form example:

    $form['multivalue'] = [
      '#type' => 'multivalue',
      '#title' => 'Title',
      '#cardinality' => -1,
      '#default_value' => $default_value,
      'entity' => [
        '#type' => 'entity_autocomplete',
        '#target_type' => 'node',
        '#selection_settings' => [
          'target_bundles' => ['article'],
        ],
        '#validate_reference' => TRUE,
        '#title' => 'Entity',
      ];
    ];

After form submission default value is saved in the form. But entity_autocomplete expects an object there instead of an integer.

The following exception is triggered:

The #default_value property has to be an entity object or an array of entity objects.

See \Drupal\Core\Entity\Element\EntityAutocomplete::valueCallback()

Suggestion: add an additional #process method to the field and load related entities.

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

a.milkovsky created an issue. See original summary.

a.milkovsky’s picture

Status: Active » Needs review
StatusFileSize
new3 KB

Proposed solution.

a.milkovsky’s picture

Issue summary: View changes
a.milkovsky’s picture

Issue summary: View changes
a.milkovsky’s picture

StatusFileSize
new1.81 KB

Moved logic to the MultiValue class.

sinn’s picture

What is a problem to load entities and then pass them into default value?

$form['multivalue'] = [
      '#type' => 'multivalue',
      '#title' => 'Title',
      '#cardinality' => -1,
      '#default_value' => array_values(\Drupal::entityTypeManager()->getStorage('node')->loadMultiple($default_value)),
      'entity' => [
        '#type' => 'entity_autocomplete',
        '#target_type' => 'node',
        '#selection_settings' => [
          'target_bundles' => ['article'],
        ],
        '#validate_reference' => TRUE,
        '#title' => 'Entity',
      ];
    ];
a.milkovsky’s picture

@sinn see my patch #5.
Also in case of multivalued field, there might be muptiple entity_reference sub-fields and the field should recognize all of them.
The `processEntityAutocompleteDefaultValue` method already supports the entityTypeManager entity load by any target entity type (not only "node" as in your example. In addition the method processes a non-integer value, in the entity_autocomplete format.

a.milkovsky’s picture

Somehow the loaded entity gets lost after clicking on the "Add more" button. Trying to find a fix.

Nevermind. The issue was with the patch from #3229995: Items are removed incorrectly with Ajax.

This patch works and is ready to review as before.

a.milkovsky’s picture

StatusFileSize
new1.95 KB

Added on additional case to check for EntityInterface.

a.milkovsky’s picture

StatusFileSize
new1.98 KB

Added an additional check `isset($element['#default_value'])`

darvanen made their first commit to this issue’s fork.

darvanen’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Created an MR to run CI and see if there are any issues. As there is a test suite I'm pretty sure this will need tests to proceed.