public Tables::addField($field, $type, $langcode)
Adds a field to a database query.
Parameters
string $field: If it doesn't contain a dot, then an entity base field name. If it contains a dot, then either field name dot field column or field name dot delta dot field column. Delta can be a numeric value or a "%delta" for any value.
string $type: Join type, can either be INNER or LEFT.
string $langcode: The language code the field values are to be queried in.
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.
Throws
\Drupal\Core\Entity\Query\QueryException If $field specifies an invalid relationship.
Overrides TablesInterface::addField
File
- core/lib/Drupal/Core/Entity/Query/Sql/Tables.php, line 64
Class
- Tables
- Adds tables and fields to the SQL entity query.
Namespace
Drupal\Core\Entity\Query\Sql
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | public function addField( $field , $type , $langcode ) { $entity_type_id = $this ->sqlQuery->getMetaData( 'entity_type' ); $all_revisions = $this ->sqlQuery->getMetaData( 'all_revisions' ); // 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_type = $this ->entityManager->getDefinition( $entity_type_id ); $field_storage_definitions = $this ->entityManager->getFieldStorageDefinitions( $entity_type_id ); for ( $key = 0; $key <= $count ; $key ++) { // This can either be the name of an entity base field or a configurable // field. $specifier = $specifiers [ $key ]; if (isset( $field_storage_definitions [ $specifier ])) { $field_storage = $field_storage_definitions [ $specifier ]; } else { $field_storage = FALSE; } // If there is revision support, only the current revisions are being // queried, and the field is revisionable then use the revision id. // Otherwise, the entity id will do. if (( $revision_key = $entity_type ->getKey( 'revision' )) && $all_revisions && $field_storage && $field_storage ->isRevisionable()) { // This contains the relevant SQL field to be used when joining entity // tables. $entity_id_field = $revision_key ; // This contains the relevant SQL field to be used when joining field // tables. $field_id_field = 'revision_id' ; } else { $entity_id_field = $entity_type ->getKey( 'id' ); $field_id_field = 'entity_id' ; } /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ $table_mapping = $this ->entityManager->getStorage( $entity_type_id )->getTableMapping(); // Check whether this field is stored in a dedicated table. if ( $field_storage && $table_mapping ->requiresDedicatedTableStorage( $field_storage )) { $delta = NULL; // Find the field column. $column = $field_storage ->getMainPropertyName(); if ( $key < $count ) { $next = $specifiers [ $key + 1]; // If this is a numeric specifier we're adding a condition on the // specific delta. if ( is_numeric ( $next )) { $delta = $next ; $index_prefix .= ".$delta" ; // Do not process it again. $key ++; $next = $specifiers [ $key + 1]; } // If this specifier is the reserved keyword "%delta" we're adding a // condition on a delta range. elseif ( $next == TableMappingInterface::DELTA) { $index_prefix .= TableMappingInterface::DELTA; // Do not process it again. $key ++; // If there are more specifiers to work with then continue // processing. If this is the last specifier then use the reserved // keyword as a column name. if ( $key < $count ) { $next = $specifiers [ $key + 1]; } else { $column = TableMappingInterface::DELTA; } } // Is this a field column? $columns = $field_storage ->getColumns(); if (isset( $columns [ $next ]) || in_array( $next , $table_mapping ->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]; $propertyDefinitions = $field_storage ->getPropertyDefinitions(); // Prepare the next index prefix. $next_index_prefix = "$relationship_specifier.$column" ; } } $table = $this ->ensureFieldTable( $index_prefix , $field_storage , $type , $langcode , $base_table , $entity_id_field , $field_id_field , $delta ); $sql_column = $table_mapping ->getFieldColumnName( $field_storage , $column ); $property_definitions = $field_storage ->getPropertyDefinitions(); if (isset( $property_definitions [ $column ])) { $this ->caseSensitiveFields[ $field ] = $property_definitions [ $column ]->getSetting( 'case_sensitive' ); } } // The field is stored in a shared table. 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 preferred, which is why // it gets added before the base table. $entity_tables = array (); if ( $all_revisions && $field_storage && $field_storage ->isRevisionable()) { $data_table = $entity_type ->getRevisionDataTable(); $entity_base_table = $entity_type ->getRevisionTable(); } else { $data_table = $entity_type ->getDataTable(); $entity_base_table = $entity_type ->getBaseTable(); } if ( $data_table ) { $this ->sqlQuery->addMetaData( 'simple_query' , FALSE); $entity_tables [ $data_table ] = $this ->getTableMapping( $data_table , $entity_type_id ); } $entity_tables [ $entity_base_table ] = $this ->getTableMapping( $entity_base_table , $entity_type_id ); $sql_column = $specifier ; // If there are more specifiers, get the right sql column name if the // next one is a column of this field. if ( $key < $count ) { $next = $specifiers [ $key + 1]; // If this specifier is the reserved keyword "%delta" we're adding a // condition on a delta range. if ( $next == TableMappingInterface::DELTA) { $key ++; if ( $key < $count ) { $next = $specifiers [ $key + 1]; } else { return 0; } } // If this is a numeric specifier we're adding a condition on the // specific delta. Since we know that this is a single value base // field no other value than 0 makes sense. if ( is_numeric ( $next )) { if ( $next > 0) { $this ->sqlQuery->condition( '1 <> 1' ); } $key ++; $next = $specifiers [ $key + 1]; } // Is this a field column? $columns = $field_storage ->getColumns(); if (isset( $columns [ $next ]) || in_array( $next , $table_mapping ->getReservedColumns())) { // Use it. $sql_column = $table_mapping ->getFieldColumnName( $field_storage , $next ); // Do not process it again. $key ++; } } $table = $this ->ensureEntityTable( $index_prefix , $sql_column , $type , $langcode , $base_table , $entity_id_field , $entity_tables ); // If there is a field storage (some specifiers are not), check for case // sensitivity. if ( $field_storage ) { $column = $field_storage ->getMainPropertyName(); $base_field_property_definitions = $field_storage ->getPropertyDefinitions(); if (isset( $base_field_property_definitions [ $column ])) { $this ->caseSensitiveFields[ $field ] = $base_field_property_definitions [ $column ]->getSetting( 'case_sensitive' ); } } } // If there are more specifiers to come, it's a relationship. if ( $field_storage && $key < $count ) { // Computed fields have prepared their property definition already, do // it for properties as well. if (! $propertyDefinitions ) { $propertyDefinitions = $field_storage ->getPropertyDefinitions(); $relationship_specifier = $specifiers [ $key + 1]; $next_index_prefix = $relationship_specifier ; } // Check for a valid relationship. if (isset( $propertyDefinitions [ $relationship_specifier ]) && $field_storage ->getPropertyDefinition( 'entity' )->getDataType() == 'entity_reference' ) { // If it is, use the entity type. $entity_type_id = $propertyDefinitions [ $relationship_specifier ]->getTargetDefinition()->getEntityTypeId(); $entity_type = $this ->entityManager->getDefinition( $entity_type_id ); $field_storage_definitions = $this ->entityManager->getFieldStorageDefinitions( $entity_type_id ); // Add the new entity base table using the table and sql column. $join_condition = '%alias.' . $entity_type ->getKey( 'id' ) . " = $table.$sql_column" ; $base_table = $this ->sqlQuery->leftJoin( $entity_type ->getBaseTable(), NULL, $join_condition ); $propertyDefinitions = array (); $key ++; $index_prefix .= "$next_index_prefix." ; } else { throw new QueryException( "Invalid specifier '$relationship_specifier'" ); } } } return "$table.$sql_column" ; } |
Please login to continue.