Chris@0: [], Chris@0: ]; Chris@0: Chris@0: $data['views']['random'] = [ Chris@0: 'title' => t('Random'), Chris@0: 'help' => t('Randomize the display order.'), Chris@0: 'sort' => [ Chris@0: 'id' => 'random', Chris@0: ], Chris@0: ]; Chris@0: Chris@0: $data['views']['null'] = [ Chris@0: 'title' => t('Null'), Chris@0: 'help' => t('Allow a contextual filter value to be ignored. The query will not be altered by this contextual filter value. Can be used when contextual filter values come from the URL, and a part of the URL needs to be ignored.'), Chris@0: 'argument' => [ Chris@0: 'id' => 'null', Chris@0: ], Chris@0: ]; Chris@0: Chris@0: $data['views']['nothing'] = [ Chris@0: 'title' => t('Custom text'), Chris@0: 'help' => t('Provide custom text or link.'), Chris@0: 'field' => [ Chris@0: 'id' => 'custom', Chris@0: ], Chris@0: ]; Chris@0: Chris@0: $data['views']['counter'] = [ Chris@0: 'title' => t('View result counter'), Chris@0: 'help' => t('Displays the actual position of the view result'), Chris@0: 'field' => [ Chris@0: 'id' => 'counter', Chris@0: ], Chris@0: ]; Chris@0: Chris@0: $data['views']['area'] = [ Chris@0: 'title' => t('Text area'), Chris@0: 'help' => t('Provide markup text for the area.'), Chris@0: 'area' => [ Chris@0: 'id' => 'text', Chris@0: ], Chris@0: ]; Chris@0: Chris@0: $data['views']['area_text_custom'] = [ Chris@0: 'title' => t('Unfiltered text'), Chris@0: 'help' => t('Add unrestricted, custom text or markup. This is similar to the custom text field.'), Chris@0: 'area' => [ Chris@0: 'id' => 'text_custom', Chris@0: ], Chris@0: ]; Chris@0: Chris@0: $data['views']['title'] = [ Chris@0: 'title' => t('Title override'), Chris@0: 'help' => t('Override the default view title for this view. This is useful to display an alternative title when a view is empty.'), Chris@0: 'area' => [ Chris@0: 'id' => 'title', Chris@0: 'sub_type' => 'empty', Chris@0: ], Chris@0: ]; Chris@0: Chris@0: $data['views']['view'] = [ Chris@0: 'title' => t('View area'), Chris@0: 'help' => t('Insert a view inside an area.'), Chris@0: 'area' => [ Chris@0: 'id' => 'view', Chris@0: ], Chris@0: ]; Chris@0: Chris@0: $data['views']['result'] = [ Chris@0: 'title' => t('Result summary'), Chris@0: 'help' => t('Shows result summary, for example the items per page.'), Chris@0: 'area' => [ Chris@0: 'id' => 'result', Chris@0: ], Chris@0: ]; Chris@0: Chris@0: $data['views']['messages'] = [ Chris@0: 'title' => t('Messages'), Chris@0: 'help' => t('Displays messages in an area.'), Chris@0: 'area' => [ Chris@0: 'id' => 'messages', Chris@0: ], Chris@0: ]; Chris@0: Chris@0: $data['views']['http_status_code'] = [ Chris@0: 'title' => t('Response status code'), Chris@0: 'help' => t('Alter the HTTP response status code used by this view, mostly helpful for empty results.'), Chris@0: 'area' => [ Chris@0: 'id' => 'http_status_code', Chris@0: ], Chris@0: ]; Chris@0: Chris@0: $data['views']['combine'] = [ Chris@0: 'title' => t('Combine fields filter'), Chris@0: 'help' => t('Combine multiple fields together and search by them.'), Chris@0: 'filter' => [ Chris@0: 'id' => 'combine', Chris@0: ], Chris@0: ]; Chris@0: Chris@0: $data['views']['dropbutton'] = [ Chris@0: 'title' => t('Dropbutton'), Chris@0: 'help' => t('Display fields in a dropbutton.'), Chris@0: 'field' => [ Chris@0: 'id' => 'dropbutton', Chris@0: ], Chris@0: ]; Chris@0: Chris@18: $data['views']['display_link'] = [ Chris@18: 'title' => t('Link to display'), Chris@18: 'help' => t('Displays a link to a path-based display of this view while keeping the filter criteria, sort criteria, pager settings and contextual filters.'), Chris@18: 'area' => [ Chris@18: 'id' => 'display_link', Chris@18: ], Chris@18: ]; Chris@18: Chris@0: // Registers an entity area handler per entity type. Chris@0: foreach (\Drupal::entityManager()->getDefinitions() as $entity_type_id => $entity_type) { Chris@0: // Excludes entity types, which cannot be rendered. Chris@0: if ($entity_type->hasViewBuilderClass()) { Chris@0: $label = $entity_type->getLabel(); Chris@0: $data['views']['entity_' . $entity_type_id] = [ Chris@0: 'title' => t('Rendered entity - @label', ['@label' => $label]), Chris@0: 'help' => t('Displays a rendered @label entity in an area.', ['@label' => $label]), Chris@0: 'area' => [ Chris@0: 'entity_type' => $entity_type_id, Chris@0: 'id' => 'entity', Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: } Chris@0: Chris@0: // Registers an action bulk form per entity. Chris@0: foreach (\Drupal::entityManager()->getDefinitions() as $entity_type => $entity_info) { Chris@0: $actions = array_filter(\Drupal::entityManager()->getStorage('action')->loadMultiple(), function (ActionConfigEntityInterface $action) use ($entity_type) { Chris@0: return $action->getType() == $entity_type; Chris@0: }); Chris@0: if (empty($actions)) { Chris@0: continue; Chris@0: } Chris@0: $data[$entity_info->getBaseTable()][$entity_type . '_bulk_form'] = [ Chris@0: 'title' => t('Bulk update'), Chris@0: 'help' => t('Allows users to apply an action to one or more items.'), Chris@0: 'field' => [ Chris@0: 'id' => 'bulk_form', Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: Chris@0: // Registers views data for the entity itself. Chris@0: foreach (\Drupal::entityManager()->getDefinitions() as $entity_type_id => $entity_type) { Chris@0: if ($entity_type->hasHandlerClass('views_data')) { Chris@0: /** @var \Drupal\views\EntityViewsDataInterface $views_data */ Chris@0: $views_data = \Drupal::entityManager()->getHandler($entity_type_id, 'views_data'); Chris@0: $data = NestedArray::mergeDeep($data, $views_data->getViewsData()); Chris@0: } Chris@0: } Chris@0: Chris@0: // Field modules can implement hook_field_views_data() to override the default Chris@0: // behavior for adding fields. Chris@0: $module_handler = \Drupal::moduleHandler(); Chris@0: Chris@0: $entity_manager = \Drupal::entityManager(); Chris@0: if ($entity_manager->hasDefinition('field_storage_config')) { Chris@0: /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */ Chris@0: foreach ($entity_manager->getStorage('field_storage_config')->loadMultiple() as $field_storage) { Chris@0: if (_views_field_get_entity_type_storage($field_storage)) { Chris@0: $result = (array) $module_handler->invoke($field_storage->getTypeProvider(), 'field_views_data', [$field_storage]); Chris@0: if (empty($result)) { Chris@0: $result = views_field_default_views_data($field_storage); Chris@0: } Chris@0: $module_handler->alter('field_views_data', $result, $field_storage); Chris@0: Chris@0: if (is_array($result)) { Chris@0: $data = NestedArray::mergeDeep($result, $data); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return $data; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_views_data_alter(). Chris@0: * Chris@0: * Field modules can implement hook_field_views_data_views_data_alter() to Chris@0: * alter the views data on a per field basis. This is weirdly named so as Chris@0: * not to conflict with the \Drupal::moduleHandler()->alter('field_views_data') Chris@0: * in views_views_data(). Chris@0: */ Chris@0: function views_views_data_alter(&$data) { Chris@0: $entity_manager = \Drupal::entityManager(); Chris@0: if (!$entity_manager->hasDefinition('field_storage_config')) { Chris@0: return; Chris@0: } Chris@0: /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */ Chris@0: foreach ($entity_manager->getStorage('field_storage_config')->loadMultiple() as $field_storage) { Chris@0: if (_views_field_get_entity_type_storage($field_storage)) { Chris@0: $function = $field_storage->getTypeProvider() . '_field_views_data_views_data_alter'; Chris@0: if (function_exists($function)) { Chris@0: $function($data, $field_storage); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Determines whether the entity type the field appears in is SQL based. Chris@0: * Chris@0: * @param \Drupal\field\FieldStorageConfigInterface $field_storage Chris@0: * The field storage definition. Chris@0: * Chris@0: * @return \Drupal\Core\Entity\Sql\SqlContentEntityStorage Chris@0: * Returns the entity type storage if supported. Chris@0: */ Chris@0: function _views_field_get_entity_type_storage(FieldStorageConfigInterface $field_storage) { Chris@0: $result = FALSE; Chris@0: $entity_manager = \Drupal::entityManager(); Chris@0: if ($entity_manager->hasDefinition($field_storage->getTargetEntityTypeId())) { Chris@0: $storage = $entity_manager->getStorage($field_storage->getTargetEntityTypeId()); Chris@0: $result = $storage instanceof SqlContentEntityStorage ? $storage : FALSE; Chris@0: } Chris@0: return $result; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the label of a certain field. Chris@0: * Chris@0: * Therefore it looks up in all bundles to find the most used field. Chris@0: */ Chris@0: function views_entity_field_label($entity_type, $field_name) { Chris@0: $label_counter = []; Chris@0: $all_labels = []; Chris@0: // Count the amount of fields per label per field storage. Chris@18: foreach (array_keys(\Drupal::service('entity_type.bundle.info')->getBundleInfo($entity_type)) as $bundle) { Chris@0: $bundle_fields = array_filter(\Drupal::entityManager()->getFieldDefinitions($entity_type, $bundle), function ($field_definition) { Chris@0: return $field_definition instanceof FieldConfigInterface; Chris@0: }); Chris@0: if (isset($bundle_fields[$field_name])) { Chris@0: $field = $bundle_fields[$field_name]; Chris@0: $label = $field->getLabel(); Chris@0: $label_counter[$label] = isset($label_counter[$label]) ? ++$label_counter[$label] : 1; Chris@0: $all_labels[$label] = TRUE; Chris@0: } Chris@0: } Chris@0: if (empty($label_counter)) { Chris@0: return [$field_name, $all_labels]; Chris@0: } Chris@0: // Sort the field labels by it most used label and return the most used one. Chris@0: // If the counts are equal, sort by the label to ensure the result is Chris@0: // deterministic. Chris@0: uksort($label_counter, function ($a, $b) use ($label_counter) { Chris@0: if ($label_counter[$a] === $label_counter[$b]) { Chris@0: return strcmp($a, $b); Chris@0: } Chris@0: return $label_counter[$a] > $label_counter[$b] ? -1 : 1; Chris@0: }); Chris@0: $label_counter = array_keys($label_counter); Chris@0: return [$label_counter[0], $all_labels]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Default views data implementation for a field. Chris@0: * Chris@0: * @param \Drupal\field\FieldStorageConfigInterface $field_storage Chris@0: * The field definition. Chris@0: * Chris@0: * @return array Chris@0: * The default views data for the field. Chris@0: */ Chris@0: function views_field_default_views_data(FieldStorageConfigInterface $field_storage) { Chris@0: $data = []; Chris@0: Chris@0: // Check the field type is available. Chris@0: if (!\Drupal::service('plugin.manager.field.field_type')->hasDefinition($field_storage->getType())) { Chris@0: return $data; Chris@0: } Chris@0: // Check the field storage has fields. Chris@0: if (!$field_storage->getBundles()) { Chris@0: return $data; Chris@0: } Chris@0: Chris@0: // Ignore custom storage too. Chris@0: if ($field_storage->hasCustomStorage()) { Chris@0: return $data; Chris@0: } Chris@0: Chris@0: // Check whether the entity type storage is supported. Chris@0: $storage = _views_field_get_entity_type_storage($field_storage); Chris@0: if (!$storage) { Chris@0: return $data; Chris@0: } Chris@0: Chris@0: $field_name = $field_storage->getName(); Chris@0: $field_columns = $field_storage->getColumns(); Chris@0: Chris@0: // Grab information about the entity type tables. Chris@0: // We need to join to both the base table and the data table, if available. Chris@0: $entity_manager = \Drupal::entityManager(); Chris@0: $entity_type_id = $field_storage->getTargetEntityTypeId(); Chris@0: $entity_type = $entity_manager->getDefinition($entity_type_id); Chris@0: if (!$base_table = $entity_type->getBaseTable()) { Chris@0: // We cannot do anything if for some reason there is no base table. Chris@0: return $data; Chris@0: } Chris@0: $entity_tables = [$base_table => $entity_type_id]; Chris@0: // Some entities may not have a data table. Chris@0: $data_table = $entity_type->getDataTable(); Chris@0: if ($data_table) { Chris@0: $entity_tables[$data_table] = $entity_type_id; Chris@0: } Chris@0: $entity_revision_table = $entity_type->getRevisionTable(); Chris@0: $supports_revisions = $entity_type->hasKey('revision') && $entity_revision_table; Chris@0: if ($supports_revisions) { Chris@0: $entity_tables[$entity_revision_table] = $entity_type_id; Chris@0: $entity_revision_data_table = $entity_type->getRevisionDataTable(); Chris@0: if ($entity_revision_data_table) { Chris@0: $entity_tables[$entity_revision_data_table] = $entity_type_id; Chris@0: } Chris@0: } Chris@0: Chris@0: // Description of the field tables. Chris@0: // @todo Generalize this code to make it work with any table layout. See Chris@0: // https://www.drupal.org/node/2079019. Chris@0: $table_mapping = $storage->getTableMapping(); Chris@0: $field_tables = [ Chris@0: EntityStorageInterface::FIELD_LOAD_CURRENT => [ Chris@0: 'table' => $table_mapping->getDedicatedDataTableName($field_storage), Chris@0: 'alias' => "{$entity_type_id}__{$field_name}", Chris@0: ], Chris@0: ]; Chris@0: if ($supports_revisions) { Chris@0: $field_tables[EntityStorageInterface::FIELD_LOAD_REVISION] = [ Chris@0: 'table' => $table_mapping->getDedicatedRevisionTableName($field_storage), Chris@0: 'alias' => "{$entity_type_id}_revision__{$field_name}", Chris@0: ]; Chris@0: } Chris@0: Chris@0: // Determine if the fields are translatable. Chris@0: $bundles_names = $field_storage->getBundles(); Chris@0: $translation_join_type = FALSE; Chris@0: $fields = []; Chris@0: $translatable_configs = []; Chris@0: $untranslatable_configs = []; Chris@0: $untranslatable_config_bundles = []; Chris@0: Chris@0: foreach ($bundles_names as $bundle) { Chris@0: $fields[$bundle] = FieldConfig::loadByName($entity_type->id(), $bundle, $field_name); Chris@0: } Chris@0: foreach ($fields as $bundle => $config_entity) { Chris@0: if (!empty($config_entity)) { Chris@0: if ($config_entity->isTranslatable()) { Chris@0: $translatable_configs[$bundle] = $config_entity; Chris@0: } Chris@0: else { Chris@0: $untranslatable_configs[$bundle] = $config_entity; Chris@0: } Chris@0: } Chris@0: else { Chris@0: // https://www.drupal.org/node/2451657#comment-11462881 Chris@0: \Drupal::logger('views')->error( Chris@17: 'A non-existent config entity name returned by FieldStorageConfigInterface::getBundles(): entity type: %entity_type, bundle: %bundle, field name: %field', Chris@17: [ Chris@17: '%entity_type' => $entity_type->id(), Chris@17: '%bundle' => $bundle, Chris@17: '%field' => $field_name, Chris@17: ] Chris@17: ); Chris@0: } Chris@0: } Chris@0: Chris@0: // If the field is translatable on all the bundles, there will be a join on Chris@0: // the langcode. Chris@0: if (!empty($translatable_configs) && empty($untranslatable_configs)) { Chris@0: $translation_join_type = 'language'; Chris@0: } Chris@0: // If the field is translatable only on certain bundles, there will be a join Chris@0: // on langcode OR bundle name. Chris@0: elseif (!empty($translatable_configs) && !empty($untranslatable_configs)) { Chris@0: foreach ($untranslatable_configs as $config) { Chris@0: $untranslatable_config_bundles[] = $config->getTargetBundle(); Chris@0: } Chris@0: $translation_join_type = 'language_bundle'; Chris@0: } Chris@0: Chris@0: // Build the relationships between the field table and the entity tables. Chris@0: $table_alias = $field_tables[EntityStorageInterface::FIELD_LOAD_CURRENT]['alias']; Chris@0: if ($data_table) { Chris@0: // Tell Views how to join to the base table, via the data table. Chris@0: $data[$table_alias]['table']['join'][$data_table] = [ Chris@0: 'table' => $table_mapping->getDedicatedDataTableName($field_storage), Chris@0: 'left_field' => $entity_type->getKey('id'), Chris@0: 'field' => 'entity_id', Chris@0: 'extra' => [ Chris@0: ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE], Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: else { Chris@0: // If there is no data table, just join directly. Chris@0: $data[$table_alias]['table']['join'][$base_table] = [ Chris@0: 'table' => $table_mapping->getDedicatedDataTableName($field_storage), Chris@0: 'left_field' => $entity_type->getKey('id'), Chris@0: 'field' => 'entity_id', Chris@0: 'extra' => [ Chris@0: ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE], Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: Chris@0: if ($translation_join_type === 'language_bundle') { Chris@0: $data[$table_alias]['table']['join'][$data_table]['join_id'] = 'field_or_language_join'; Chris@0: $data[$table_alias]['table']['join'][$data_table]['extra'][] = [ Chris@0: 'left_field' => 'langcode', Chris@0: 'field' => 'langcode', Chris@0: ]; Chris@0: $data[$table_alias]['table']['join'][$data_table]['extra'][] = [ Chris@0: 'field' => 'bundle', Chris@0: 'value' => $untranslatable_config_bundles, Chris@0: ]; Chris@0: } Chris@0: elseif ($translation_join_type === 'language') { Chris@0: $data[$table_alias]['table']['join'][$data_table]['extra'][] = [ Chris@0: 'left_field' => 'langcode', Chris@0: 'field' => 'langcode', Chris@0: ]; Chris@0: } Chris@0: Chris@0: if ($supports_revisions) { Chris@0: $table_alias = $field_tables[EntityStorageInterface::FIELD_LOAD_REVISION]['alias']; Chris@0: if ($entity_revision_data_table) { Chris@0: // Tell Views how to join to the revision table, via the data table. Chris@0: $data[$table_alias]['table']['join'][$entity_revision_data_table] = [ Chris@0: 'table' => $table_mapping->getDedicatedRevisionTableName($field_storage), Chris@0: 'left_field' => $entity_type->getKey('revision'), Chris@0: 'field' => 'revision_id', Chris@0: 'extra' => [ Chris@0: ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE], Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: else { Chris@0: // If there is no data table, just join directly. Chris@0: $data[$table_alias]['table']['join'][$entity_revision_table] = [ Chris@0: 'table' => $table_mapping->getDedicatedRevisionTableName($field_storage), Chris@0: 'left_field' => $entity_type->getKey('revision'), Chris@0: 'field' => 'revision_id', Chris@0: 'extra' => [ Chris@0: ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE], Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: if ($translation_join_type === 'language_bundle') { Chris@0: $data[$table_alias]['table']['join'][$entity_revision_data_table]['join_id'] = 'field_or_language_join'; Chris@0: $data[$table_alias]['table']['join'][$entity_revision_data_table]['extra'][] = [ Chris@0: 'left_field' => 'langcode', Chris@0: 'field' => 'langcode', Chris@0: ]; Chris@0: $data[$table_alias]['table']['join'][$entity_revision_data_table]['extra'][] = [ Chris@0: 'value' => $untranslatable_config_bundles, Chris@0: 'field' => 'bundle', Chris@0: ]; Chris@0: } Chris@0: elseif ($translation_join_type === 'language') { Chris@0: $data[$table_alias]['table']['join'][$entity_revision_data_table]['extra'][] = [ Chris@0: 'left_field' => 'langcode', Chris@0: 'field' => 'langcode', Chris@0: ]; Chris@0: } Chris@0: } Chris@0: Chris@0: $group_name = $entity_type->getLabel(); Chris@0: // Get the list of bundles the field appears in. Chris@0: $bundles_names = $field_storage->getBundles(); Chris@0: // Build the list of additional fields to add to queries. Chris@0: $add_fields = ['delta', 'langcode', 'bundle']; Chris@0: foreach (array_keys($field_columns) as $column) { Chris@0: $add_fields[] = $table_mapping->getFieldColumnName($field_storage, $column); Chris@0: } Chris@0: // Determine the label to use for the field. We don't have a label available Chris@0: // at the field level, so we just go through all fields and take the one Chris@0: // which is used the most frequently. Chris@0: list($label, $all_labels) = views_entity_field_label($entity_type_id, $field_name); Chris@0: Chris@0: // Expose data for the field as a whole. Chris@0: foreach ($field_tables as $type => $table_info) { Chris@0: $table = $table_info['table']; Chris@0: $table_alias = $table_info['alias']; Chris@0: Chris@0: if ($type == EntityStorageInterface::FIELD_LOAD_CURRENT) { Chris@0: $group = $group_name; Chris@0: $field_alias = $field_name; Chris@0: } Chris@0: else { Chris@0: $group = t('@group (historical data)', ['@group' => $group_name]); Chris@0: $field_alias = $field_name . '-revision_id'; Chris@0: } Chris@0: Chris@0: $data[$table_alias][$field_alias] = [ Chris@0: 'group' => $group, Chris@0: 'title' => $label, Chris@0: 'title short' => $label, Chris@0: 'help' => t('Appears in: @bundles.', ['@bundles' => implode(', ', $bundles_names)]), Chris@0: ]; Chris@0: Chris@0: // Go through and create a list of aliases for all possible combinations of Chris@0: // entity type + name. Chris@0: $aliases = []; Chris@0: $also_known = []; Chris@0: foreach ($all_labels as $label_name => $true) { Chris@0: if ($type == EntityStorageInterface::FIELD_LOAD_CURRENT) { Chris@0: if ($label != $label_name) { Chris@0: $aliases[] = [ Chris@0: 'base' => $base_table, Chris@0: 'group' => $group_name, Chris@0: 'title' => $label_name, Chris@0: 'help' => t('This is an alias of @group: @field.', ['@group' => $group_name, '@field' => $label]), Chris@0: ]; Chris@0: $also_known[] = t('@group: @field', ['@group' => $group_name, '@field' => $label_name]); Chris@0: } Chris@0: } Chris@0: elseif ($supports_revisions && $label != $label_name) { Chris@0: $aliases[] = [ Chris@0: 'base' => $table, Chris@0: 'group' => t('@group (historical data)', ['@group' => $group_name]), Chris@0: 'title' => $label_name, Chris@0: 'help' => t('This is an alias of @group: @field.', ['@group' => $group_name, '@field' => $label]), Chris@0: ]; Chris@0: $also_known[] = t('@group (historical data): @field', ['@group' => $group_name, '@field' => $label_name]); Chris@0: } Chris@0: } Chris@0: if ($aliases) { Chris@0: $data[$table_alias][$field_alias]['aliases'] = $aliases; Chris@0: // The $also_known variable contains markup that is HTML escaped and that Chris@0: // loses safeness when imploded. The help text is used in #description Chris@0: // and therefore XSS admin filtered by default. Escaped HTML is not Chris@0: // altered by XSS filtering, therefore it is safe to just concatenate the Chris@0: // strings. Afterwards we mark the entire string as safe, so it won't be Chris@0: // escaped, no matter where it is used. Chris@0: // Considering the dual use of this help data (both as metadata and as Chris@0: // help text), other patterns such as use of #markup would not be correct Chris@0: // here. Chris@0: $data[$table_alias][$field_alias]['help'] = Markup::create($data[$table_alias][$field_alias]['help'] . ' ' . t('Also known as:') . ' ' . implode(', ', $also_known)); Chris@0: } Chris@0: Chris@0: $keys = array_keys($field_columns); Chris@0: $real_field = reset($keys); Chris@0: $data[$table_alias][$field_alias]['field'] = [ Chris@0: 'table' => $table, Chris@0: 'id' => 'field', Chris@0: 'field_name' => $field_name, Chris@0: 'entity_type' => $entity_type_id, Chris@0: // Provide a real field for group by. Chris@0: 'real field' => $field_alias . '_' . $real_field, Chris@0: 'additional fields' => $add_fields, Chris@0: // Default the element type to div, let the UI change it if necessary. Chris@0: 'element type' => 'div', Chris@0: 'is revision' => $type == EntityStorageInterface::FIELD_LOAD_REVISION, Chris@0: ]; Chris@0: } Chris@0: Chris@0: // Expose data for each field property individually. Chris@0: foreach ($field_columns as $column => $attributes) { Chris@0: $allow_sort = TRUE; Chris@0: Chris@0: // Identify likely filters and arguments for each column based on field type. Chris@0: switch ($attributes['type']) { Chris@0: case 'int': Chris@0: case 'mediumint': Chris@0: case 'tinyint': Chris@0: case 'bigint': Chris@0: case 'serial': Chris@0: case 'numeric': Chris@0: case 'float': Chris@0: $filter = 'numeric'; Chris@0: $argument = 'numeric'; Chris@0: $sort = 'standard'; Chris@0: if ($field_storage->getType() == 'boolean') { Chris@0: $filter = 'boolean'; Chris@0: } Chris@0: break; Chris@0: case 'blob': Chris@18: // It does not make sense to sort by blob. Chris@0: $allow_sort = FALSE; Chris@0: default: Chris@0: $filter = 'string'; Chris@0: $argument = 'string'; Chris@0: $sort = 'standard'; Chris@0: break; Chris@0: } Chris@0: Chris@0: if (count($field_columns) == 1 || $column == 'value') { Chris@0: $title = t('@label (@name)', ['@label' => $label, '@name' => $field_name]); Chris@0: $title_short = $label; Chris@0: } Chris@0: else { Chris@0: $title = t('@label (@name:@column)', ['@label' => $label, '@name' => $field_name, '@column' => $column]); Chris@0: $title_short = t('@label:@column', ['@label' => $label, '@column' => $column]); Chris@0: } Chris@0: Chris@0: // Expose data for the property. Chris@0: foreach ($field_tables as $type => $table_info) { Chris@0: $table = $table_info['table']; Chris@0: $table_alias = $table_info['alias']; Chris@0: Chris@0: if ($type == EntityStorageInterface::FIELD_LOAD_CURRENT) { Chris@0: $group = $group_name; Chris@0: } Chris@0: else { Chris@0: $group = t('@group (historical data)', ['@group' => $group_name]); Chris@0: } Chris@0: $column_real_name = $table_mapping->getFieldColumnName($field_storage, $column); Chris@0: Chris@0: // Load all the fields from the table by default. Chris@0: $additional_fields = $table_mapping->getAllColumns($table); Chris@0: Chris@0: $data[$table_alias][$column_real_name] = [ Chris@0: 'group' => $group, Chris@0: 'title' => $title, Chris@0: 'title short' => $title_short, Chris@0: 'help' => t('Appears in: @bundles.', ['@bundles' => implode(', ', $bundles_names)]), Chris@0: ]; Chris@0: Chris@0: // Go through and create a list of aliases for all possible combinations of Chris@0: // entity type + name. Chris@0: $aliases = []; Chris@0: $also_known = []; Chris@0: foreach ($all_labels as $label_name => $true) { Chris@0: if ($label != $label_name) { Chris@0: if (count($field_columns) == 1 || $column == 'value') { Chris@0: $alias_title = t('@label (@name)', ['@label' => $label_name, '@name' => $field_name]); Chris@0: } Chris@0: else { Chris@0: $alias_title = t('@label (@name:@column)', ['@label' => $label_name, '@name' => $field_name, '@column' => $column]); Chris@0: } Chris@0: $aliases[] = [ Chris@0: 'group' => $group_name, Chris@0: 'title' => $alias_title, Chris@0: 'help' => t('This is an alias of @group: @field.', ['@group' => $group_name, '@field' => $title]), Chris@0: ]; Chris@0: $also_known[] = t('@group: @field', ['@group' => $group_name, '@field' => $title]); Chris@0: } Chris@0: } Chris@0: if ($aliases) { Chris@0: $data[$table_alias][$column_real_name]['aliases'] = $aliases; Chris@0: // The $also_known variable contains markup that is HTML escaped and Chris@0: // that loses safeness when imploded. The help text is used in Chris@0: // #description and therefore XSS admin filtered by default. Escaped Chris@0: // HTML is not altered by XSS filtering, therefore it is safe to just Chris@0: // concatenate the strings. Afterwards we mark the entire string as Chris@0: // safe, so it won't be escaped, no matter where it is used. Chris@0: // Considering the dual use of this help data (both as metadata and as Chris@0: // help text), other patterns such as use of #markup would not be Chris@0: // correct here. Chris@0: $data[$table_alias][$column_real_name]['help'] = Markup::create($data[$table_alias][$column_real_name]['help'] . ' ' . t('Also known as:') . ' ' . implode(', ', $also_known)); Chris@0: } Chris@0: Chris@0: $data[$table_alias][$column_real_name]['argument'] = [ Chris@0: 'field' => $column_real_name, Chris@0: 'table' => $table, Chris@0: 'id' => $argument, Chris@0: 'additional fields' => $additional_fields, Chris@0: 'field_name' => $field_name, Chris@0: 'entity_type' => $entity_type_id, Chris@0: 'empty field name' => t('- No value -'), Chris@0: ]; Chris@0: $data[$table_alias][$column_real_name]['filter'] = [ Chris@0: 'field' => $column_real_name, Chris@0: 'table' => $table, Chris@0: 'id' => $filter, Chris@0: 'additional fields' => $additional_fields, Chris@0: 'field_name' => $field_name, Chris@0: 'entity_type' => $entity_type_id, Chris@0: 'allow empty' => TRUE, Chris@0: ]; Chris@0: if (!empty($allow_sort)) { Chris@0: $data[$table_alias][$column_real_name]['sort'] = [ Chris@0: 'field' => $column_real_name, Chris@0: 'table' => $table, Chris@0: 'id' => $sort, Chris@0: 'additional fields' => $additional_fields, Chris@0: 'field_name' => $field_name, Chris@0: 'entity_type' => $entity_type_id, Chris@0: ]; Chris@0: } Chris@0: Chris@0: // Set click sortable if there is a field definition. Chris@0: if (isset($data[$table_alias][$field_name]['field'])) { Chris@0: $data[$table_alias][$field_name]['field']['click sortable'] = $allow_sort; Chris@0: } Chris@0: Chris@0: // Expose additional delta column for multiple value fields. Chris@0: if ($field_storage->isMultiple()) { Chris@0: $title_delta = t('@label (@name:delta)', ['@label' => $label, '@name' => $field_name]); Chris@0: $title_short_delta = t('@label:delta', ['@label' => $label]); Chris@0: Chris@0: $data[$table_alias]['delta'] = [ Chris@0: 'group' => $group, Chris@0: 'title' => $title_delta, Chris@0: 'title short' => $title_short_delta, Chris@0: 'help' => t('Delta - Appears in: @bundles.', ['@bundles' => implode(', ', $bundles_names)]), Chris@0: ]; Chris@0: $data[$table_alias]['delta']['field'] = [ Chris@0: 'id' => 'numeric', Chris@0: ]; Chris@0: $data[$table_alias]['delta']['argument'] = [ Chris@0: 'field' => 'delta', Chris@0: 'table' => $table, Chris@0: 'id' => 'numeric', Chris@0: 'additional fields' => $additional_fields, Chris@0: 'empty field name' => t('- No value -'), Chris@0: 'field_name' => $field_name, Chris@0: 'entity_type' => $entity_type_id, Chris@0: ]; Chris@0: $data[$table_alias]['delta']['filter'] = [ Chris@0: 'field' => 'delta', Chris@0: 'table' => $table, Chris@0: 'id' => 'numeric', Chris@0: 'additional fields' => $additional_fields, Chris@0: 'field_name' => $field_name, Chris@0: 'entity_type' => $entity_type_id, Chris@0: 'allow empty' => TRUE, Chris@0: ]; Chris@0: $data[$table_alias]['delta']['sort'] = [ Chris@0: 'field' => 'delta', Chris@0: 'table' => $table, Chris@0: 'id' => 'standard', Chris@0: 'additional fields' => $additional_fields, Chris@0: 'field_name' => $field_name, Chris@0: 'entity_type' => $entity_type_id, Chris@0: ]; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return $data; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_field_views_data(). Chris@0: * Chris@0: * The function implements the hook in behalf of 'core' because it adds a Chris@0: * relationship and a reverse relationship to entity_reference field type, which Chris@0: * is provided by core. Chris@0: */ Chris@0: function core_field_views_data(FieldStorageConfigInterface $field_storage) { Chris@0: $data = views_field_default_views_data($field_storage); Chris@0: Chris@0: // The code below only deals with the Entity reference field type. Chris@0: if ($field_storage->getType() != 'entity_reference') { Chris@0: return $data; Chris@0: } Chris@0: Chris@0: $entity_manager = \Drupal::entityManager(); Chris@0: $entity_type_id = $field_storage->getTargetEntityTypeId(); Chris@0: /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ Chris@0: $table_mapping = $entity_manager->getStorage($entity_type_id)->getTableMapping(); Chris@0: Chris@0: foreach ($data as $table_name => $table_data) { Chris@0: // Add a relationship to the target entity type. Chris@0: $target_entity_type_id = $field_storage->getSetting('target_type'); Chris@0: $target_entity_type = $entity_manager->getDefinition($target_entity_type_id); Chris@0: $entity_type_id = $field_storage->getTargetEntityTypeId(); Chris@0: $entity_type = $entity_manager->getDefinition($entity_type_id); Chris@0: $target_base_table = $target_entity_type->getDataTable() ?: $target_entity_type->getBaseTable(); Chris@0: $field_name = $field_storage->getName(); Chris@0: Chris@0: // Provide a relationship for the entity type with the entity reference Chris@0: // field. Chris@0: $args = [ Chris@0: '@label' => $target_entity_type->getLabel(), Chris@0: '@field_name' => $field_name, Chris@0: ]; Chris@0: $data[$table_name][$field_name]['relationship'] = [ Chris@0: 'title' => t('@label referenced from @field_name', $args), Chris@0: 'label' => t('@field_name: @label', $args), Chris@0: 'group' => $entity_type->getLabel(), Chris@0: 'help' => t('Appears in: @bundles.', ['@bundles' => implode(', ', $field_storage->getBundles())]), Chris@0: 'id' => 'standard', Chris@0: 'base' => $target_base_table, Chris@0: 'entity type' => $target_entity_type_id, Chris@0: 'base field' => $target_entity_type->getKey('id'), Chris@0: 'relationship field' => $field_name . '_target_id', Chris@0: ]; Chris@0: Chris@0: // Provide a reverse relationship for the entity type that is referenced by Chris@0: // the field. Chris@0: $args['@entity'] = $entity_type->getLabel(); Chris@0: $args['@label'] = $target_entity_type->getLowercaseLabel(); Chris@0: $pseudo_field_name = 'reverse__' . $entity_type_id . '__' . $field_name; Chris@0: $data[$target_base_table][$pseudo_field_name]['relationship'] = [ Chris@0: 'title' => t('@entity using @field_name', $args), Chris@0: 'label' => t('@field_name', ['@field_name' => $field_name]), Chris@0: 'group' => $target_entity_type->getLabel(), Chris@0: 'help' => t('Relate each @entity with a @field_name set to the @label.', $args), Chris@0: 'id' => 'entity_reverse', Chris@0: 'base' => $entity_type->getDataTable() ?: $entity_type->getBaseTable(), Chris@0: 'entity_type' => $entity_type_id, Chris@0: 'base field' => $entity_type->getKey('id'), Chris@0: 'field_name' => $field_name, Chris@0: 'field table' => $table_mapping->getDedicatedDataTableName($field_storage), Chris@0: 'field field' => $field_name . '_target_id', Chris@0: 'join_extra' => [ Chris@0: [ Chris@0: 'field' => 'deleted', Chris@0: 'value' => 0, Chris@0: 'numeric' => TRUE, Chris@0: ], Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: Chris@0: return $data; Chris@0: }