You are here

public function RelationRelationship::query in Relation 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/views/relationship/RelationRelationship.php \Drupal\relation\Plugin\views\relationship\RelationRelationship::query()

Add anything to the query that we might need to.

Overrides RelationshipPluginBase::query

File

src/Plugin/views/relationship/RelationRelationship.php, line 74
Views relationship support.

Class

RelationRelationship
Relate entities using a Relation endpoint.

Namespace

Drupal\relation\Plugin\views\relationship

Code

public function query() {
  $table_mapping = \Drupal::entityTypeManager()
    ->getStorage('relation')
    ->getTableMapping();
  $endpoints_field = FieldStorageConfig::loadByName('relation', 'endpoints');
  $relation_data_table_name = $table_mapping
    ->getDedicatedDataTableName($endpoints_field);
  $entity_id_field_name = $table_mapping
    ->getFieldColumnName($endpoints_field, 'entity_id');
  $entity_type_field_name = $table_mapping
    ->getFieldColumnName($endpoints_field, 'entity_type');
  $r_index_field_name = $table_mapping
    ->getFieldColumnName($endpoints_field, 'r_index');
  $join_type = empty($this->options['required']) ? 'LEFT' : 'INNER';
  $endpoints_twice = isset($this->definition['entity_type_left']) && isset($this->definition['entity_type_right']);
  $this
    ->ensureMyTable();

  // Join the left table with the entity type to the endpoints field data
  // table.
  $configuration = array(
    'left_table' => $this->tableAlias,
    'left_field' => $this->realField,
    'table' => $relation_data_table_name,
    'type' => $join_type,
    'extra' => array(
      array(
        'field' => 'bundle',
        'value' => $this->definition['relation_type'],
      ),
    ),
  );
  if (isset($this->definition['entity_type_left'])) {

    // The left table is an entity, not a relation.
    $configuration['field'] = $entity_id_field_name;
    $this
      ->ensureNoDuplicateEntities($configuration['extra'], $this->options['entity_deduplication_left'], $this->definition['relation_type'], $this->definition['entity_type_left'], $this->tableAlias, $this->realField);
    $configuration['extra'][] = array(
      'field' => $entity_type_field_name,
      'value' => $this->definition['entity_type_left'],
    );
  }
  else {

    // The left table is relation.
    $configuration['field'] = 'entity_id';
  }
  if ($this->definition['directional'] && $this->options['r_index'] > -1) {
    $configuration['extra'][] = array(
      'field' => $r_index_field_name,
      'value' => $this->options['r_index'],
    );
  }
  $join = Views::pluginManager('join')
    ->createInstance('standard', $configuration);
  $join->adjusted = TRUE;
  $l = $this->query
    ->addTable($relation_data_table_name, $this->relationship, $join);
  if ($endpoints_twice) {

    // Execute a self-join.
    $configuration = array(
      'left_table' => $l,
      'left_field' => 'entity_id',
      'table' => $relation_data_table_name,
      'field' => 'entity_id',
      'type' => $join_type,
      'extra' => array(
        array(
          'field' => $entity_type_field_name,
          'value' => $this->definition['entity_type_right'],
        ),
      ),
    );
    if ($this->definition['entity_type_left'] == $this->definition['entity_type_right']) {
      $configuration['extra'][] = array(
        // This definition is a bit funny but there's no other way to tell
        // Views to use an expression in join extra as it is.
        'field' => $r_index_field_name . ' !=  ' . $l . '.' . $r_index_field_name . ' AND 1',
        'value' => 1,
      );
    }
    $join = Views::pluginManager('join')
      ->createInstance('standard', $configuration);
    $join->adjusted = TRUE;
    $r = $this->query
      ->addTable($relation_data_table_name, $this->relationship, $join);
  }
  else {
    $r = $l;
  }
  $configuration = array(
    'left_table' => $r,
    'table' => $this->definition['base'],
    'field' => $this->definition['base field'],
    'type' => $join_type,
  );
  if (isset($this->definition['entity_type_right'])) {

    // We are finishing on an entity table.
    $configuration['left_field'] = $entity_id_field_name;
    $this
      ->ensureNoDuplicateEntities($configuration['extra'], $this->options['entity_deduplication_right'], $this->definition['relation_type'], $this->definition['entity_type_right'], $r, $entity_id_field_name);
    $configuration['extra'][] = array(
      'table' => $r,
      'field' => $entity_type_field_name,
      'value' => $this->definition['entity_type_right'],
    );
  }
  else {

    // We are finishing on relation.
    $configuration['left_field'] = 'entity_id';
  }
  $join = Views::pluginManager('join')
    ->createInstance('standard', $configuration);
  $join->adjusted = TRUE;

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