abstract class EntityReferenceHandlerBase in CMS Content Sync 8
Same name and namespace in other branches
- 2.1.x src/Plugin/EntityReferenceHandlerBase.php \Drupal\cms_content_sync\Plugin\EntityReferenceHandlerBase
- 2.0.x src/Plugin/EntityReferenceHandlerBase.php \Drupal\cms_content_sync\Plugin\EntityReferenceHandlerBase
Providing a base implementation for any reference field type.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\cms_content_sync\Plugin\FieldHandlerBase implements FieldHandlerInterface, ContainerFactoryPluginInterface
- class \Drupal\cms_content_sync\Plugin\EntityReferenceHandlerBase
- class \Drupal\cms_content_sync\Plugin\FieldHandlerBase implements FieldHandlerInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of EntityReferenceHandlerBase
2 files declare their use of EntityReferenceHandlerBase
- DefaultEntityReferenceHandler.php in src/
Plugin/ cms_content_sync/ field_handler/ DefaultEntityReferenceHandler.php - DefaultWebformHandler.php in src/
Plugin/ cms_content_sync/ field_handler/ DefaultWebformHandler.php
File
- src/
Plugin/ EntityReferenceHandlerBase.php, line 20
Namespace
Drupal\cms_content_sync\PluginView source
abstract class EntityReferenceHandlerBase extends FieldHandlerBase {
/**
* {@inheritdoc}
*/
public function getHandlerSettings($current_values, $type = 'both') {
$options = [];
// Will be added in an upcoming release and recommended for paragraphs
// and bricks.
// Other entity types like media or taxonomy can use this as a performance
// improvement as well.
/*if(!$this->forceReferencedEntityEmbedding()) {
$options = [
'embed_referenced_entities' => [
'#type' => 'checkbox',
'#title' => 'Embed referenced entities',
'#default_value' => $this->shouldEmbedReferencedEntities(),
],
];
}*/
$referenced_entity_types = $this
->getReferencedEntityTypes();
if (!$this
->forcePushingReferencedEntities() && !$this
->forceEmbeddingReferencedEntities() && 'pull' !== $type && !in_array('view', $referenced_entity_types) && !in_array('classy_paragraphs_style', $referenced_entity_types)) {
$options['export_referenced_entities'] = [
'#type' => 'checkbox',
'#title' => 'Push referenced entities',
'#default_value' => isset($current_values['export_referenced_entities']) ? $current_values['export_referenced_entities'] : $this
->shouldPushReferencedEntities(true),
];
}
if ($this
->allowSubscribeFilter() && $this->flow && 'push' !== $type) {
$type = $this->fieldDefinition
->getSetting('target_type');
$bundles = $this->fieldDefinition
->getSetting('target_bundles');
if (!$bundles) {
$field_settings = $this->fieldDefinition
->getSettings();
if (isset($field_settings['handler_settings']['target_bundles'])) {
$bundles = $field_settings['handler_settings']['target_bundles'];
}
}
global $config;
$config_key = $this->entityTypeName . '-' . $this->bundleName . '-' . $this->fieldName;
$disabled = !empty($config['cms_content_sync.flow.' . $this->flow
->id()]['sync_entities'][$config_key]['handler_settings']['subscribe_only_to']);
$entities = [];
$current = $disabled ? $config['cms_content_sync.flow.' . $this->flow
->id()]['sync_entities'][$config_key]['handler_settings']['subscribe_only_to'] : (empty($current_values['subscribe_only_to']) ? null : $current_values['subscribe_only_to']);
if (!empty($current)) {
$storage = \Drupal::entityTypeManager()
->getStorage($type);
$repository = \Drupal::service('entity.repository');
foreach ($current as $ref) {
$entity = null;
if (isset($ref['uuid'])) {
$entity = $repository
->loadEntityByUuid($ref['type'], $ref['uuid']);
}
elseif (isset($ref['target_id'])) {
$entity = $storage
->load($ref['target_id']);
}
if ($entity) {
$entities[] = $entity;
}
}
}
$options['subscribe_only_to'] = [
'#type' => 'entity_autocomplete',
// The textfield component that the autocomplete inherits from sets this to 128 by default. We have no
// restriction, so we set this to a very high number that can allow 100 terms.
'#maxlength' => 4096,
'#size' => 30,
'#target_type' => $type,
'#tags' => true,
'#selection_settings' => [
'target_bundles' => $bundles,
],
'#title' => 'Subscribe only to',
'#disabled' => $disabled,
'#description' => $disabled ? $this
->t('Value provided via settings.php.') : '',
'#default_value' => $entities,
];
}
return $options;
}
/**
* {@inheritdoc}
*/
public function validateHandlerSettings(array &$form, FormStateInterface $form_state, $settings_key, $current_values) {
if (!$this
->shouldPushReferencedEntities() && !$this
->shouldEmbedReferencedEntities()) {
return;
}
$reference_types = $this
->getReferencedEntityTypes();
foreach ($current_values['sync_entities'] as $key => $config) {
// Ignore field definitions.
if (1 != substr_count($key, '-')) {
continue;
}
// Ignore ignored configs.
if (Flow::HANDLER_IGNORE == $config['handler']) {
continue;
}
list($entity_type_id) = explode('-', $key);
$index = array_search($entity_type_id, $reference_types);
// Ignore configs that don't match our entity type.
if (false === $index) {
continue;
}
// One has an push handler, so we can ignore this.
unset($reference_types[$index]);
}
if (!count($reference_types)) {
return;
}
// We are just about to load this element, so we don't have any form element available yet. Validation will be
// triggered again when the form is submitted.
if (empty($form[$this->entityTypeName][$this->bundleName]['fields'][$settings_key]['handler'])) {
return;
}
// No fitting handler was found- inform the user that he's missing some
// configuration.
if ($this
->forcePushingReferencedEntities() || $this
->forceEmbeddingReferencedEntities()) {
$element =& $form[$this->entityTypeName][$this->bundleName]['fields'][$settings_key]['handler'];
}
else {
$element =& $form[$this->entityTypeName][$this->bundleName]['fields'][$settings_key]['handler_settings']['export_referenced_entities'];
}
foreach ($reference_types as $type) {
$form_state
->setError($element, t('You want to push %referenced\'s that are referenced in %source automatically, but you have not defined any handler for this entity type. Please scroll to the bundles of this entity type, add a handler and set "push" to "referenced" there.', [
'%referenced' => $type,
'%source' => $settings_key,
]));
}
}
/**
* @param $fieldDefinition
*
* @return array
*/
public static function getReferencedEntityTypesFromFieldDefinition(FieldDefinitionInterface $fieldDefinition) {
if ('dynamic_entity_reference' == $fieldDefinition
->getFieldStorageDefinition()
->getType()) {
if ($fieldDefinition
->getFieldStorageDefinition()
->getSetting('exclude_entity_types')) {
$entity_types = EntityHandlerPluginManager::getEntityTypes();
$included = [];
$excluded = $fieldDefinition
->getFieldStorageDefinition()
->getSetting('entity_type_ids');
foreach ($entity_types as $entity_type) {
if (!in_array($entity_type['entity_type'], $excluded)) {
$included[] = $entity_type['entity_type'];
}
}
return $included;
}
return $fieldDefinition
->getFieldStorageDefinition()
->getSetting('entity_type_ids');
}
$reference_type = $fieldDefinition
->getFieldStorageDefinition()
->getPropertyDefinition('entity')
->getTargetDefinition()
->getEntityTypeId();
return [
$reference_type,
];
}
/**
* {@inheritdoc}
*/
public function pull(PullIntent $intent) {
$action = $intent
->getAction();
// Deletion doesn't require any action on field basis for static data.
if (SyncIntent::ACTION_DELETE == $action) {
return false;
}
return $this
->setValues($intent);
}
/**
* {@inheritdoc}
*/
public function push(PushIntent $intent) {
$action = $intent
->getAction();
/**
* @var \Drupal\Core\Entity\EntityInterface $entity
*/
$entity = $intent
->getEntity();
// Deletion doesn't require any action on field basis for static data.
if (SyncIntent::ACTION_DELETE == $action) {
return false;
}
$data = $entity
->get($this->fieldName)
->getValue();
$result = [];
foreach ($data as $delta => $value) {
$reference = $this
->loadReferencedEntityFromFieldValue($value);
if (!$reference || $reference
->uuid() == $intent
->getUuid()) {
continue;
}
unset($value['target_id']);
$result[] = $this
->serializeReference($intent, $reference, $value);
}
$intent
->setProperty($this->fieldName, $result);
return true;
}
/**
* Don't expose option, but force push.
*
* @return bool
*/
protected function forcePushingReferencedEntities() {
return false;
}
/**
* Don't expose option, but force push.
*
* @return bool
*/
protected function forceEmbeddingReferencedEntities() {
return false;
}
/**
* Check if referenced entities should be embedded automatically.
*
* @param bool $default
* Whether to get the default value (TRUE) if none is set
* yet
*
* @return bool
*/
protected function shouldEmbedReferencedEntities($default = false) {
if ($this
->forceEmbeddingReferencedEntities()) {
return true;
}
if (isset($this->settings['handler_settings']['embed_referenced_entities'])) {
return (bool) $this->settings['handler_settings']['embed_referenced_entities'];
}
if ($default) {
return true;
}
return false;
}
/**
* Check if referenced entities should be pushed automatically.
*
* @param bool $default
* Whether to get the default value (TRUE) if none is set
* yet
*
* @return bool
*/
protected function shouldPushReferencedEntities($default = false) {
// Not syndicating views.
$getReferencedEntityTypes = $this
->getReferencedEntityTypes();
if (in_array('view', $getReferencedEntityTypes)) {
return false;
}
if ($this
->forcePushingReferencedEntities()) {
return true;
}
if (isset($this->settings['handler_settings']['export_referenced_entities'])) {
return (bool) $this->settings['handler_settings']['export_referenced_entities'];
}
if ($default) {
return true;
}
return false;
}
/**
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*
* @return bool
*/
protected function allowPushingReferencedEntities() {
$referenced_entity_types = \Drupal::entityTypeManager()
->getStorage($this
->getReferencedEntityTypes());
foreach ($referenced_entity_types as $referenced_entity_type) {
if ($referenced_entity_type instanceof ConfigEntityStorage) {
return false;
}
}
return true;
}
/**
* @return bool
*/
protected function allowSubscribeFilter() {
return false;
}
/**
* @return string[]
*/
protected function getReferencedEntityTypes() {
return self::getReferencedEntityTypesFromFieldDefinition($this->fieldDefinition);
}
/**
* Load the entity that is either referenced or embedded by $definition.
*
* @param $definition
*
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Core\Entity\EntityStorageException
* @throws \Drupal\cms_content_sync\Exception\SyncException
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
*
* @return \Drupal\Core\Entity\EntityInterface
*/
protected function loadReferencedEntity(PullIntent $intent, $definition) {
return $intent
->loadEmbeddedEntity($definition);
}
/**
* {@inheritdoc}
*/
protected function setValues(PullIntent $intent) {
if ($intent
->shouldMergeChanges() && !$this
->forceMergeOverwrite()) {
return false;
}
/**
* @var \Drupal\Core\Entity\EntityInterface $entity
*/
$entity = $intent
->getEntity();
$data = $intent
->getProperty($this->fieldName);
$values = [];
foreach ($data ? $data : [] as $value) {
$reference = $this
->loadReferencedEntity($intent, $value);
if ($reference) {
$info = $intent
->getEmbeddedEntityData($value);
$attributes = $this
->getFieldValuesForReference($reference, $intent);
if (is_array($attributes)) {
$values[] = array_merge($info, $attributes);
}
else {
$values[] = $attributes;
}
}
elseif (!$this
->shouldEmbedReferencedEntities()) {
// Shortcut: If it's just one value and a normal entity_reference field, the MissingDependencyManager will
// directly update the field value of the entity and save it. Otherwise it will request a full pull of the
// entity. So this saves some performance for simple references.
if ('entity_reference' === $this->fieldDefinition
->getType() && !$this->fieldDefinition
->getFieldStorageDefinition()
->isMultiple()) {
$intent
->saveUnresolvedDependency($value, $this->fieldName);
}
else {
$intent
->saveUnresolvedDependency($value);
}
}
}
$entity
->set($this->fieldName, $values);
return true;
}
/**
* Get the values to be set to the $entity->field_*.
*
* @param $reference
* @param $intent
*
* @return array
*/
protected function getFieldValuesForReference($reference, $intent) {
if ('entity_reference_revisions' == $this->fieldDefinition
->getType()) {
$attributes = [
'target_id' => $reference
->id(),
'target_revision_id' => $reference
->getRevisionId(),
];
}
else {
$attributes = [
'target_id' => $reference
->id(),
];
}
return $attributes;
}
/**
* Load the referenced entity, given the $entity->field_* value.
*
* @param $value
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*
* @return null|\Drupal\Core\Entity\EntityInterface
*/
protected function loadReferencedEntityFromFieldValue($value) {
if (empty($value['target_id'])) {
return null;
}
$entityTypeManager = \Drupal::entityTypeManager();
$reference_type = isset($value['target_type']) ? $value['target_type'] : $this
->getReferencedEntityTypes()[0];
$storage = $entityTypeManager
->getStorage($reference_type);
$target_id = $value['target_id'];
return $storage
->load($target_id);
}
/**
* @return string[]
*/
protected function getInvalidSubfields() {
return [];
}
/**
* @param $value
*
* @throws \Drupal\Core\Entity\EntityStorageException
* @throws \Drupal\cms_content_sync\Exception\SyncException
* @throws \GuzzleHttp\Exception\GuzzleException
*
* @return array
*/
protected function serializeReference(PushIntent $intent, EntityInterface $reference, $value) {
foreach ($this
->getInvalidSubfields() as $field) {
unset($value[$field]);
}
foreach ($value as $key => $data) {
if ('field_' == substr($key, 0, 6)) {
unset($value[$key]);
}
}
// Allow mapping by label.
if ('taxonomy_term' == $reference
->getEntityTypeId()) {
$value[Entity::LABEL_KEY] = $reference
->label();
}
if ($this
->shouldEmbedReferencedEntities()) {
return $intent
->embed($reference, $value);
}
if ($this
->shouldPushReferencedEntities()) {
return $intent
->addDependency($reference, $value);
}
return $intent
->addReference($reference, $value);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityReferenceHandlerBase:: |
protected | function | 1 | |
EntityReferenceHandlerBase:: |
protected | function | 1 | |
EntityReferenceHandlerBase:: |
protected | function | Don't expose option, but force push. | 2 |
EntityReferenceHandlerBase:: |
protected | function | Don't expose option, but force push. | 3 |
EntityReferenceHandlerBase:: |
protected | function | Get the values to be set to the $entity->field_*. | 3 |
EntityReferenceHandlerBase:: |
public | function |
Get the handler settings. Overrides FieldHandlerBase:: |
2 |
EntityReferenceHandlerBase:: |
protected | function | 1 | |
EntityReferenceHandlerBase:: |
protected | function | 3 | |
EntityReferenceHandlerBase:: |
public static | function | ||
EntityReferenceHandlerBase:: |
protected | function | Load the entity that is either referenced or embedded by $definition. | 2 |
EntityReferenceHandlerBase:: |
protected | function | Load the referenced entity, given the $entity->field_* value. | 2 |
EntityReferenceHandlerBase:: |
public | function |
Overrides FieldHandlerBase:: |
|
EntityReferenceHandlerBase:: |
public | function |
Overrides FieldHandlerBase:: |
|
EntityReferenceHandlerBase:: |
protected | function | 2 | |
EntityReferenceHandlerBase:: |
protected | function | 1 | |
EntityReferenceHandlerBase:: |
protected | function | Check if referenced entities should be embedded automatically. | |
EntityReferenceHandlerBase:: |
protected | function | Check if referenced entities should be pushed automatically. | |
EntityReferenceHandlerBase:: |
public | function |
Validate the settings defined above. $form and $form_state are the same as
in the Form API. $settings_key is the index at $form['sync_entities'] for
this handler instance. Overrides FieldHandlerBase:: |
|
FieldHandlerBase:: |
protected | property | ||
FieldHandlerBase:: |
protected | property | ||
FieldHandlerBase:: |
protected | property | ||
FieldHandlerBase:: |
protected | property | ||
FieldHandlerBase:: |
protected | property | ||
FieldHandlerBase:: |
protected | property | A logger instance. | |
FieldHandlerBase:: |
protected | property | Additional settings as provided by { | |
FieldHandlerBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
FieldHandlerBase:: |
protected | function | 1 | |
FieldHandlerBase:: |
public | function |
Get the allowed pull options. Overrides FieldHandlerInterface:: |
|
FieldHandlerBase:: |
public | function |
Get the allowed push options. Overrides FieldHandlerInterface:: |
|
FieldHandlerBase:: |
public | function |
Overrides FieldHandlerInterface:: |
|
FieldHandlerBase:: |
public | function |
Constructs a Drupal\rest\Plugin\ResourceBase object. Overrides PluginBase:: |
|
FieldHandlerInterface:: |
public static | function | Check if this handler supports the given field instance. | 10 |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |