core_extend-8.x-1.0-alpha3/core_extend.module
core_extend.module
<?php
/**
* @file
* Contains core_extend.module.
*/
use Drupal\Core\Field\FieldStorageDefinitionInterface;
/**
* Implements hook_theme().
*/
function core_extend_theme() {
$theme = [];
$theme['core_extend_entity_form'] = [
'render element' => 'form',
];
return $theme;
}
/**
* Allow reverse relationships for base fields.
*/
function core_extend_entity_reference_add_views_reverse_relationship(array &$data, FieldStorageDefinitionInterface $field_storage) {
$entity_type_manager = \Drupal::entityTypeManager();
$field_name = $field_storage->getName();
$entity_type_id = $field_storage->getTargetEntityTypeId();
$entity_type = $entity_type_manager->getDefinition($entity_type_id);
/** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $entity_table_mapping */
$entity_table_mapping = $entity_type_manager->getStorage($entity_type_id)->getTableMapping();
$entity_base_table = $entity_type->getDataTable() ?: $entity_type->getBaseTable();
$target_entity_type_id = $field_storage->getSetting('target_type');
$target_entity_type = $entity_type_manager->getDefinition($target_entity_type_id);
$target_base_table = $target_entity_type->getDataTable() ?: $target_entity_type->getBaseTable();
// Provide a reverse relationship for the entity type that is referenced by
// the field.
$args = [
'@label' => $target_entity_type->getLowercaseLabel(),
'@entity' => $entity_type->getLabel(),
'@field_name' => $field_name,
];
$pseudo_field_name = 'reverse__' . $entity_type_id . '__' . $field_name;
if ($entity_table_mapping->requiresDedicatedTableStorage($field_storage)) {
$data[$target_base_table][$pseudo_field_name] = [
// There is a bridge table. Use the entity_reverse relationship plugin.
'relationship' => [
'title' => t('@entity using @field_name', $args),
'label' => t('@field_name', ['@field_name' => $field_name]),
'help' => t('Relate each @entity with a @field_name set to the @label.', $args),
'group' => $target_entity_type->getLabel(),
'id' => 'entity_reverse',
'base' => $entity_base_table,
'entity_type' => $entity_type_id,
'base field' => $entity_type->getKey('id'),
'field_name' => $field_name,
'field table' => $entity_table_mapping->getFieldTableName($field_name),
'field field' => $entity_table_mapping->getFieldColumnName($field_storage, 'target_id'),
],
];
}
else {
// The data is on the base table. Use the standard relationship plugin.
$data[$target_base_table][$pseudo_field_name] = [
'relationship' => [
'title' => t('@entity using @field_name', $args),
'label' => t('@field_name', ['@field_name' => $field_name]),
'help' => t('Relate each @entity with a @field_name set to the @label.', $args),
'group' => $target_entity_type->getLabel(),
'id' => 'standard',
'relationship field' => $data[$target_base_table]['table']['base']['field'],
'base' => $entity_base_table,
'base field' => $entity_table_mapping->getFieldColumnName($field_storage, 'target_id'),
'entity_type' => $entity_type_id,
],
];
}
}
