class CreateModeratedForwardRevision in Acquia Content Hub 8.2
Sets the entity with a forward revision and change of moderation state.
@package Drupal\acquia_contenthub\EventSubscriber\PreEntitySave
Hierarchy
- class \Drupal\acquia_contenthub_moderation\EventSubscriber\PreEntitySave\CreateModeratedForwardRevision implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of CreateModeratedForwardRevision
1 file declares its use of CreateModeratedForwardRevision
- CreateModeratedForwardRevisionTest.php in tests/
src/ Kernel/ EventSubscriber/ ModeratedRevisions/ CreateModeratedForwardRevisionTest.php
1 string reference to 'CreateModeratedForwardRevision'
- acquia_contenthub_moderation.services.yml in modules/
acquia_contenthub_moderation/ acquia_contenthub_moderation.services.yml - modules/acquia_contenthub_moderation/acquia_contenthub_moderation.services.yml
1 service uses CreateModeratedForwardRevision
File
- modules/
acquia_contenthub_moderation/ src/ EventSubscriber/ PreEntitySave/ CreateModeratedForwardRevision.php, line 20
Namespace
Drupal\acquia_contenthub_moderation\EventSubscriber\PreEntitySaveView source
class CreateModeratedForwardRevision implements EventSubscriberInterface {
/**
* The field name that stores content moderation states.
*
* @var string
*/
protected $fieldName = 'moderation_state';
/**
* The Entity Type Manager Service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Configuration for Content Hub Moderation.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;
/**
* The Content Moderation Information Interface.
*
* @var \Drupal\content_moderation\ModerationInformationInterface
*/
protected $moderationInfo;
/**
* The acquia_contenthub_moderation logger channel.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $channel;
/**
* CreateModeratedForwardRevision constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The Entity Type Manager Service.
* @param \Drupal\Core\Config\ConfigFactory $config_factory
* The Config Factory.
* @param \Drupal\content_moderation\ModerationInformationInterface $moderation_info
* The Content Moderation Information Service.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* The logger factory.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactory $config_factory, ModerationInformationInterface $moderation_info, LoggerChannelFactoryInterface $logger_factory) {
$this->entityTypeManager = $entity_type_manager;
$this->config = $config_factory
->get('acquia_contenthub_moderation.settings');
$this->moderationInfo = $moderation_info;
$this->channel = $logger_factory
->get('acquia_contenthub_moderation');
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
// Priority is low so that this is one of the last things to be changed.
$events[AcquiaContentHubEvents::PRE_ENTITY_SAVE][] = [
'onPreEntitySave',
5,
];
return $events;
}
/**
* Creates a forward revision and state change for entities being imported.
*
* @param \Drupal\acquia_contenthub\Event\PreEntitySaveEvent $event
* The pre entity save event.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function onPreEntitySave(PreEntitySaveEvent $event) {
$entity = $event
->getEntity();
if (!$entity instanceof ContentEntityInterface) {
// Only Content Entities can be moderated.
return;
}
$entity_type = $entity
->getEntityType();
$bundle = $entity
->bundle();
if (!$bundle) {
return;
}
// If this bundle is not moderated, nothing needs to be done.
if (!$this->moderationInfo
->shouldModerateEntitiesOfBundle($entity_type, $bundle)) {
return;
}
// Is there a workflow moderation state defined for imported entities.
$workflow = $this->moderationInfo
->getWorkflowForEntity($entity);
if (!$workflow) {
return;
}
$import_moderation_state = $this->config
->get("workflows.{$workflow->id()}.moderation_state");
if (empty($import_moderation_state)) {
$this->channel
->error("Acquia Content Hub moderation config doesn't have a default moderation state.");
return;
}
// Use configured moderation state.
$entity
->set($this->fieldName, $import_moderation_state);
// Check whether the entity is configured to create a new revision.
if ($entity instanceof RevisionableInterface && $entity
->isNewRevision()) {
// Make it a forward revision if import moderation state is
// not a "published" state.
$published_state = $workflow
->getTypePlugin()
->getState($import_moderation_state)
->isPublishedState();
if (!$published_state) {
$entity
->isDefaultRevision(FALSE);
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CreateModeratedForwardRevision:: |
protected | property | The acquia_contenthub_moderation logger channel. | |
CreateModeratedForwardRevision:: |
protected | property | Configuration for Content Hub Moderation. | |
CreateModeratedForwardRevision:: |
protected | property | The Entity Type Manager Service. | |
CreateModeratedForwardRevision:: |
protected | property | The field name that stores content moderation states. | |
CreateModeratedForwardRevision:: |
protected | property | The Content Moderation Information Interface. | |
CreateModeratedForwardRevision:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
CreateModeratedForwardRevision:: |
public | function | Creates a forward revision and state change for entities being imported. | |
CreateModeratedForwardRevision:: |
public | function | CreateModeratedForwardRevision constructor. |