DomainEntityMapper.php in Domain Access Entity 8
File
src/DomainEntityMapper.php
View source
<?php
namespace Drupal\domain_entity;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldConfig;
class DomainEntityMapper {
const FIELD_NAME = 'domain_access';
const BEHAVIOR_AUTO = 'auto';
const BEHAVIOR_USER = 'user';
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
public function getEnabledEntityTypes() {
$result = [];
$types = $this
->getEntityTypes();
foreach ($types as $id => $type) {
if ($this
->loadFieldStorage($id)) {
$result[$id] = $type;
}
}
return $result;
}
public function getEntityTypes() {
$entity_types = $this->entityTypeManager
->getDefinitions();
$result = [];
foreach ($entity_types as $entity_type_id => $entity_type) {
if ($entity_type
->entityClassImplements(FieldableEntityInterface::class)) {
$result[$entity_type_id] = $entity_type;
}
}
return $result;
}
public function loadFieldStorage($entity_type_id) {
$storage = $this->entityTypeManager
->getStorage('field_storage_config');
return $storage
->load($entity_type_id . '.' . self::FIELD_NAME);
}
public function loadField($entity_type_id, $bundle) {
$storage = $this->entityTypeManager
->getStorage('field_config');
return $storage
->load($entity_type_id . '.' . $bundle . '.' . self::FIELD_NAME);
}
public function deleteFieldStorage($entity_type_id) {
$field_storage = $this
->loadFieldStorage($entity_type_id);
if ($field_storage) {
$field_storage
->delete();
}
}
public function addDomainField($entity_type, $bundle) {
$field_storage = $this
->createFieldStorage($entity_type);
$field = FieldConfig::loadByName($entity_type, $bundle, self::FIELD_NAME);
if (empty($field)) {
$field = [
'label' => 'Domain Access',
'description' => 'Select the affiliate domain(s). If nothing was selected: Affiliated to all domains.',
'bundle' => $bundle,
'required' => FALSE,
'field_storage' => $field_storage,
'default_value_callback' => 'domain_entity_field_default_domains',
];
$field = $this->entityTypeManager
->getStorage('field_config')
->create($field);
$field
->save();
$entity_form_display = $this->entityTypeManager
->getStorage('entity_form_display')
->load($entity_type . '.' . $bundle . '.default');
if ($entity_form_display) {
$entity_form_display
->setComponent(self::FIELD_NAME, [
'type' => 'options_buttons',
])
->save();
}
$entity_view_display = $this->entityTypeManager
->getStorage('entity_view_display')
->load($entity_type . '.' . $bundle . '.default');
if ($entity_view_display) {
$entity_view_display
->removeComponent(self::FIELD_NAME)
->save();
}
}
}
public function createFieldStorage($entity_type_id) {
if ($field_storage = $this
->loadFieldStorage($entity_type_id)) {
return $field_storage;
}
$storage = $this->entityTypeManager
->getStorage('field_storage_config');
$field_storage = $storage
->create([
'entity_type' => $entity_type_id,
'field_name' => self::FIELD_NAME,
'type' => 'entity_reference',
'persist_with_no_fields' => TRUE,
'locked' => FALSE,
]);
$field_storage
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setSetting('target_type', 'domain')
->save();
return $field_storage;
}
}