annotate core/modules/workspaces/src/ViewsQueryAlter.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@17 1 <?php
Chris@17 2
Chris@17 3 namespace Drupal\workspaces;
Chris@17 4
Chris@17 5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
Chris@17 6 use Drupal\Core\Entity\EntityFieldManagerInterface;
Chris@17 7 use Drupal\Core\Entity\EntityTypeInterface;
Chris@17 8 use Drupal\Core\Entity\EntityTypeManagerInterface;
Chris@17 9 use Drupal\views\Plugin\views\query\QueryPluginBase;
Chris@17 10 use Drupal\views\Plugin\views\query\Sql;
Chris@17 11 use Drupal\views\Plugin\ViewsHandlerManager;
Chris@17 12 use Drupal\views\ViewExecutable;
Chris@17 13 use Drupal\views\ViewsData;
Chris@17 14 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@17 15
Chris@17 16 /**
Chris@17 17 * Defines a class for altering views queries.
Chris@17 18 *
Chris@17 19 * @internal
Chris@17 20 */
Chris@17 21 class ViewsQueryAlter implements ContainerInjectionInterface {
Chris@17 22
Chris@17 23 /**
Chris@17 24 * The entity type manager service.
Chris@17 25 *
Chris@17 26 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
Chris@17 27 */
Chris@17 28 protected $entityTypeManager;
Chris@17 29
Chris@17 30 /**
Chris@17 31 * The entity field manager.
Chris@17 32 *
Chris@17 33 * @var \Drupal\Core\Entity\EntityFieldManagerInterface
Chris@17 34 */
Chris@17 35 protected $entityFieldManager;
Chris@17 36
Chris@17 37 /**
Chris@17 38 * The workspace manager service.
Chris@17 39 *
Chris@17 40 * @var \Drupal\workspaces\WorkspaceManagerInterface
Chris@17 41 */
Chris@17 42 protected $workspaceManager;
Chris@17 43
Chris@17 44 /**
Chris@17 45 * The views data.
Chris@17 46 *
Chris@17 47 * @var \Drupal\views\ViewsData
Chris@17 48 */
Chris@17 49 protected $viewsData;
Chris@17 50
Chris@17 51 /**
Chris@17 52 * A plugin manager which handles instances of views join plugins.
Chris@17 53 *
Chris@17 54 * @var \Drupal\views\Plugin\ViewsHandlerManager
Chris@17 55 */
Chris@17 56 protected $viewsJoinPluginManager;
Chris@17 57
Chris@17 58 /**
Chris@17 59 * Constructs a new ViewsQueryAlter instance.
Chris@17 60 *
Chris@17 61 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
Chris@17 62 * The entity type manager service.
Chris@17 63 * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
Chris@17 64 * The entity field manager.
Chris@17 65 * @param \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager
Chris@17 66 * The workspace manager service.
Chris@17 67 * @param \Drupal\views\ViewsData $views_data
Chris@17 68 * The views data.
Chris@17 69 * @param \Drupal\views\Plugin\ViewsHandlerManager $views_join_plugin_manager
Chris@17 70 * The views join plugin manager.
Chris@17 71 */
Chris@17 72 public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, WorkspaceManagerInterface $workspace_manager, ViewsData $views_data, ViewsHandlerManager $views_join_plugin_manager) {
Chris@17 73 $this->entityTypeManager = $entity_type_manager;
Chris@17 74 $this->entityFieldManager = $entity_field_manager;
Chris@17 75 $this->workspaceManager = $workspace_manager;
Chris@17 76 $this->viewsData = $views_data;
Chris@17 77 $this->viewsJoinPluginManager = $views_join_plugin_manager;
Chris@17 78 }
Chris@17 79
Chris@17 80 /**
Chris@17 81 * {@inheritdoc}
Chris@17 82 */
Chris@17 83 public static function create(ContainerInterface $container) {
Chris@17 84 return new static(
Chris@17 85 $container->get('entity_type.manager'),
Chris@17 86 $container->get('entity_field.manager'),
Chris@17 87 $container->get('workspaces.manager'),
Chris@17 88 $container->get('views.views_data'),
Chris@17 89 $container->get('plugin.manager.views.join')
Chris@17 90 );
Chris@17 91 }
Chris@17 92
Chris@17 93 /**
Chris@17 94 * Implements a hook bridge for hook_views_query_alter().
Chris@17 95 *
Chris@17 96 * @see hook_views_query_alter()
Chris@17 97 */
Chris@17 98 public function alterQuery(ViewExecutable $view, QueryPluginBase $query) {
Chris@17 99 // Don't alter any views queries if we're in the default workspace.
Chris@17 100 if ($this->workspaceManager->getActiveWorkspace()->isDefaultWorkspace()) {
Chris@17 101 return;
Chris@17 102 }
Chris@17 103
Chris@17 104 // Don't alter any non-sql views queries.
Chris@17 105 if (!$query instanceof Sql) {
Chris@17 106 return;
Chris@17 107 }
Chris@17 108
Chris@17 109 // Find out what entity types are represented in this query.
Chris@17 110 $entity_type_ids = [];
Chris@17 111 foreach ($query->relationships as $info) {
Chris@17 112 $table_data = $this->viewsData->get($info['base']);
Chris@17 113 if (empty($table_data['table']['entity type'])) {
Chris@17 114 continue;
Chris@17 115 }
Chris@17 116 $entity_type_id = $table_data['table']['entity type'];
Chris@17 117 // This construct ensures each entity type exists only once.
Chris@17 118 $entity_type_ids[$entity_type_id] = $entity_type_id;
Chris@17 119 }
Chris@17 120
Chris@17 121 $entity_type_definitions = $this->entityTypeManager->getDefinitions();
Chris@17 122 foreach ($entity_type_ids as $entity_type_id) {
Chris@17 123 if ($this->workspaceManager->isEntityTypeSupported($entity_type_definitions[$entity_type_id])) {
Chris@17 124 $this->alterQueryForEntityType($query, $entity_type_definitions[$entity_type_id]);
Chris@17 125 }
Chris@17 126 }
Chris@17 127 }
Chris@17 128
Chris@17 129 /**
Chris@17 130 * Alters the entity type tables for a Views query.
Chris@17 131 *
Chris@17 132 * This should only be called after determining that this entity type is
Chris@17 133 * involved in the query, and that a non-default workspace is in use.
Chris@17 134 *
Chris@17 135 * @param \Drupal\views\Plugin\views\query\Sql $query
Chris@17 136 * The query plugin object for the query.
Chris@17 137 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
Chris@17 138 * The entity type definition.
Chris@17 139 */
Chris@17 140 protected function alterQueryForEntityType(Sql $query, EntityTypeInterface $entity_type) {
Chris@17 141 /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
Chris@17 142 $table_mapping = $this->entityTypeManager->getStorage($entity_type->id())->getTableMapping();
Chris@17 143 $field_storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions($entity_type->id());
Chris@17 144 $dedicated_field_storage_definitions = array_filter($field_storage_definitions, function ($definition) use ($table_mapping) {
Chris@17 145 return $table_mapping->requiresDedicatedTableStorage($definition);
Chris@17 146 });
Chris@17 147 $dedicated_field_data_tables = array_map(function ($definition) use ($table_mapping) {
Chris@17 148 return $table_mapping->getDedicatedDataTableName($definition);
Chris@17 149 }, $dedicated_field_storage_definitions);
Chris@17 150
Chris@17 151 $move_workspace_tables = [];
Chris@17 152 $table_queue =& $query->getTableQueue();
Chris@17 153 foreach ($table_queue as $alias => &$table_info) {
Chris@17 154 // If we reach the workspace_association array item before any candidates,
Chris@17 155 // then we do not need to move it.
Chris@17 156 if ($table_info['table'] == 'workspace_association') {
Chris@17 157 break;
Chris@17 158 }
Chris@17 159
Chris@17 160 // Any dedicated field table is a candidate.
Chris@17 161 if ($field_name = array_search($table_info['table'], $dedicated_field_data_tables, TRUE)) {
Chris@17 162 $relationship = $table_info['relationship'];
Chris@17 163
Chris@17 164 // There can be reverse relationships used. If so, Workspaces can't do
Chris@17 165 // anything with them. Detect this and skip.
Chris@17 166 if ($table_info['join']->field != 'entity_id') {
Chris@17 167 continue;
Chris@17 168 }
Chris@17 169
Chris@17 170 // Get the dedicated revision table name.
Chris@17 171 $new_table_name = $table_mapping->getDedicatedRevisionTableName($field_storage_definitions[$field_name]);
Chris@17 172
Chris@17 173 // Now add the workspace_association table.
Chris@17 174 $workspace_association_table = $this->ensureWorkspaceAssociationTable($entity_type->id(), $query, $relationship);
Chris@17 175
Chris@17 176 // Update the join to use our COALESCE.
Chris@17 177 $revision_field = $entity_type->getKey('revision');
Chris@17 178 $table_info['join']->leftTable = NULL;
Chris@17 179 $table_info['join']->leftField = "COALESCE($workspace_association_table.target_entity_revision_id, $relationship.$revision_field)";
Chris@17 180
Chris@17 181 // Update the join and the table info to our new table name, and to join
Chris@17 182 // on the revision key.
Chris@17 183 $table_info['table'] = $new_table_name;
Chris@17 184 $table_info['join']->table = $new_table_name;
Chris@17 185 $table_info['join']->field = 'revision_id';
Chris@17 186
Chris@17 187 // Finally, if we added the workspace_association table we have to move
Chris@17 188 // it in the table queue so that it comes before this field.
Chris@17 189 if (empty($move_workspace_tables[$workspace_association_table])) {
Chris@17 190 $move_workspace_tables[$workspace_association_table] = $alias;
Chris@17 191 }
Chris@17 192 }
Chris@17 193 }
Chris@17 194
Chris@17 195 // JOINs must be in order. i.e, any tables you mention in the ON clause of a
Chris@17 196 // JOIN must appear prior to that JOIN. Since we're modifying a JOIN in
Chris@17 197 // place, and adding a new table, we must ensure that the new table appears
Chris@17 198 // prior to this one. So we recorded at what index we saw that table, and
Chris@17 199 // then use array_splice() to move the workspace_association table join to
Chris@17 200 // the correct position.
Chris@17 201 foreach ($move_workspace_tables as $workspace_association_table => $alias) {
Chris@17 202 $this->moveEntityTable($query, $workspace_association_table, $alias);
Chris@17 203 }
Chris@17 204
Chris@17 205 $base_entity_table = $entity_type->isTranslatable() ? $entity_type->getDataTable() : $entity_type->getBaseTable();
Chris@17 206
Chris@17 207 $base_fields = array_diff($table_mapping->getFieldNames($entity_type->getBaseTable()), [$entity_type->getKey('langcode')]);
Chris@17 208 $revisionable_fields = array_diff($table_mapping->getFieldNames($entity_type->getRevisionDataTable()), $base_fields);
Chris@17 209
Chris@17 210 // Go through and look to see if we have to modify fields and filters.
Chris@17 211 foreach ($query->fields as &$field_info) {
Chris@17 212 // Some fields don't actually have tables, meaning they're formulae and
Chris@17 213 // whatnot. At this time we are going to ignore those.
Chris@17 214 if (empty($field_info['table'])) {
Chris@17 215 continue;
Chris@17 216 }
Chris@17 217
Chris@17 218 // Dereference the alias into the actual table.
Chris@17 219 $table = $table_queue[$field_info['table']]['table'];
Chris@17 220 if ($table == $base_entity_table && in_array($field_info['field'], $revisionable_fields)) {
Chris@17 221 $relationship = $table_queue[$field_info['table']]['alias'];
Chris@17 222 $alias = $this->ensureRevisionTable($entity_type, $query, $relationship);
Chris@17 223 if ($alias) {
Chris@17 224 // Change the base table to use the revision table instead.
Chris@17 225 $field_info['table'] = $alias;
Chris@17 226 }
Chris@17 227 }
Chris@17 228 }
Chris@17 229
Chris@17 230 $relationships = [];
Chris@17 231 // Build a list of all relationships that might be for our table.
Chris@17 232 foreach ($query->relationships as $relationship => $info) {
Chris@17 233 if ($info['base'] == $base_entity_table) {
Chris@17 234 $relationships[] = $relationship;
Chris@17 235 }
Chris@17 236 }
Chris@17 237
Chris@17 238 // Now we have to go through our where clauses and modify any of our fields.
Chris@17 239 foreach ($query->where as &$clauses) {
Chris@17 240 foreach ($clauses['conditions'] as &$where_info) {
Chris@17 241 // Build a matrix of our possible relationships against fields we need
Chris@17 242 // to switch.
Chris@17 243 foreach ($relationships as $relationship) {
Chris@17 244 foreach ($revisionable_fields as $field) {
Chris@17 245 if (is_string($where_info['field']) && $where_info['field'] == "$relationship.$field") {
Chris@17 246 $alias = $this->ensureRevisionTable($entity_type, $query, $relationship);
Chris@17 247 if ($alias) {
Chris@17 248 // Change the base table to use the revision table instead.
Chris@17 249 $where_info['field'] = "$alias.$field";
Chris@17 250 }
Chris@17 251 }
Chris@17 252 }
Chris@17 253 }
Chris@17 254 }
Chris@17 255 }
Chris@17 256
Chris@17 257 // @todo Handle $query->orderby, $query->groupby, $query->having and
Chris@17 258 // $query->count_field in https://www.drupal.org/node/2968165.
Chris@17 259 }
Chris@17 260
Chris@17 261 /**
Chris@17 262 * Adds the 'workspace_association' table to a views query.
Chris@17 263 *
Chris@17 264 * @param string $entity_type_id
Chris@17 265 * The ID of the entity type to join.
Chris@17 266 * @param \Drupal\views\Plugin\views\query\Sql $query
Chris@17 267 * The query plugin object for the query.
Chris@17 268 * @param string $relationship
Chris@17 269 * The primary table alias this table is related to.
Chris@17 270 *
Chris@17 271 * @return string
Chris@17 272 * The alias of the 'workspace_association' table.
Chris@17 273 */
Chris@17 274 protected function ensureWorkspaceAssociationTable($entity_type_id, Sql $query, $relationship) {
Chris@17 275 if (isset($query->tables[$relationship]['workspace_association'])) {
Chris@17 276 return $query->tables[$relationship]['workspace_association']['alias'];
Chris@17 277 }
Chris@17 278
Chris@17 279 $table_data = $this->viewsData->get($query->relationships[$relationship]['base']);
Chris@17 280
Chris@17 281 // Construct the join.
Chris@17 282 $definition = [
Chris@17 283 'table' => 'workspace_association',
Chris@17 284 'field' => 'target_entity_id',
Chris@17 285 'left_table' => $relationship,
Chris@17 286 'left_field' => $table_data['table']['base']['field'],
Chris@17 287 'extra' => [
Chris@17 288 [
Chris@17 289 'field' => 'target_entity_type_id',
Chris@17 290 'value' => $entity_type_id,
Chris@17 291 ],
Chris@17 292 [
Chris@17 293 'field' => 'workspace',
Chris@17 294 'value' => $this->workspaceManager->getActiveWorkspace()->id(),
Chris@17 295 ],
Chris@17 296 ],
Chris@17 297 'type' => 'LEFT',
Chris@17 298 ];
Chris@17 299
Chris@17 300 $join = $this->viewsJoinPluginManager->createInstance('standard', $definition);
Chris@17 301 $join->adjusted = TRUE;
Chris@17 302
Chris@17 303 return $query->queueTable('workspace_association', $relationship, $join);
Chris@17 304 }
Chris@17 305
Chris@17 306 /**
Chris@17 307 * Adds the revision table of an entity type to a query object.
Chris@17 308 *
Chris@17 309 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
Chris@17 310 * The entity type definition.
Chris@17 311 * @param \Drupal\views\Plugin\views\query\Sql $query
Chris@17 312 * The query plugin object for the query.
Chris@17 313 * @param string $relationship
Chris@17 314 * The name of the relationship.
Chris@17 315 *
Chris@17 316 * @return string
Chris@17 317 * The alias of the relationship.
Chris@17 318 */
Chris@17 319 protected function ensureRevisionTable(EntityTypeInterface $entity_type, Sql $query, $relationship) {
Chris@17 320 // Get the alias for the 'workspace_association' table we chain off of in
Chris@17 321 // the COALESCE.
Chris@17 322 $workspace_association_table = $this->ensureWorkspaceAssociationTable($entity_type->id(), $query, $relationship);
Chris@17 323
Chris@17 324 // Get the name of the revision table and revision key.
Chris@17 325 $base_revision_table = $entity_type->isTranslatable() ? $entity_type->getRevisionDataTable() : $entity_type->getRevisionTable();
Chris@17 326 $revision_field = $entity_type->getKey('revision');
Chris@17 327
Chris@17 328 // If the table was already added and has a join against the same field on
Chris@17 329 // the revision table, reuse that rather than adding a new join.
Chris@17 330 if (isset($query->tables[$relationship][$base_revision_table])) {
Chris@17 331 $table_queue =& $query->getTableQueue();
Chris@17 332 $alias = $query->tables[$relationship][$base_revision_table]['alias'];
Chris@17 333 if (isset($table_queue[$alias]['join']->field) && $table_queue[$alias]['join']->field == $revision_field) {
Chris@17 334 // If this table previously existed, but was not added by us, we need
Chris@17 335 // to modify the join and make sure that 'workspace_association' comes
Chris@17 336 // first.
Chris@17 337 if (empty($table_queue[$alias]['join']->workspace_adjusted)) {
Chris@17 338 $table_queue[$alias]['join'] = $this->getRevisionTableJoin($relationship, $base_revision_table, $revision_field, $workspace_association_table);
Chris@17 339 // We also have to ensure that our 'workspace_association' comes before
Chris@17 340 // this.
Chris@17 341 $this->moveEntityTable($query, $workspace_association_table, $alias);
Chris@17 342 }
Chris@17 343
Chris@17 344 return $alias;
Chris@17 345 }
Chris@17 346 }
Chris@17 347
Chris@17 348 // Construct a new join.
Chris@17 349 $join = $this->getRevisionTableJoin($relationship, $base_revision_table, $revision_field, $workspace_association_table);
Chris@17 350 return $query->queueTable($base_revision_table, $relationship, $join);
Chris@17 351 }
Chris@17 352
Chris@17 353 /**
Chris@17 354 * Fetches a join for a revision table using the workspace_association table.
Chris@17 355 *
Chris@17 356 * @param string $relationship
Chris@17 357 * The relationship to use in the view.
Chris@17 358 * @param string $table
Chris@17 359 * The table name.
Chris@17 360 * @param string $field
Chris@17 361 * The field to join on.
Chris@17 362 * @param string $workspace_association_table
Chris@17 363 * The alias of the 'workspace_association' table joined to the main entity
Chris@17 364 * table.
Chris@17 365 *
Chris@17 366 * @return \Drupal\views\Plugin\views\join\JoinPluginInterface
Chris@17 367 * An adjusted views join object to add to the query.
Chris@17 368 */
Chris@17 369 protected function getRevisionTableJoin($relationship, $table, $field, $workspace_association_table) {
Chris@17 370 $definition = [
Chris@17 371 'table' => $table,
Chris@17 372 'field' => $field,
Chris@17 373 // Making this explicitly null allows the left table to be a formula.
Chris@17 374 'left_table' => NULL,
Chris@17 375 'left_field' => "COALESCE($workspace_association_table.target_entity_revision_id, $relationship.$field)",
Chris@17 376 ];
Chris@17 377
Chris@17 378 /** @var \Drupal\views\Plugin\views\join\JoinPluginInterface $join */
Chris@17 379 $join = $this->viewsJoinPluginManager->createInstance('standard', $definition);
Chris@17 380 $join->adjusted = TRUE;
Chris@17 381 $join->workspace_adjusted = TRUE;
Chris@17 382
Chris@17 383 return $join;
Chris@17 384 }
Chris@17 385
Chris@17 386 /**
Chris@17 387 * Moves a 'workspace_association' table to appear before the given alias.
Chris@17 388 *
Chris@17 389 * Because Workspace chains possibly pre-existing tables onto the
Chris@17 390 * 'workspace_association' table, we have to ensure that the
Chris@17 391 * 'workspace_association' table appears in the query before the alias it's
Chris@17 392 * chained on or the SQL is invalid.
Chris@17 393 *
Chris@17 394 * @param \Drupal\views\Plugin\views\query\Sql $query
Chris@17 395 * The SQL query object.
Chris@17 396 * @param string $workspace_association_table
Chris@17 397 * The alias of the 'workspace_association' table.
Chris@17 398 * @param string $alias
Chris@17 399 * The alias of the table it needs to appear before.
Chris@17 400 */
Chris@17 401 protected function moveEntityTable(Sql $query, $workspace_association_table, $alias) {
Chris@17 402 $table_queue =& $query->getTableQueue();
Chris@17 403 $keys = array_keys($table_queue);
Chris@17 404 $current_index = array_search($workspace_association_table, $keys);
Chris@17 405 $index = array_search($alias, $keys);
Chris@17 406
Chris@17 407 // If it's already before our table, we don't need to move it, as we could
Chris@17 408 // accidentally move it forward.
Chris@17 409 if ($current_index < $index) {
Chris@17 410 return;
Chris@17 411 }
Chris@17 412 $splice = [$workspace_association_table => $table_queue[$workspace_association_table]];
Chris@17 413 unset($table_queue[$workspace_association_table]);
Chris@17 414
Chris@17 415 // Now move the item to the proper location in the array. Don't use
Chris@17 416 // array_splice() because that breaks indices.
Chris@17 417 $table_queue = array_slice($table_queue, 0, $index, TRUE) +
Chris@17 418 $splice +
Chris@17 419 array_slice($table_queue, $index, NULL, TRUE);
Chris@17 420 }
Chris@17 421
Chris@17 422 }