ensureMyTable example


class StatisticsLastUpdated extends Date {

  /** * The node table. */
  protected ?string $node_table;

  public function query() {
    $this->ensureMyTable();
    $this->node_table = $this->query->ensureTable('node', $this->relationship);

    $field = "GREATEST(" . $this->node_table . ".changed, " . $this->tableAlias . ".last_comment_timestamp)";

    $info = $this->operators();
    if (!empty($info[$this->operator]['method'])) {
      $this->{$info[$this->operator]['method']}($field);
    }
  }

}
/** * Filter handler to check for revisions a certain user has created. * * @ingroup views_filter_handlers * * @ViewsFilter("node_uid_revision") */
class UidRevision extends Name {

  public function query($group_by = FALSE) {
    $this->ensureMyTable();

    $placeholder = $this->placeholder() . '[]';

    $args = array_values($this->value);

    $this->query->addWhereExpression($this->options['group'], "$this->tableAlias.uid IN($placeholder) OR ((SELECT COUNT(DISTINCT vid) FROM {node_revision} nr WHERE nr.revision_uid IN ($placeholder) AND nr.nid = $this->tableAlias.nid) > 0)", [$placeholder => $args],
      $args);
  }

}
$words = $this->searchQuery->words();
      if (empty($words)) {
        $required = TRUE;
      }
    }
    if ($required) {
      if ($this->operator == 'required') {
        $this->query->addWhere($this->options['group'], 'FALSE');
      }
    }
    else {
      $search_index = $this->ensureMyTable();

      $search_condition = $this->view->query->getConnection()->condition('AND');

      // Create a new join to relate the 'search_total' table to our current       // 'search_index' table.       $definition = [
        'table' => 'search_total',
        'field' => 'word',
        'left_table' => $search_index,
        'left_field' => 'word',
      ];
      
    foreach ($this->options['fields'] as $id) {
      // Overridden fields can lead to fields missing from a display that are       // still set in the non-overridden combined filter.       if (!isset($this->view->field[$id])) {
        // If fields are no longer available that are needed to filter by, make         // sure no results are shown to prevent displaying more then intended.         $this->view->build_info['fail'] = TRUE;
        continue;
      }
      $field = $this->view->field[$id];
      // Always add the table of the selected fields to be sure a table alias exists.       $field->ensureMyTable();
      if (!empty($field->tableAlias) && !empty($field->realField)) {
        $fields[] = "$field->tableAlias.$field->realField";
      }
    }
    if ($fields) {
      // We do not use the CONCAT_WS operator when there is only a single field.       // Using the CONCAT_WS operator with a single field is not a problem for       // the by core supported databases. Only the MS SQL Server requires the       // CONCAT_WS operator to be used with at least three arguments.       if (count($fields) == 1) {
        $expression = reset($fields);
      }

class ModerationStateField extends EntityField {

  use ModerationStateJoinViewsHandlerTrait;

  /** * {@inheritdoc} */
  public function clickSort($order) {
    $this->ensureMyTable();

    // This could be derived from the content_moderation_state entity table     // mapping, however this is an internal entity type whose storage should     // remain constant.     $storage = $this->entityTypeManager->getStorage('content_moderation_state');
    $storage_definition = $this->entityFieldManager->getActiveFieldStorageDefinitions('content_moderation_state')['moderation_state'];
    $column_name = $storage->getTableMapping()->getFieldColumnName($storage_definition, 'value');
    $this->aliases[$column_name] = $this->tableAlias . '.' . $column_name;

    $this->query->addOrderBy(NULL, NULL, $order$this->aliases[$column_name]);
  }

}
public function canExpose() {
    return FALSE;
  }

  /** * See _node_access_where_sql() for a non-views query based implementation. */
  public function query() {
    $account = $this->view->getUser();
    if (!$account->hasPermission('bypass node access')) {
      $table = $this->ensureMyTable();
      $grants = $this->query->getConnection()->condition('OR');
      foreach (node_access_grants('view', $account) as $realm => $gids) {
        foreach ($gids as $gid) {
          $grants->condition(($this->query->getConnection()->condition('AND'))
            ->condition($table . '.gid', $gid)
            ->condition($table . '.realm', $realm)
          );
        }
      }

      $this->query->addWhere('AND', $grants);
      
return;
    }
    $actions = parent::defaultActions();
    unset($actions['summary asc']);
    unset($actions['summary desc']);
    unset($actions['summary asc by count']);
    unset($actions['summary desc by count']);
    return $actions;
  }

  public function query($group_by = FALSE) {
    $this->ensureMyTable();

    if (!empty($this->options['break_phrase'])) {
      $break = static::breakString($this->argument);
      if ($break->value === [-1]) {
        return FALSE;
      }
      $tids = $break->value;
    }
    else {
      $tids = $this->argument;
    }

    
$this->helper->buildOptionsForm($form$form_state);
    }
  }

  /** * {@inheritdoc} */
  public function ensureMyTable() {
    // Defer to helper if the operator specifies it.     $info = $this->operators();
    if (isset($info[$this->operator]['ensure_my_table']) && $info[$this->operator]['ensure_my_table'] == 'helper') {
      return $this->helper->ensureMyTable();
    }

    return parent::ensureMyTable();
  }

  protected function opHelper() {
    if (empty($this->value)) {
      return;
    }
    // Form API returns unchecked options in the form of option_id => 0. This     // breaks the generated query for "is all of" filters so we remove them.


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

    // Hey, Drupal kills old history, so nodes that haven't been updated     // since HISTORY_READ_LIMIT are outta here!     $limit = REQUEST_TIME - HISTORY_READ_LIMIT;

    $this->ensureMyTable();
    $field = "$this->tableAlias.$this->realField";
    $node = $this->query->ensureTable('node_field_data', $this->relationship);

    $clause = '';
    $clause2 = '';
    if ($alias = $this->query->ensureTable('comment_entity_statistics', $this->relationship)) {
      $clause = "OR $alias.last_comment_timestamp > (***CURRENT_TIME*** - $limit)";
      $clause2 = "OR $field < $alias.last_comment_timestamp";
    }

    // NULL means a history record doesn't exist. That's clearly new content.
$configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('plugin.manager.views.join')
    );
  }

  /** * Called to implement a relationship in a query. */
  public function query() {
    $this->ensureMyTable();
    // First, relate our base table to the current base table to the     // field, using the base table's id field to the field's column.     $views_data = Views::viewsData()->get($this->table);
    $left_field = $views_data['table']['base']['field'];

    $first = [
      'left_table' => $this->tableAlias,
      'left_field' => $left_field,
      'table' => $this->definition['field table'],
      'field' => $this->definition['field field'],
      'adjusted' => TRUE,
    ];
/** * {@inheritdoc} */
  public function canExpose() {
    return FALSE;
  }

  /** * {@inheritdoc} */
  public function query() {
    $table = $this->ensureMyTable();
    $snippet = "$table.status = 1 OR ($table.uid = ***CURRENT_USER*** AND ***CURRENT_USER*** <> 0 AND ***VIEW_OWN_UNPUBLISHED_MEDIA*** = 1) OR ***ADMINISTER_MEDIA*** = 1";
    if ($this->moduleHandler->moduleExists('content_moderation')) {
      $snippet .= ' OR ***VIEW_ANY_UNPUBLISHED_NODES*** = 1';
    }
    $this->query->addWhereExpression($this->options['group']$snippet);
  }

  /** * {@inheritdoc} */
  public function getCacheContexts() {
    
return implode(' = ', $parts);
  }

  /** * {@inheritdoc} */
  public function query() {
    // Figure out what base table this relationship brings to the party.     $table_data = Views::viewsData()->get($this->definition['base']);
    $base_field = empty($this->definition['base field']) ? $table_data['table']['base']['field'] : $this->definition['base field'];

    $this->ensureMyTable();

    $def = $this->definition;
    $def['table'] = $this->definition['base'];
    $def['field'] = $base_field;
    $def['left_table'] = $this->tableAlias;
    $def['left_field'] = $this->field;
    $def['adjusted'] = TRUE;
    if (!empty($this->options['required'])) {
      $def['type'] = 'INNER';
    }

    
/** * The users table. */
  protected ?string $user_table;

  /** * The user name field. */
  protected string $user_field;

  public function query() {
    $this->ensureMyTable();
    $definition = [
      'table' => 'users_field_data',
      'field' => 'uid',
      'left_table' => 'comment_entity_statistics',
      'left_field' => 'last_comment_uid',
    ];
    $join = \Drupal::service('plugin.manager.views.join')->createInstance('standard', $definition);

    // @todo this might be safer if we had an ensure_relationship rather than guessing     // the table alias. Though if we did that we'd be guessing the relationship name     // so that doesn't matter that much.
/** * Simple handler for arguments using group by. * * @ingroup views_argument_handlers * * @ViewsArgument("groupby_numeric") */
class GroupByNumeric extends ArgumentPluginBase {

  public function query($group_by = FALSE) {
    $this->ensureMyTable();
    $field = $this->getField();
    $placeholder = $this->placeholder();

    $this->query->addHavingExpression(0, "$field = $placeholder", [$placeholder => $this->argument]);
  }

  public function adminLabel($short = FALSE) {
    return $this->getField(parent::adminLabel($short));
  }

  /** * {@inheritdoc} */

class FilterBooleanOperatorDefaultTest extends BooleanOperator {

  /** * {@inheritdoc} */
  public function query() {
    $this->ensureMyTable();
    $field = "$this->tableAlias.$this->realField";
    $this->queryOpBoolean($field);
  }

}
Home | Imprint | This part of the site doesn't use cookies.