You are here

public function OpignoMessageThreadNameFilter::query in Opigno messaging 3.x

Add this filter to the query.

Due to the nature of fapi, the value and the operator have an unintended level of indirection. You will find them in $this->operator and $this->value respectively.

Overrides StringFilter::query

File

src/Plugin/views/filter/OpignoMessageThreadNameFilter.php, line 61

Class

OpignoMessageThreadNameFilter
Filter view handler for the Opigno message thread name.

Namespace

Drupal\opigno_messaging\Plugin\views\filter

Code

public function query() {
  if (!$this->query instanceof Sql) {
    return;
  }

  // Prepare the query.
  $this
    ->ensureMyTable();

  // Join the table to get all thread members, exclude the current one.
  $members_join_definition = [
    'left_table' => $this->tableAlias,
    'left_field' => $this->realField,
    'table' => 'private_message_thread__members',
    'field' => 'entity_id',
    'extra' => [
      [
        'field' => 'members_target_id',
        'value' => $this->currentUid,
        'operator' => '!=',
      ],
    ],
  ];
  $members_join = $this->joinManager
    ->createInstance('standard', $members_join_definition);
  $members_alias = $this->query
    ->addTable('private_message_thread__members', NULL, $members_join);

  // Join the table to get the user first name.
  $first_name_join_definition = [
    'left_table' => $members_alias,
    'left_field' => 'members_target_id',
    'table' => 'user__field_first_name',
    'field' => 'entity_id',
  ];
  $first_name_join = $this->joinManager
    ->createInstance('standard', $first_name_join_definition);
  $first_name_alias = $this->query
    ->addTable('user__field_first_name', NULL, $first_name_join);

  // Join the table to get the user last name.
  $last_name_join_definition = [
    'left_table' => $members_alias,
    'left_field' => 'members_target_id',
    'table' => 'user__field_last_name',
    'field' => 'entity_id',
  ];
  $last_name_join = $this->joinManager
    ->createInstance('standard', $last_name_join_definition);
  $last_name_alias = $this->query
    ->addTable('user__field_last_name', NULL, $last_name_join);

  // Join the table to get the machine user name.
  $username_join_definition = [
    'left_table' => $members_alias,
    'left_field' => 'members_target_id',
    'table' => 'users_field_data',
    'field' => 'uid',
  ];
  $username_join = $this->joinManager
    ->createInstance('standard', $username_join_definition);
  $username_alias = $this->query
    ->addTable('users_field_data', NULL, $username_join);

  // Join the table to get the discussion subject.
  $subject_join_definition = [
    'left_table' => $this->tableAlias,
    'left_field' => $this->realField,
    'table' => 'private_message_thread__field_pm_subject',
    'field' => 'entity_id',
  ];
  $subject_join = $this->joinManager
    ->createInstance('standard', $subject_join_definition);
  $subject_alias = $this->query
    ->addTable('private_message_thread__field_pm_subject', NULL, $subject_join);

  // Check if the given text is a part of the user first/last names, default
  // username of the discussion subject.
  $condition = new Condition('OR');
  $val = $this->connection
    ->escapeLike($this->value);
  $condition
    ->condition("{$first_name_alias}.field_first_name_value", "%{$val}%", 'LIKE');
  $condition
    ->condition("{$last_name_alias}.field_last_name_value", "%{$val}%", 'LIKE');
  $condition
    ->condition("{$username_alias}.name", "%{$val}%", 'LIKE');
  $condition
    ->condition("{$subject_alias}.field_pm_subject_value", "%{$val}%", 'LIKE');
  $this->query
    ->addWhere($this->options['group'], $condition);
}