If I create an entity reference field which values are attained from a view, and if I cache the view output I get a faulty EntityReference_SelectionHandler_Views::validateReferencableEntities() response. This is easily reproducable by adding the same entity reference with views based values twice to a single content type. Because the output is cached the selectable entities in the content form are correctly (and quickly) selectable. But if I save the content item form, the referenced values get checked and only the first is marked correctly.

On node save Drupal reports:

The referenced entity (node: XXX) is invalid

It's weird that the same view that produces the valid_ids in the form selection, doesn't validate when being checked during save. The methods getReferencableEntities() and validateReferencableEntities() should produce identical results.

The proper behaviour of the entity reference field should be on of the following 3 options:

  1. Caching should be disabled on the entity reference displays of views. This could be an effective workaround at the cost of performance.
  2. The validation should be checked against an uncached view result. This is my preferred option.
  3. The validation should be disabled completely on entity references with a view result as source. I reckon extra validation is necessary for language checks or node access checks. Hence this is less desirable.

Related issues: #1332262: The referenced entity is invalid (for translations)

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

basvredeling’s picture

For now, I've changed the method to check for valid ids after the query result has been fetched. Passing it as an array to the intializeView() method doesn't seem to work. However, I'm not sure what impact this has on caching. But caching should be disabled by initializeView() when it sets $this->view->is_cacheable to false

  function validateReferencableEntities(array $ids) {
    $display_name = $this->field['settings']['handler_settings']['view']['display_name'];
    $args = $this->field['settings']['handler_settings']['view']['args'];
    $valid_ids = array();
    if ($this->initializeView(NULL, 'CONTAINS', 0)) {
      // Get the results.
      $entities = $this->view->execute_display($display_name, $args);
      $result = array_keys($entities);
      $valid_ids = array_intersect($result, $ids);
    }
    return $valid_ids;
basvredeling’s picture

Issue summary: View changes

specified relevant methods

Chipie’s picture

Where did you change the method?

basvredeling’s picture

I've altered DrupalRoot/sites/all/modules/contrib/entityreference/plugins/selection/EntityReference_SelectionHandler_Views.class.php around line 124

Chipie’s picture

Thanks. It work now.

basvredeling’s picture

Status: Active » Needs review
FileSize
2.33 KB

I've added a patch also incorporating the patch in comment #1 from this issue: #1819618: Incorrect autocomplete validation with views selection mode

passengerabcd’s picture

Version: 7.x-1.x-dev » 7.x-1.0

The patch worked for entity reference views of nodes, but not for users. I have a field using entity reference views to query certain users, the validation won't work and the field is not set even it's a required field. Would you have a look into it? And an update patch is much appreciated.

basvredeling’s picture

Version: 7.x-1.0 » 7.x-1.x-dev
Status: Needs review » Needs work

Patch works against 7.x-1.x-dev... not 1.0
@passengerabcd Could you provide a description to reproduce your issue?

passengerabcd’s picture

@basvredeling, I have done more testings on this, and it turned out the validation thing went wrong under such circumstances:

1. Make a entity reference view of users (search on the User:Name field for the auto-complete widget);
2. Use this view as a field of a content type (target type is Users and select the view from step 1);
3. Mark this field as required.

Now, when trying to add a node of this very content type and filling out the field with some wrong usernames, we have problems:
1. Let's say we have usernames as user1, user2 and user3;
2. When filling the field with user4, the validation worked;
3. When filling the field with use or user, the validation didn't work, and the node is saved with an empty field value even the filed is required;

It looks like the the validation just check the input is contained in the view output, not an exact match. Or the validation is somehow not working well with the auto-complete widget.

Hope I explained the situation clear enough, and I just found out the entity reference views of nodes have exactly the same problem.

drasgardian’s picture

Status: Needs work » Needs review
FileSize
2.52 KB

Looks like that last patch wasn't taking into account that getReferencableEntities() returns an array keyed by bundle rather than entity_id.

Revised patch attached.

basvredeling’s picture

Status: Needs review » Reviewed & tested by the community

Works fine, thanks for the addition.
If this is committed #1819618: Incorrect autocomplete validation with views selection mode can be closed too.

passengerabcd’s picture

#9 patch works for me. Thanks for the patch.

However, I happened to find another bug for the validation error. NOT with views but with simple mode (with optional filter by bundle).

For example, I have a bundle with valid record of "item1 (16)", the validation will fail if I just input "item1" or "item1 " (it has a space in the end), as a result null value is submitted and saved regardless the field is required.

Maybe it's better to open a new issue for this, but I think the bug behavior is similar to #8, so maybe someone can find valuable input from patch in #9.

passengerabcd’s picture

#9 patch works for me. Thanks for the patch.

However, I happened to find another bug for the validation error. NOT with views but with simple mode (with optional filter by bundle).

For example, I have a bundle with valid record of "item1 (16)", the validation will fail if I just input "item1" or "item1 " (it has a space in the end), as a result null value is submitted and saved regardless the field is required.

Maybe it's better to open a new issue for this, but I think the bug behavior is similar to #8, so maybe someone can find valuable input from patch in #9.

basvredeling’s picture

Seems like a different but similar issue. But we could solve both and rename this issue. The patch is not commited anyway.

dajjen’s picture

I have trouble getting #9 to work. It seems like validateAutocompleteInput returns the bundle instead of the id.

I changed from

 return key($entities[]);

to

$array_key = key($entities);

      if (is_array($entities[$array_key]) && $array_key != NULL) {
        $first_entity_multi_array = $entities[$array_key];
        $first_entity_key = key($first_entity_multi_array);
        return $first_entity_key;
      }

      else {
        return key($entities[]);
      }
dajjen’s picture

Here is a patch with my changes. from #14

Damien Tournoud’s picture

Status: Reviewed & tested by the community » Needs work

Needs work. This fetches the whole result set of the views to intersect it with the values being checked. This is just not acceptable.

Feels like a Views bug or a user error. Is it possible in Views to cache the View differently depending on its arguments?

Damien Tournoud’s picture

Issue summary: View changes

changed formulation