public function GroupwiseMax::query

Called to implement a relationship in a query. This is mostly a copy of our parent's query() except for this bit with the join class.

Overrides RelationshipPluginBase::query

File

drupal/core/modules/views/lib/Drupal/views/Plugin/views/relationship/GroupwiseMax.php, line 342
Definition of Drupal\views\Plugin\views\relationship\GroupwiseMax.

Class

GroupwiseMax
Relationship handler that allows a groupwise maximum of the linked in table. For a definition, see: http://dev.mysql.com/doc/refman/5.0/en/example-maximum-column-group-row.... In lay terms, instead of joining to get all matching records in the…

Namespace

Drupal\views\Plugin\views\relationship

Code

public function query() {

  // Figure out what base table this relationship brings to the party.
  $table_data = views_fetch_data($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';
  }
  if ($this->options['subquery_regenerate']) {

    // For testing only, regenerate the subquery each time.
    $def['left_query'] = $this
      ->left_query($this->options);
  }
  else {

    // Get the stored subquery SQL string.
    $cid = 'views_relationship_groupwise_max:' . $this->view->storage
      ->get('name') . ':' . $this->view->current_display . ':' . $this->options['id'];
    $cache = cache('views_results')
      ->get($cid);
    if (isset($cache->data)) {
      $def['left_query'] = $cache->data;
    }
    else {
      $def['left_query'] = $this
        ->left_query($this->options);
      cache('views_results')
        ->set($cid, $def['left_query']);
    }
  }
  if (!empty($def['join_id'])) {
    $id = $def['join_id'];
  }
  else {
    $id = 'subquery';
  }
  $join = drupal_container()
    ->get('plugin.manager.views.join')
    ->createInstance($id, $def);

  // use a short alias for this:
  $alias = $def['table'] . '_' . $this->table;
  $this->alias = $this->query
    ->add_relationship($alias, $join, $this->definition['base'], $this->relationship);
}