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

Issue #3336402: Deprecate hook_content_export_entity_alter in favor of new ExportEvent

parent a27dbe18
No related branches found
No related tags found
1 merge request!37Issue #3336402: Move to plugins
...@@ -44,6 +44,11 @@ function hook_content_export_field_value_alter(&$value, FieldItemListInterface $ ...@@ -44,6 +44,11 @@ function hook_content_export_field_value_alter(&$value, FieldItemListInterface $
* The original value of base fields during the import. * The original value of base fields during the import.
* @param \Drupal\Core\Entity\FieldableEntityInterface $entity * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
* The entity to be exported. * The entity to be exported.
*
* @deprecated in single_content_sync:1.4.0, and is removed from single_content_sync:2.0.0.
* @see https://www.drupal.org/project/single_content_sync/issues/3336402
* @see \Drupal\single_content_sync\ContentExporter::doExportToArray()
* @see \Drupal\single_content_sync\Event\ExportEvent
*/ */
function hook_content_export_entity_alter(array &$base_fields, FieldableEntityInterface $entity) { function hook_content_export_entity_alter(array &$base_fields, FieldableEntityInterface $entity) {
switch ($entity->getEntityTypeId()) { switch ($entity->getEntityTypeId()) {
......
...@@ -10,6 +10,7 @@ services: ...@@ -10,6 +10,7 @@ services:
- '@entity.repository' - '@entity.repository'
- '@plugin.manager.single_content_sync_field_processor' - '@plugin.manager.single_content_sync_field_processor'
- '@plugin.manager.single_content_sync_base_fields_processor' - '@plugin.manager.single_content_sync_base_fields_processor'
- '@event_dispatcher'
single_content_sync.importer: single_content_sync.importer:
class: Drupal\single_content_sync\ContentImporter class: Drupal\single_content_sync\ContentImporter
......
...@@ -12,6 +12,8 @@ use Drupal\Core\Messenger\MessengerInterface; ...@@ -12,6 +12,8 @@ use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Serialization\Yaml; use Drupal\Core\Serialization\Yaml;
use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\field\FieldConfigInterface; use Drupal\field\FieldConfigInterface;
use Drupal\single_content_sync\Event\ExportEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/** /**
* Define a service to export content. * Define a service to export content.
...@@ -97,6 +99,13 @@ class ContentExporter implements ContentExporterInterface { ...@@ -97,6 +99,13 @@ class ContentExporter implements ContentExporterInterface {
*/ */
protected SingleContentSyncBaseFieldsProcessorPluginManagerInterface $entityBaseFieldsProcessorPluginManager; protected SingleContentSyncBaseFieldsProcessorPluginManagerInterface $entityBaseFieldsProcessorPluginManager;
/**
* The event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected EventDispatcherInterface $eventDispatcher;
/** /**
* ContentExporter constructor. * ContentExporter constructor.
* *
...@@ -116,6 +125,8 @@ class ContentExporter implements ContentExporterInterface { ...@@ -116,6 +125,8 @@ class ContentExporter implements ContentExporterInterface {
* The field processor plugin manager. * The field processor plugin manager.
* @param \Drupal\single_content_sync\SingleContentSyncBaseFieldsProcessorPluginManagerInterface $entity_base_fields_processor_plugin_manager * @param \Drupal\single_content_sync\SingleContentSyncBaseFieldsProcessorPluginManagerInterface $entity_base_fields_processor_plugin_manager
* The entity base fields processor plugin manager. * The entity base fields processor plugin manager.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher.
*/ */
public function __construct( public function __construct(
EntityTypeManagerInterface $entity_type_manager, EntityTypeManagerInterface $entity_type_manager,
...@@ -125,7 +136,8 @@ class ContentExporter implements ContentExporterInterface { ...@@ -125,7 +136,8 @@ class ContentExporter implements ContentExporterInterface {
ContentSyncHelperInterface $content_sync_helper, ContentSyncHelperInterface $content_sync_helper,
EntityRepositoryInterface $entity_repository, EntityRepositoryInterface $entity_repository,
SingleContentSyncFieldProcessorPluginManagerInterface $field_processor_plugin_manager, SingleContentSyncFieldProcessorPluginManagerInterface $field_processor_plugin_manager,
SingleContentSyncBaseFieldsProcessorPluginManagerInterface $entity_base_fields_processor_plugin_manager SingleContentSyncBaseFieldsProcessorPluginManagerInterface $entity_base_fields_processor_plugin_manager,
EventDispatcherInterface $event_dispatcher
) { ) {
$this->entityTypeManager = $entity_type_manager; $this->entityTypeManager = $entity_type_manager;
$this->moduleHandler = $module_handler; $this->moduleHandler = $module_handler;
...@@ -135,6 +147,7 @@ class ContentExporter implements ContentExporterInterface { ...@@ -135,6 +147,7 @@ class ContentExporter implements ContentExporterInterface {
$this->entityRepository = $entity_repository; $this->entityRepository = $entity_repository;
$this->fieldProcessorPluginManager = $field_processor_plugin_manager; $this->fieldProcessorPluginManager = $field_processor_plugin_manager;
$this->entityBaseFieldsProcessorPluginManager = $entity_base_fields_processor_plugin_manager; $this->entityBaseFieldsProcessorPluginManager = $entity_base_fields_processor_plugin_manager;
$this->eventDispatcher = $event_dispatcher;
} }
/** /**
...@@ -229,8 +242,17 @@ class ContentExporter implements ContentExporterInterface { ...@@ -229,8 +242,17 @@ class ContentExporter implements ContentExporterInterface {
'custom_fields' => $this->exportCustomValues($entity), 'custom_fields' => $this->exportCustomValues($entity),
]; ];
$exportEvent = new ExportEvent($entity, $output);
$this->eventDispatcher->dispatch($exportEvent);
$output = $exportEvent->getContent();
// Alter value by using hook_content_export_entity_alter(). // Alter value by using hook_content_export_entity_alter().
$this->moduleHandler->alter('content_export_entity', $output['base_fields'], $entity); $this->moduleHandler->alterDeprecated(
'Deprecated as of single_content_sync 1.4.0; subscribe to \Drupal\single_content_sync\Event\ExportEvent instead. For implementing support of new entity types, implement SingleContentSyncBaseFieldsProcessor plugin.',
'content_export_entity',
$output['base_fields'],
$entity,
);
// Display a message when we don't support base fields export for specific // Display a message when we don't support base fields export for specific
// entity type. // entity type.
......
<?php
namespace Drupal\single_content_sync\Event;
use Drupal\Component\EventDispatcher\Event;
use Drupal\Core\Entity\ContentEntityInterface;
/**
* The event dispatched when the entity is exported.
*/
class ExportEvent extends Event {
/**
* Constructs a new ExportEvent object.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity being exported.
* @param array $content
* The content being exported.
*/
public function __construct(
protected ContentEntityInterface $entity,
protected array $content,
) {}
/**
* Gets the entity being exported.
*
* @return \Drupal\Core\Entity\ContentEntityInterface
* The entity being exported.
*/
public function getEntity(): ContentEntityInterface {
return $this->entity;
}
/**
* Gets the content being exported.
*
* @return array
* The exported content. The array keys are:
* - 'entity_type': The entity type.
* - 'bundle': The entity bundle.
* - 'uuid': The entity UUID.
* - 'base_fields': The entity base fields.
* - 'custom_fields': The entity custom fields.
*/
public function getContent(): array {
return $this->content;
}
/**
* Sets the exported content.
*
* @param array $content
* The exported content. The same array keys should be preserved as returned
* by getContent().
*
* @return $this
*/
public function setContent(array $content): static {
$this->content = $content;
return $this;
}
}
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