#StackBounty: #8 #views Custom view sort plugin not working only when used in table sorting

Bounty: 50

We have a view of committee memberships (= custom entity). The committee memberships hold references to the committee and the politician and have a field "role". In a view we want to output all committee memberships in a logical order: chairperson first, the vice chairperson, foreperson, spokesperson and member. As you can see alphabetical order is not suitable.

We defined a custom view sort plugin which is playing nice on simple views where the sorting is set in the views configuration. However on a table view when selecting the column for the sorting by role it is not called.


We defined the plugin in the following way in a class extending EntityViewsData:

$data['committee_membership']['committee_role']['sort']['id'] = 'sort_committee_membership';

The plugin is that:

 * Sort handler for sorting committee memberships by list key position.
 * @ingroup views_sort_handlers
 * @ViewsSort("sort_committee_membership")
class SortCommitteeMembership extends SortPluginBase {

  public function query() {
    $field_storage = Drupal::service('entity_field.manager')->getFieldStorageDefinitions('committee_membership')[$this->field];
    $allowed_values = array_keys(options_allowed_values($field_storage));
    $formula = 'FIELD(' . $this->tableAlias . '.' . $this->field . ', ' . implode(', ', array_map(array(Database::getConnection(), 'quote'), $allowed_values)) . ')';

    $this->query->addOrderBy(NULL, $formula, $this->options['order'], $this->tableAlias . '_' . $this->field . '_keys');


Is there something missing which is needed to implement when the sorting is exposed?!

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.