You are here

public function ProfileEntitySortable::clickSort in Open Social 10.2.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_profile/src/Plugin/views/field/ProfileEntitySortable.php \Drupal\social_profile\Plugin\views\field\ProfileEntitySortable::clickSort()
  2. 10.3.x modules/social_features/social_profile/src/Plugin/views/field/ProfileEntitySortable.php \Drupal\social_profile\Plugin\views\field\ProfileEntitySortable::clickSort()
  3. 10.0.x modules/social_features/social_profile/src/Plugin/views/field/ProfileEntitySortable.php \Drupal\social_profile\Plugin\views\field\ProfileEntitySortable::clickSort()
  4. 10.1.x modules/social_features/social_profile/src/Plugin/views/field/ProfileEntitySortable.php \Drupal\social_profile\Plugin\views\field\ProfileEntitySortable::clickSort()

Adds an ORDER BY clause to the query for click sort columns.

Parameters

string $order: Either ASC or DESC

Overrides FieldPluginBase::clickSort

File

modules/social_features/social_profile/src/Plugin/views/field/ProfileEntitySortable.php, line 98

Class

ProfileEntitySortable
Field handler to sort rendered profile entity in views.

Namespace

Drupal\social_profile\Plugin\views\field

Code

public function clickSort($order) {
  if (isset($this->field_alias)) {

    // If we want to sort on the profile name, add the correct alias.
    if ($this->table === 'profile' && $this->field === 'profile_entity_sortable') {

      /** @var \Drupal\views\Plugin\views\query\Sql $query */
      $query = $this->query;

      // Get a field list that will be used for sorting.
      $order_by_fields = $this
        ->orderByFields();

      // Add relationship for necessary fields.
      foreach ($order_by_fields as $sort_field) {
        $definition = [
          'table' => 'profile__' . $sort_field,
          'field' => 'entity_id',
          'left_table' => $this->relationship,
          'left_field' => 'profile_id',
        ];
        $join = $this->joinManager
          ->createInstance('standard', $definition);
        $query
          ->addRelationship($definition['table'], $join, $this->relationship);
      }

      // If we have more than one field for sort then use Firstname, Lastname,
      // and Nickname fields.
      if (count($order_by_fields) > 1) {
        $this->field_alias = 'profile_full_name';

        // We will have different expressions depending on is the Nickname
        // field provided or not.
        // Members will be sort by next queue:
        // - Nickname
        // - Firstname + Lastname (if Nickname is NULL)
        // - Firstname (if Nickname and Lastname are NULL)
        // - Lastname (if Nickname and Firstname are NULL)
        // - Username (if all Name fields are NULL)
        $field = in_array('field_profile_nick_name', $order_by_fields) ? "CASE WHEN\n              profile__field_profile_nick_name.field_profile_nick_name_value IS NOT NULL\n            THEN\n              TRIM(profile__field_profile_nick_name.field_profile_nick_name_value)\n            WHEN\n              (profile__field_profile_nick_name.field_profile_nick_name_value IS NULL) AND ((profile__field_profile_first_name.field_profile_first_name_value IS NOT NULL) OR (profile__field_profile_last_name.field_profile_last_name_value IS NOT NULL))\n            THEN\n              CONCAT(TRIM(COALESCE(profile__field_profile_first_name.field_profile_first_name_value, '')), ' ', TRIM(COALESCE(profile__field_profile_last_name.field_profile_last_name_value, '')))\n            ELSE\n              TRIM(" . $this->view->relationship['profile']->tableAlias . ".name)\n            END" : "CASE WHEN\n              ((profile__field_profile_first_name.field_profile_first_name_value IS NOT NULL) OR (profile__field_profile_last_name.field_profile_last_name_value IS NOT NULL))\n            THEN\n              CONCAT(TRIM(COALESCE(profile__field_profile_first_name.field_profile_first_name_value, '')), ' ', TRIM(COALESCE(profile__field_profile_last_name.field_profile_last_name_value, '')))\n            ELSE\n              TRIM(" . $this->view->relationship['profile']->tableAlias . ".name)\n            END";
        $query
          ->addField(NULL, $field, $this->field_alias);
      }
      elseif (count($order_by_fields) === 1) {
        $this->field_alias = $definition['table'] . '.profile_name_value';
      }
    }

    // Since fields should always have themselves already added, just
    // add a sort on the field.
    $params = $this->options['group_type'] != 'group' ? [
      'function' => $this->options['group_type'],
    ] : [];
    $this->query
      ->addOrderBy(NULL, NULL, $order, $this->field_alias, $params);
  }
}