function Tables::addField

Parameters

string $field: If it contains a dot, then field name dot field column. If it doesn't then entity property name.

string $type: Join type, can either be INNER or LEFT.

Return value

string The return value is a string containing the alias of the table, a dot and the appropriate SQL column as passed in. This allows the direct use of this in a query for a condition or sort.

File

drupal/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Entity/Tables.php, line 62
Definition of Drupal\field_sql_storage\Entity\Tables.

Class

Tables
Adds tables and fields to the SQL entity query.

Namespace

Drupal\field_sql_storage\Entity

Code

function addField($field, $type, $langcode) {
  $entity_type = $this->sqlQuery
    ->getMetaData('entity_type');
  $age = $this->sqlQuery
    ->getMetaData('age');

  // This variable ensures grouping works correctly. For example:
  // ->condition('tags', 2, '>')
  // ->condition('tags', 20, '<')
  // ->condition('node_reference.nid.entity.tags', 2)
  // The first two should use the same table but the last one needs to be a
  // new table. So for the first two, the table array index will be 'tags'
  // while the third will be 'node_reference.nid.tags'.
  $index_prefix = '';
  $specifiers = explode('.', $field);
  $base_table = 'base_table';
  $count = count($specifiers) - 1;

  // This will contain the definitions of the last specifier seen by the
  // system.
  $propertyDefinitions = array();
  $entity_info = entity_get_info($entity_type);

  // Use the lightweight and fast field map for checking whether a specifier
  // is a field or not. While calling field_info_field() on every specifier
  // delivers the same information, if no specifiers are using the field API
  // it is much faster if field_info_field() is never called.
  $field_map = field_info_field_map();
  for ($key = 0; $key <= $count; $key++) {

    // If there is revision support and only the current revision is being
    // queried then use the revision id. Otherwise, the entity id will do.
    if (!empty($entity_info['entity_keys']['revision']) && $age == FIELD_LOAD_CURRENT) {

      // This contains the relevant SQL field to be used when joining entity
      // tables.
      $entity_id_field = $entity_info['entity_keys']['revision'];

      // This contains the relevant SQL field to be used when joining field
      // tables.
      $field_id_field = 'revision_id';
    }
    else {
      $entity_id_field = $entity_info['entity_keys']['id'];
      $field_id_field = 'entity_id';
    }

    // This can either be the name of an entity property (non-configurable
    // field), a field API field (a configurable field).
    $specifier = $specifiers[$key];

    // First, check for field API fields by trying to retrieve the field specified.
    // Normally it is a field name, but field_purge_batch() is passing in
    // id:$field_id so check that first.
    if (substr($specifier, 0, 3) == 'id:') {
      $field = field_info_field_by_id(substr($specifier, 3));
    }
    elseif (isset($field_map[$specifier])) {
      $field = field_info_field($specifier);
    }
    else {
      $field = FALSE;
    }

    // If we managed to retrieve the field, process it.
    if ($field) {

      // Find the field column.
      $column = FALSE;
      if ($key < $count) {
        $next = $specifiers[$key + 1];

        // Is this a field column?
        if (isset($field['columns'][$next]) || in_array($next, Field::getReservedColumns())) {

          // Use it.
          $column = $next;

          // Do not process it again.
          $key++;
        }

        // If there are more specifiers, the next one must be a
        // relationship. Either the field name followed by a relationship
        // specifier, for example $node->field_image->entity. Or a field
        // column followed by a relationship specifier, for example
        // $node->field_image->fid->entity. In both cases, prepare the
        // property definitions for the relationship. In the first case,
        // also use the property definitions for column.
        if ($key < $count) {
          $relationship_specifier = $specifiers[$key + 1];

          // Get the field definitions form a mocked entity.
          $entity = entity_create($entity_type, array());
          $propertyDefinitions = $entity->{$field['field_name']}
            ->getPropertyDefinitions();

          // If the column is not yet known, ie. the
          // $node->field_image->entity case then use the id source as the
          // column.
          if (!$column && isset($propertyDefinitions[$relationship_specifier]['settings']['id source'])) {

            // If this is a valid relationship, use the id source.
            // Otherwise, the code executing the relationship will throw an
            // exception anyways so no need to do it here.
            $column = $propertyDefinitions[$relationship_specifier]['settings']['id source'];
          }

          // Prepare the next index prefix.
          $next_index_prefix = "{$relationship_specifier}.{$column}";
        }
      }
      else {

        // If this is the last specifier, default to value.
        $column = 'value';
      }
      $table = $this
        ->ensureFieldTable($index_prefix, $field, $type, $langcode, $base_table, $entity_id_field, $field_id_field);
      $sql_column = _field_sql_storage_columnname($field['field_name'], $column);
    }
    else {

      // ensureEntityTable() decides whether an entity property will be
      // queried from the data table or the base table based on where it
      // finds the property first. The data table is prefered, which is why
      // it gets added before the base table.
      $entity_tables = array();
      if (isset($entity_info['data_table'])) {
        $this->sqlQuery
          ->addMetaData('simple_query', FALSE);
        $entity_tables[$entity_info['data_table']] = drupal_get_schema($entity_info['data_table']);
      }
      $entity_tables[$entity_info['base_table']] = drupal_get_schema($entity_info['base_table']);
      $sql_column = $specifier;
      $table = $this
        ->ensureEntityTable($index_prefix, $specifier, $type, $langcode, $base_table, $entity_id_field, $entity_tables);
    }

    // If there are more specifiers to come, it's a relationship.
    if ($key < $count) {

      // Computed fields have prepared their property definition already, do
      // it for properties as well.
      if (!$propertyDefinitions) {

        // Create a relevant entity to find the definition for this
        // property.
        $values = array();

        // If there are bundles, pick one. It does not matter which,
        // properties exist on all bundles.
        if (!empty($entity_info['entity keys']['bundle'])) {
          $values[$entity_info['entity keys']['bundle']] = key(entity_get_bundles('node'));
        }
        $entity = entity_create($entity_type, $values);
        $propertyDefinitions = $entity->{$specifier}
          ->getPropertyDefinitions();
        $relationship_specifier = $specifiers[$key + 1];
        $next_index_prefix = $relationship_specifier;
      }

      // Check for a valid relationship.
      if (isset($propertyDefinitions[$relationship_specifier]['constraints']['EntityType']) && isset($propertyDefinitions[$relationship_specifier]['settings']['id source'])) {

        // If it is, use the entity type.
        $entity_type = $propertyDefinitions[$relationship_specifier]['constraints']['EntityType'];
        $entity_info = entity_get_info($entity_type);

        // Add the new entity base table using the table and sql column.
        $join_condition = '%alias.' . $entity_info['entity_keys']['id'] . " = {$table}.{$sql_column}";
        $base_table = $this->sqlQuery
          ->leftJoin($entity_info['base_table'], NULL, $join_condition);
        $propertyDefinitions = array();
        $key++;
        $index_prefix .= "{$next_index_prefix}.";
      }
      else {
        throw new QueryException(format_string('Invalid specifier @next.', array(
          '@next' => $relationship_specifier,
        )));
      }
    }
  }
  return "{$table}.{$sql_column}";
}