Commit 80fb60de authored by Ben Dougherty's avatar Ben Dougherty

Merge pull request #50 from benjy/add-import-export-events

Add a ImportEvent and ExportEvent
parents 3b235525 dbd733e6
services:
default_content.manager:
class: Drupal\default_content\DefaultContentManager
arguments: ['@serializer', '@plugin.manager.rest', '@current_user', '@entity.manager', '@rest.link_manager']
arguments: ['@serializer', '@plugin.manager.rest', '@current_user', '@entity.manager', '@rest.link_manager', '@event_dispatcher']
......@@ -13,8 +13,13 @@ use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Session\AccountInterface;
use Drupal\default_content\Event\DefaultContentEvents;
use Drupal\default_content\Event\ExportEvent;
use Drupal\default_content\Event\ImportEvent;
use Drupal\rest\LinkManager\LinkManagerInterface;
use Drupal\rest\Plugin\Type\ResourcePluginManager;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Serializer\Serializer;
/**
......@@ -81,6 +86,13 @@ class DefaultContentManager implements DefaultContentManagerInterface {
*/
protected $linkManager;
/**
* The event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* Constructs the default content manager.
*
......@@ -95,11 +107,12 @@ class DefaultContentManager implements DefaultContentManagerInterface {
* @param \Drupal\rest\LinkManager\LinkManagerInterface $link_manager
* The link manager service.
*/
public function __construct(Serializer $serializer, ResourcePluginManager $resource_plugin_manager, AccountInterface $current_user, EntityManager $entity_manager, LinkManagerInterface $link_manager) {
public function __construct(Serializer $serializer, ResourcePluginManager $resource_plugin_manager, AccountInterface $current_user, EntityManager $entity_manager, LinkManagerInterface $link_manager, EventDispatcherInterface $event_dispatcher) {
$this->serializer = $serializer;
$this->resourcePluginManager = $resource_plugin_manager;
$this->entityManager = $entity_manager;
$this->linkManager = $link_manager;
$this->eventDispatcher = $event_dispatcher;
}
/**
......@@ -178,9 +191,10 @@ class DefaultContentManager implements DefaultContentManagerInterface {
$entity = $this->serializer->deserialize($contents, $class, 'hal_json', array('request_method' => 'POST'));
$entity->enforceIsNew(TRUE);
$entity->save();
$created[] = $entity;
$created[$entity->uuid()] = $entity;
}
}
$this->eventDispatcher->dispatch(DefaultContentEvents::IMPORT, new ImportEvent($created, $module));
}
// Reset the tree.
$this->resetTree();
......@@ -200,6 +214,8 @@ class DefaultContentManager implements DefaultContentManagerInterface {
$return = $this->serializer->serialize($entity, 'hal_json', ['json_encode_options' => JSON_PRETTY_PRINT]);
// Reset link domain.
$this->linkManager->setLinkDomain(FALSE);
$this->eventDispatcher->dispatch(DefaultContentEvents::EXPORT, new ExportEvent($entity));
return $return;
}
......
<?php
/**
* @file
* \Drupal\default_content\Event\DefaultContentEvents
*/
namespace Drupal\default_content\Event;
/**
* Defines the events for Default Content.
*
* @see \Drupal\default_content\Event\ImportEvent
* @see \Drupal\default_content\Event\ExportEvent
*/
final class DefaultContentEvents {
/**
* Name of the event fired when importing default content.
*
* This event allows modules to perform actions after the default content has
* been imported. The event listener receives a
* \Drupal\default_content\Event\ImportEvent instance.
*
* @Event
*
* @see \Drupal\default_content\Event\ImportEvent
*
* @var string
*/
const IMPORT = 'default_content.import';
/**
* Name of the event fired when exporting default content.
*
* This event allows modules to perform actions after the default content has
* been exported. The event listener receives a
* \Drupal\default_content\Event\ExportEvent instance.
*
* @Event
*
* @see \Drupal\default_content\Event\ExportEvent
*
* @var string
*/
const EXPORT = 'default_content.export';
}
<?php
/**
* @file
* \Drupal\default_content\Event\ExportEvent
*/
namespace Drupal\default_content\Event;
use Drupal\Core\Entity\ContentEntityInterface;
use Symfony\Component\EventDispatcher\Event;
class ExportEvent extends Event {
/**
* The entity we're exporting.
*
* @var \Drupal\Core\Entity\ContentEntityInterface
*/
protected $entity;
/**
* Constructs a new export event.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The exported content entity.
*/
public function __construct(ContentEntityInterface $entity) {
$this->entity = $entity;
}
/**
* Get the entity we've exported.
*
* @return \Drupal\Core\Entity\ContentEntityInterface[]
*/
public function getExportedEntity() {
return $this->entity;
}
}
<?php
/**
* @file
* \Drupal\default_content\Event\ImportEvent
*/
namespace Drupal\default_content\Event;
use Symfony\Component\EventDispatcher\Event;
class ImportEvent extends Event {
/**
* An array of content entities that were imported.
*
* @var \Drupal\Core\Entity\ContentEntityInterface[]
*/
protected $entities;
/**
* The module that provides the default content.
*
* @var string
*/
protected $module;
/**
* Constructs a new import event.
*
* @param \Drupal\Core\Entity\ContentEntityInterface[] $entities
* An array of content entities that were imported.
* @param string $module
* The module that provided the default content.
*/
public function __construct(array $entities, $module) {
$this->entities = $entities;
$this->module = $module;
}
/**
* Get the imported entities.
*
* @return \Drupal\Core\Entity\ContentEntityInterface[]
*/
public function getImportedEntities() {
return $this->entities;
}
/**
* Gets the module name.
*
* @return string
* The module name that provided the default content.
*/
public function getModule() {
return $this->module;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment