DependencyFieldMapGenerator.php in Entity Reference Integrity 8
File
src/DependencyFieldMapGenerator.php
View source
<?php
namespace Drupal\entity_reference_integrity;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class DependencyFieldMapGenerator implements DependencyFieldMapGeneratorInterface {
protected $entityFieldManager;
private $entityTypeManager;
protected $fieldType;
protected $targetEntitySettingsKey;
public function __construct(EntityFieldManagerInterface $entityFieldManager, EntityTypeManagerInterface $entityTypeManager, $fieldType, $targetEntitySettingsKey) {
$this->entityFieldManager = $entityFieldManager;
$this->entityTypeManager = $entityTypeManager;
$this->fieldType = $fieldType;
$this->targetEntitySettingsKey = $targetEntitySettingsKey;
}
public function getReferentialFieldMap() {
$referential_field_map = [];
foreach ($this->entityFieldManager
->getFieldMapByFieldType($this->fieldType) as $source_entity_type_id => $fields) {
$source_entity_storage_definitions = $this->entityFieldManager
->getFieldStorageDefinitions($source_entity_type_id);
$source_entity_type = $this->entityTypeManager
->getDefinition($source_entity_type_id);
foreach ($fields as $field_name => $field_data) {
$field_storage_definition = !empty($source_entity_storage_definitions[$field_name]) ? $source_entity_storage_definitions[$field_name] : FALSE;
if (!$field_storage_definition) {
continue;
}
if ($field_storage_definition
->hasCustomStorage()) {
continue;
}
if (in_array($field_name, $source_entity_type
->getRevisionMetadataKeys())) {
continue;
}
$target_entity_type_id = $source_entity_storage_definitions[$field_name]
->getSetting($this->targetEntitySettingsKey);
$referential_field_map[$target_entity_type_id][$source_entity_type_id][] = $field_name;
}
}
return $referential_field_map;
}
public function getReferencingFields($entity_type_id) {
$map = $this
->getReferentialFieldMap();
return isset($map[$entity_type_id]) ? $map[$entity_type_id] : [];
}
}