5 * Provide views data for the Entityqueue module.
8 use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
9 use Drupal\entityqueue\Entity\EntityQueue;
12 * Implements hook_views_data_alter().
14 function entityqueue_views_data_alter(array &$data) {
15 $entity_type_manager = \Drupal::entityTypeManager();
16 $entity_subqueue = $entity_type_manager->getDefinition('entity_subqueue');
18 // Find all entity types that need an 'entityqueue' relationship.
19 $target_entity_type_ids = [];
20 $queues = EntityQueue::loadMultiple();
21 foreach ($queues as $queue) {
22 $target_entity_type_ids[$queue->getTargetEntityTypeId()] = TRUE;
25 // Filter entity types to those that have a 'views_data' handler and use a SQL
27 /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
29 foreach (array_keys($target_entity_type_ids) as $entity_type_id) {
30 $entity_type = $entity_type_manager->getDefinition($entity_type_id);
31 if ($entity_type->hasHandlerClass('views_data') && $entity_type_manager->getStorage($entity_type->id()) instanceof SqlEntityStorageInterface) {
32 $entity_types[$entity_type_id] = $entity_type;
36 foreach ($entity_types as $entity_type_id => $entity_type) {
37 $field_name = 'items';
38 $field_storage = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions('entity_subqueue')[$field_name];
39 $target_base_table = $entity_type->getDataTable() ?: $entity_type->getBaseTable();
41 /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
42 $table_mapping = $entity_type_manager->getStorage('entity_subqueue')->getTableMapping();
43 $columns = $table_mapping->getColumnNames($field_name);
44 $subqueue_items_table_name = $table_mapping->getDedicatedDataTableName($field_storage);
46 $data[$target_base_table]['entityqueue_relationship']['relationship'] = [
47 'id' => 'entity_queue',
48 'title' => t('@target_label queue', ['@target_label' => $entity_type->getLabel()]),
49 'label' => t('@target_label queue', ['@target_label' => $entity_type->getLabel()]),
50 'group' => t('Entityqueue'),
51 'help' => t('Create a relationship from @target_label to an entityqueue.', ['@target_label' => $entity_type->getLabel()]),
52 'base' => $entity_subqueue->getDataTable() ?: $entity_subqueue->getBaseTable(),
53 'entity_type' => 'entity_subqueue',
54 'base field' => $entity_subqueue->getKey('id'),
55 'field_name' => $field_storage->getName(),
56 'field table' => $subqueue_items_table_name,
57 'field field' => $columns['target_id'],
60 $data[$target_base_table]['entityqueue_relationship']['sort'] = array(
61 'id' => 'entity_queue_position',
62 'group' => t('Entityqueue'),
63 'title' => t('@target_label Queue Position', array(
64 '@target_label' => $entity_type->getLabel(),
66 'label' => t('@target_label Queue Position', array(
67 '@target_label' => $entity_type->getLabel(),
69 'help' => t('Position of item in the @target_label queue.', array(
70 '@target_label' => $entity_type->getLabel(),
73 'field table' => $subqueue_items_table_name,
74 'field_name' => $field_name,
77 $data[$target_base_table]['entityqueue_relationship']['filter'] = array(
78 'id' => 'entity_queue_in_queue',
80 'group' => t('Entityqueue'),
81 'title' => t('@target_label In Queue', array(
82 '@target_label' => $entity_type->getLabel(),
84 'label' => t('@target_label In Queue', array(
85 '@target_label' => $entity_type->getLabel(),
87 'help' => t('Filter for entities that are available or not in the @target_label entity queue.', array(
88 '@target_label' => $entity_type->getLabel(),
90 'field table' => $subqueue_items_table_name,
91 'field field' => $columns['target_id'],