DefaultFieldCollectionHandler.php in CMS Content Sync 2.0.x
File
src/Plugin/cms_content_sync/field_handler/DefaultFieldCollectionHandler.php
View source
<?php
namespace Drupal\cms_content_sync\Plugin\cms_content_sync\field_handler;
use Drupal\cms_content_sync\PullIntent;
use Drupal\Core\Field\FieldDefinitionInterface;
class DefaultFieldCollectionHandler extends DefaultEntityReferenceHandler {
public static $currentFieldHandler;
public static $currentPullIntent;
public static function supports($entity_type, $bundle, $field_name, FieldDefinitionInterface $field) {
if (!in_array($field
->getType(), [
'field_collection',
])) {
return false;
}
return true;
}
protected function forceEmbeddingReferencedEntities() {
return true;
}
protected function getReferencedEntityTypes() {
return [
'field_collection_item',
];
}
protected function loadReferencedEntity(PullIntent $intent, $definition) {
$previousFieldHandler = self::$currentFieldHandler;
$previousPullIntent = self::$currentPullIntent;
self::$currentFieldHandler = $this;
self::$currentPullIntent = $intent;
$entity = parent::loadReferencedEntity($intent, $definition);
self::$currentFieldHandler = $previousFieldHandler;
self::$currentPullIntent = $previousPullIntent;
return $entity;
}
protected function loadReferencedEntityFromFieldValue($value) {
if (empty($value['revision_id'])) {
return null;
}
return \Drupal::entityTypeManager()
->getStorage('field_collection_item')
->loadRevision($value['revision_id']);
}
protected function getInvalidSubfields() {
return [
'_accessCacheability',
'_attributes',
'_loaded',
'top',
'target_revision_id',
'subform',
'value',
'revision_id',
];
}
protected function getFieldValuesForReference($reference, $intent) {
$entity = $intent
->getEntity();
$reference->host_type = $entity
->getEntityTypeId();
$reference->host_id = $entity
->id();
$reference->host_entity = $entity;
$reference->field_name = $this->fieldName;
$reference
->save(true);
return [
'value' => $reference
->id(),
'revision_id' => $reference
->getRevisionId(),
];
}
}