cloudwords-8.x-1.x-dev/src/cloudwords_handler_filter_project_user.php

src/cloudwords_handler_filter_project_user.php
<?php
namespace Drupal\cloudwords;

/**
 * Simple filter to handle equal to / not equal to filters
 *
 * @ingroup views_filter_handlers
 */
class cloudwords_handler_filter_project_user extends views_handler_filter {
  // exposed filter options
  var $always_multiple = TRUE;

  /**
   * Provide simple equality operator
   */
  function operator_options() {
    return [
      '=' => t('The current user'),
      // '!=' => t('Is not equal to'),
    ];
  }

  /**
   * Provide a simple textfield for equality
   */
  function value_form(&$form, &$form_state) {
    $form['value'] = [
      '#type' => 'select',
      '#title' => t('Value'),
      '#size' => 30,
      '#options' => ['yes' => t('Yes'), 'no' => t('No')],
      '#default_value' => $this->value,
      '#multiple' => FALSE,
    ];
  }

  function query() {
    $user = \Drupal::currentUser();
    // This can only work if we're logged in.
    if (!$user || !$user->id()) {
      return;
    }

    // Don't filter if we're exposed and the checkbox isn't selected.
    if ((!empty($this->options['exposed'])) && empty($this->value)) {
      return;
    }

    $uid = $user->id();

    $this->ensure_my_table();
    $field = "$this->table_alias.$this->real_field";
    $translatable = $this->query->ensure_table('cloudwords_translatable', $this->relationship);

    if ($this->value == 'yes') {
      $op = '=';
    }
    if ($this->value == 'no') {
      $op = '!=';
    }

    // NULL means a history record doesn't exist. That's clearly new content.
    // Unless it's very very old content. Everything in the query is already
    // type safe cause none of it is coming from outside here.
    $this->query->add_where_expression($this->options['group'], "$translatable.uid $op $uid");
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc