Problem/Motivation

Version 7.1.6 work correctly. After update to 1.7 or 1.8 order by title in views Entity Reference not appears in select reference field that use this view.

Steps to reproduce

Create two contents A and B. Create Entity Reference in views for content B with ordering by Title asc. Add to field reference (select) to content A and select views reference. Create content B with titles : 2,1,xys,0. Start create content A. Order in select box: 2,1,xys,0.

Restore 7.1.6 order: 0,1,2,xyz

Thank you for help

Comments

psaleks created an issue. See original summary.

psaleks’s picture

Title: Field refference did not use order (asc,desc) that done in views refference » Field refference d0 not use order (asc,desc) that done in views refference
psaleks’s picture

Title: Field refference d0 not use order (asc,desc) that done in views refference » Field refference do not use order (asc,desc) that done in views refference
psaleks’s picture

Problem with incorrect optimization in EntityReference_SelectionHandler_Views.class.php. v7.x-1.8
line 142

      $target_type = $this->field['settings']['target_type'];

      // Do not load the entire entity for performance reasons.
      $entity_info = entity_get_info($target_type);
      $query = db_select($entity_info['base table'], 'e');
      $query->fields('e');
      $query->condition('e.' . $entity_info['entity keys']['id'], array_keys($result), 'IN');

      foreach ($query->execute() as $entity) {
        // When the bundle key is not on the 'base table', get it on the $entity
        // through entity_load().
        if (!empty($entity_info['entity keys']['bundle']) && empty($entity->{$entity_info['entity keys']['bundle']})) {
          $loaded_entities = entity_load($target_type, array($entity->{$entity_info['entity keys']['id']}));
          if (empty($loaded_entities)) {
            continue;
          }
          $entity = reset($loaded_entities);
        }
        list($id,, $bundle) = entity_extract_ids($target_type, $entity);
        $return[$bundle][$id] = $result[$id];
      }

To fix bug replace by code from v7.x-1.6

      $target_type = $this->field['settings']['target_type'];
      $entities = entity_load($target_type, array_keys($result));
      foreach($entities as $entity) {
        list($id,, $bundle) = entity_extract_ids($target_type, $entity);
        $return[$bundle][$id] = $result[$id];
      }
danyg’s picture

Status: Active » Needs review
StatusFileSize
new657 bytes

Here is the patch which fixes this issue.

danyg’s picture

Ok, here is an improved patch for this issue. It now respects if there is more than one bundle are being used in the reference field (like listing all pages and stories)

psaleks’s picture

Patch from #6 works for me

danyg’s picture

@psaleks Can you set the status to RTBC (Reviewed & tested by the community) if it looks good to you?

leducdubleuet’s picture

Title: Field refference do not use order (asc,desc) that done in views refference » Field reference do not use order (asc,desc) that done in views reference
eckersley’s picture

I came here because I had the same problem. Patch #6 fixed it for me too. Thanks!

salag’s picture

I tried the patch in #6 and got this error:

ParseError: syntax error, unexpected 'if' (T_IF) in _registry_check_code() (line 155 of [website]/sites/all/modules/entityreference/plugins/selection/EntityReference_SelectionHandler_Views.class.php).

psaleks’s picture

Status: Needs review » Reviewed & tested by the community
danyg’s picture

@SaIAG it's very strange, it looks that applying the patch wasn't successful for you and it caused a parse error.
The error comes from the same file but from an earlier point. Have you already applied a different patch on this module?

joseph.olstad’s picture

has anyone tested this patch with PHP 8.1 or PHP 8.2 ?

the count function is more strict since 8.1 and 8.2, just wondering if there's a scenario where
$return[$bundle]

is not countable by PHP 8.1/ PHP 8.2 and the deprecated notice would show up?

psaleks’s picture

Patch works for php 8.1.12.

salag’s picture

@danyg #13 no, no other patches applied. I tried it once, it failed and I reverted to default module file.
Told the content guy to use intelli-sense on the name, which helps a little. Looking forward to the update.

We're still on php 7.4.33. Don't know if that has anything to do with it.

joseph.olstad’s picture

@SalAG, sounds like your patching tool might not be working correctly, could you please try modifying the module file manually and please test the changes with PHP 7.4 and then report the results to us?

joseph.olstad’s picture

  • joseph.olstad committed f73a617e on 7.x-1.x authored by danyg
    Issue #3349989 by danyg, joseph.olstad, psaleks, LeDucDuBleuet,...
joseph.olstad’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.