View source
<?php
namespace Drupal\cms_content_sync\Plugin\cms_content_sync\field_handler;
use Drupal\cms_content_sync\Entity\EntityStatus;
use Drupal\cms_content_sync\Plugin\EntityReferenceHandlerBase;
use Drupal\Core\Config\Entity\ConfigEntityStorage;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use EdgeBox\SyncCore\Interfaces\Configuration\IDefineEntityType;
class DefaultEntityReferenceHandler extends EntityReferenceHandlerBase {
public const SUPPORTED_CONFIG_ENTITY_TYPES = [
'block',
'classy_paragraphs_style',
'view',
];
public static function supports($entity_type, $bundle, $field_name, FieldDefinitionInterface $field) {
$supported = [
'entity_reference',
'entity_reference_revisions',
'bibcite_contributor',
'viewsreference',
'dynamic_entity_reference',
];
if (!in_array($field
->getType(), $supported)) {
return false;
}
$types = EntityReferenceHandlerBase::getReferencedEntityTypesFromFieldDefinition($field);
foreach ($types as $type) {
if (in_array($type, [
'user',
'brick_type',
'paragraph',
])) {
return false;
}
if ('menu_link' == $field_name) {
return false;
}
$referenced_entity_type = \Drupal::entityTypeManager()
->getStorage($type);
if ($referenced_entity_type instanceof ConfigEntityStorage && !in_array($type, self::SUPPORTED_CONFIG_ENTITY_TYPES)) {
return false;
}
}
return true;
}
public static function saveEmbeddedPushToPools(EntityInterface $entity, $parent_entity = null, $tree_position = []) {
$entityTypeManager = \Drupal::entityTypeManager();
$entityFieldManager = \Drupal::service('entity_field.manager');
$fields = $entityFieldManager
->getFieldDefinitions($entity
->getEntityTypeId(), $entity
->bundle());
foreach ($fields as $name => $definition) {
if ('entity_reference_revisions' == $definition
->getType()) {
$reference_type = $definition
->getFieldStorageDefinition()
->getPropertyDefinition('entity')
->getTargetDefinition()
->getEntityTypeId();
$storage = $entityTypeManager
->getStorage($reference_type);
$data = $entity
->get($name)
->getValue();
foreach ($data as $delta => $value) {
if (empty($value['target_id'])) {
continue;
}
$target_id = $value['target_id'];
$reference = $storage
->load($target_id);
if (!$reference) {
continue;
}
if (!empty($value['subform']['cms_content_sync_group'])) {
$set = $value['subform']['cms_content_sync_group'];
EntityStatus::accessTemporaryPushToPoolInfoForField($entity
->getEntityTypeId(), $entity
->uuid(), $name, $delta, $tree_position, $set['cms_content_sync_flow'], $set['cms_content_sync_pool'], !empty($set['cms_content_sync_uuid']) ? $set['cms_content_sync_uuid'] : null);
}
EntityStatus::saveSelectedPushToPoolForField($entity, $name, $delta, $reference, $tree_position);
self::saveEmbeddedPushToPools($reference, $entity, array_merge($tree_position, [
$name,
$delta,
'subform',
]));
}
}
}
}
public function definePropertyAtType(IDefineEntityType $type_definition) {
$type_definition
->addReferenceProperty($this->fieldName, $this->fieldDefinition
->getLabel(), true, $this->fieldDefinition
->isRequired());
}
protected function allowSubscribeFilter() {
$type = $this->fieldDefinition
->getSetting('target_type');
return 'taxonomy_term' == $type;
}
protected function getInvalidSubfields() {
return [
'_accessCacheability',
'_attributes',
'_loaded',
'top',
'target_revision_id',
'subform',
];
}
}