How do the sorting by name/weight for the list?

Comments

donnadion’s picture

Category: feature » bug

Great module! But I would call this a bug report.

Using the default select list widget, I choose sort by base property: title to sort by name. Without the option limit, the sort works as expected. With the option limit, the sort does not work, the default (no sort selected) order is displayed.

Thank you!

juto’s picture

reference_option_limit.module

-   // $query->fieldOrderBy('name', 'value', 'ASC') @todo! add some sort of ordering.
+   $query->propertyOrderBy('name/weight', 'ASC');
joachim’s picture

You'd need to pick out the ordering that's been set in the field settings. Which depends on the type of field:

- taxonomy ref: term weight
- entityref: field settings

blauerberg’s picture

Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new1.19 KB

> - taxonomy ref: term weight
> - entityref: field settings

It seems not working and donnadion's report reproduced in my environment.
Please try this patch.

joachim’s picture

Status: Needs review » Needs work

Thanks for the patch! Looks good, just a few tweaks needed:

  1. +++ b/reference_option_limit.module
    @@ -389,8 +389,24 @@ function reference_option_limit_form_alter(&$form, &$form_state, $form_id) {
    +      } else {
    +        $query->propertyOrderBy('weight', 'ASC');
    +      }
    

    Could this check the field type is a term ref here? (In case we later add other field types to this module!)

    (Also, remember not to coddle the else/elseif.)

  2. +++ b/reference_option_limit.module
    @@ -389,8 +389,24 @@ function reference_option_limit_form_alter(&$form, &$form_state, $form_id) {
    +            $keys = explode(':', $sort_settings['field']);
    

    Entityref does this:

    list($field, $column) = explode(':', $sort_settings['field'], 2);
    $query->fieldOrderBy($field, $column, $sort_settings['direction']);

    which I think would be easier to read.

blauerberg’s picture

Status: Needs work » Needs review
StatusFileSize
new1.41 KB

Thanks for review, fixed by #5.

blauerberg’s picture

StatusFileSize
new1.35 KB

oops.. #6 is mistake..

joachim’s picture

Status: Needs review » Needs work

Patch isn't applying to 7.x-1.x I'm afraid!

blauerberg’s picture

Status: Needs work » Needs review
StatusFileSize
new1.3 KB

for 7.x-1.x.

joachim’s picture

Status: Needs review » Fixed

Thanks for working on this :)

git commit -m "Issue #2043665 by blauerberg: Fixed filtered entities not sorted." --author="blauerberg "

Status: Fixed » Closed (fixed)

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

dariogcode’s picture

A little note about this. For huge taxonomy terms, weight may have the same value, then term reference will show unsorted. Then sort by name is what we need.

For those interested, I'm using this code in a custom module:

function coche_post_entity_query_alter($query) {
  if (isset($query->entityConditions['entity_type']['value']) && $query->entityConditions['entity_type']['value'] == 'taxonomy_term' && isset($query->tags['reference_option_limit'])) {
    $query->propertyOrderBy('name', 'ASC');
  }
}
johnpitcairn’s picture

@tilon: I think you mean if there are child taxonomy terms the query result may have duplicate weights.

Here's what I'm using to replace the order by weight if it is the only order in the query:

/**
 * Implements hook_query_TAG_alter().
 */
function MYMODULE_query_reference_option_limit_alter($query) {
  $order = &$query->getOrderBy();
  if (count($order) == 1 && isset($order['taxonomy_term_data.weight'])) {
    $order = array('taxonomy_term_data.name' => 'ASC');
  }
}