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.
Examples:
- https://www.abgeordnetenwatch.de/bundestag/19/ausschuesse/1-untersuchungsausschuss-terroranschlag-auf-dem-breitscheidplatz - the sorting is correct
- https://www.abgeordnetenwatch.de/bundestag/19/ausschuesse/1-untersuchungsausschuss-terroranschlag-auf-dem-breitscheidplatz/tabelle#filterbar the committee members are sorted by last name by default (we will change this)... when you click on the last column heading "Rolle im Ausschuss" it should be the same order as the first example. But it is sorted by the key of the role stored in database. "Stellv. Mitglied" has the key "alternate_member" in database that is why these members come first in this table
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() {
$this->ensureMyTable();
$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?!