Skip to content
Snippets Groups Projects
Commit 744b4f01 authored by Vadym Abramchuk's avatar Vadym Abramchuk
Browse files

Issue #3336402: Add text field processor plugin

parent f1cb6886
No related branches found
No related tags found
1 merge request!37Issue #3336402: Move to plugins
<?php
namespace Drupal\single_content_sync\Plugin\Derivative\SingleContentSyncFieldProcessor;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Retrieves field processor plugin definitions for text field types.
*
* @see \Drupal\single_content_sync\Plugin\SingleContentSyncFieldProcessor\TextField
*/
class TextFieldDeriver extends DeriverBase {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
$textFieldTypes = [
'text_long',
'text_with_summary',
];
foreach ($textFieldTypes as $fieldType) {
$this->derivatives[$fieldType] = $base_plugin_definition;
$this->derivatives[$fieldType]['id'] = $base_plugin_definition['id'] . ':' . $fieldType;
$this->derivatives[$fieldType]['label'] = new TranslatableMarkup('Text field processor for @fieldType', ['@fieldType' => $fieldType]);
$this->derivatives[$fieldType]['field_type'] = $fieldType;
}
return parent::getDerivativeDefinitions($base_plugin_definition);
}
}
<?php
namespace Drupal\single_content_sync\Plugin\SingleContentSyncFieldProcessor;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\media\MediaInterface;
use Drupal\single_content_sync\ContentExporterInterface;
use Drupal\single_content_sync\ContentImporterInterface;
use Drupal\single_content_sync\SingleContentSyncFieldProcessorPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of the text field processor plugin.
*
* @SingleContentSyncFieldProcessor(
* id = "text_field",
* deriver = "Drupal\single_content_sync\Plugin\Derivative\SingleContentSyncFieldProcessor\TextFieldDeriver",
* )
*/
class TextField extends SingleContentSyncFieldProcessorPluginBase implements ContainerFactoryPluginInterface {
/**
* The content exporter service.
*
* @var \Drupal\single_content_sync\ContentExporterInterface
*/
protected ContentExporterInterface $exporter;
/**
* The content importer service.
*
* @var \Drupal\single_content_sync\ContentImporterInterface
*/
protected ContentImporterInterface $importer;
/**
* The entity repository service.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected EntityRepositoryInterface $entityRepository;
/**
* Constructs new EntityReference plugin instance.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\single_content_sync\ContentExporterInterface $exporter
* The content exporter service.
* @param \Drupal\single_content_sync\ContentImporterInterface $importer
* The content importer service.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository service.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
ContentExporterInterface $exporter,
ContentImporterInterface $importer,
EntityRepositoryInterface $entity_repository
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->exporter = $exporter;
$this->importer = $importer;
$this->entityRepository = $entity_repository;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('single_content_sync.exporter'),
$container->get('single_content_sync.importer'),
$container->get('entity.repository'),
);
}
/**
* {@inheritdoc}
*/
public function exportFieldValue(FieldItemListInterface $field): array {
$value = $field->getValue();
foreach ($value as &$item) {
$text = $item['value'];
if (stristr($text, '<drupal-media') === FALSE) {
continue;
}
$dom = Html::load($text);
$xpath = new \DOMXPath($dom);
$embed_entities = [];
foreach ($xpath->query('//drupal-media[@data-entity-type="media" and normalize-space(@data-entity-uuid)!=""]') as $node) {
/** @var \DOMElement $node */
$uuid = $node->getAttribute('data-entity-uuid');
$media = $this->entityRepository->loadEntityByUuid('media', $uuid);
assert($media === NULL || $media instanceof MediaInterface);
if ($media) {
$embed_entities[] = $this->exporter->doExportToArray($media);
}
}
$item['embed_entities'] = $embed_entities;
}
return $value;
}
/**
* {@inheritdoc}
*/
public function importFieldValue(FieldableEntityInterface $entity, string $fieldName, array $value): void {
foreach ($value as $item) {
$embed_entities = $item['embed_entities'] ?? [];
foreach ($embed_entities as $embed_entity) {
$this->importer->doImport($embed_entity);
}
}
$entity->set($fieldName, $value);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment