Field reference do not use order (asc,desc) that done in views reference

Created on 23 March 2023, over 1 year ago
Updated 28 April 2023, over 1 year ago

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

🐛 Bug report
Status

Fixed

Version

1.8

Component

Code

Created by

🇨🇦Canada psaleks

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @psaleks
  • 🇨🇦Canada psaleks

    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];
          }
    
  • Status changed to Needs review over 1 year ago
  • 🇭🇺Hungary danyg Budapest

    Here is the patch which fixes this issue.

  • 🇭🇺Hungary danyg Budapest

    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)

  • 🇨🇦Canada psaleks

    Patch from #6 works for me

  • 🇭🇺Hungary danyg Budapest

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

  • 🇨🇦Canada leducdubleuet Chicoutimi QC
  • 🇬🇧United Kingdom eckersley

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

  • 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).

  • Status changed to RTBC over 1 year ago
  • 🇭🇺Hungary danyg Budapest

    @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?

  • 🇨🇦Canada joseph.olstad

    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?

  • 🇨🇦Canada psaleks

    Patch works for php 8.1.12.

  • @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.

  • 🇨🇦Canada joseph.olstad

    @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?

  • Status changed to Fixed over 1 year ago
  • Status changed to Fixed over 1 year ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024