View source
<?php
namespace Drupal\moderation_sidebar\Form;
use Drupal\content_moderation\ModerationInformationInterface;
use Drupal\content_moderation\StateTransitionValidationInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\ContentEntityStorageInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\RevisionLogInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class QuickTransitionForm extends FormBase {
protected $moderationInformation;
protected $validation;
protected $entityTypeManager;
public function __construct(ModerationInformationInterface $moderation_info, StateTransitionValidationInterface $validation, EntityTypeManagerInterface $entity_type_manager) {
$this->moderationInformation = $moderation_info;
$this->validation = $validation;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('content_moderation.moderation_information'), $container
->get('content_moderation.state_transition_validation'), $container
->get('entity_type.manager'));
}
public function getFormId() {
return 'moderation_sidebar_quick_transition_form';
}
public function buildForm(array $form, FormStateInterface $form_state, ContentEntityInterface $entity = NULL) {
if (!$entity
->access('update')) {
return [];
}
if (!$entity
->isDefaultRevision() && $entity
->isLatestTranslationAffectedRevision()) {
$form['discard_draft'] = [
'#type' => 'submit',
'#id' => 'moderation-sidebar-discard-draft',
'#value' => $this
->t('Discard draft'),
'#attributes' => [
'class' => [
'moderation-sidebar-link',
'button',
'button--danger',
],
],
'#submit' => [
'::discardDraft',
],
];
}
$form_state
->set('entity', $entity);
$transitions = $this->validation
->getValidTransitions($entity, $this
->currentUser());
$workflow = $this->moderationInformation
->getWorkFlowForEntity($entity);
$disabled_transitions = $this
->configFactory()
->getEditable('moderation_sidebar.settings')
->get('workflows.' . $workflow
->id() . '_workflow.disabled_transitions');
$current_state = $this
->getModerationState($entity);
$transitions = array_filter($transitions, function ($transition) use ($current_state) {
return $transition
->to()
->id() != $current_state
->id();
});
$is_transition_enabled = FALSE;
foreach ($transitions as $transition) {
if (empty($disabled_transitions[$transition
->id()])) {
$form[$transition
->id()] = [
'#type' => 'submit',
'#id' => $transition
->id(),
'#value' => $transition
->label(),
'#attributes' => [
'class' => [
'moderation-sidebar-link',
'button--primary',
],
],
];
$is_transition_enabled = TRUE;
}
}
if ($is_transition_enabled) {
$form['revision_log_toggle'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use custom log message'),
'#default_value' => FALSE,
'#attributes' => [
'class' => [
'moderation-sidebar-revision-log-toggle',
],
],
];
$form['revision_log'] = [
'#type' => 'textarea',
'#placeholder' => $this
->t('Briefly describe this state change.'),
'#attributes' => [
'class' => [
'moderation-sidebar-revision-log',
],
],
'#states' => [
'visible' => [
':input[name="revision_log_toggle"]' => [
'checked' => TRUE,
],
],
],
];
}
return $form;
}
public function discardDraft(array &$form, FormStateInterface $form_state) {
$entity = $form_state
->get('entity');
$storage = $this->entityTypeManager
->getStorage($entity
->getEntityTypeId());
$default_revision_id = $this->moderationInformation
->getDefaultRevisionId($entity
->getEntityTypeId(), $entity
->id());
$default_revision = $storage
->loadRevision($default_revision_id);
if ($form_state
->getValue('revision_log_toggle')) {
$revision_log = $form_state
->getValue('revision_log');
}
else {
$revision_log = $this
->t('Used the Moderation Sidebar to discard the current draft');
}
$revision = $this
->prepareNewRevision($default_revision, $revision_log);
$revision
->save();
$this
->messenger()
->addMessage($this
->t('The draft has been discarded successfully.'));
if ($entity
->getEntityTypeId() == 'node') {
$url = Url::fromRoute('entity.node.revision', [
'node' => $entity
->id(),
'node_revision' => $entity
->getRevisionId(),
])
->toString();
$this
->messenger()
->addMessage($this
->t('<a href="@url">You can view an archive of the draft by clicking here.</a>', [
'@url' => $url,
]));
}
$form_state
->setRedirectUrl($entity
->toLink()
->getUrl());
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$entity = $form_state
->get('entity');
$transitions = $this->validation
->getValidTransitions($entity, $this
->currentUser());
$transitions['moderation-sidebar-discard-draft'] = '';
$element = $form_state
->getTriggeringElement();
if (!isset($transitions[$element['#id']])) {
$form_state
->setError($element, $this
->t('Invalid transition selected.'));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$entity = $form_state
->get('entity');
$transitions = $this->validation
->getValidTransitions($entity, $this
->currentUser());
$element = $form_state
->getTriggeringElement();
$state = $transitions[$element['#id']]
->to();
$state_id = $state
->id();
if ($form_state
->getValue('revision_log_toggle')) {
$revision_log = $form_state
->getValue('revision_log');
}
else {
$revision_log = $this
->t('Used the Moderation Sidebar to change the state to "@state".', [
'@state' => $state
->label(),
]);
}
$revision = $this
->prepareNewRevision($entity, $revision_log);
$revision
->set('moderation_state', $state_id);
$revision
->save();
$this
->messenger()
->addMessage($this
->t('The moderation state has been updated.'));
if (!$this->moderationInformation
->hasPendingRevision($entity)) {
$form_state
->setRedirectUrl($entity
->toUrl());
}
else {
$form_state
->setRedirectUrl($entity
->toUrl('latest-version'));
}
}
protected function getModerationState(ContentEntityInterface $entity) {
$state_id = $entity->moderation_state
->get(0)
->getValue()['value'];
$workflow = $this->moderationInformation
->getWorkFlowForEntity($entity);
return $workflow
->getTypePlugin()
->getState($state_id);
}
protected function prepareNewRevision(EntityInterface $entity, $message) {
$storage = $this->entityTypeManager
->getStorage($entity
->getEntityTypeId());
if ($storage instanceof ContentEntityStorageInterface) {
$revision = $storage
->createRevision($entity);
if ($revision instanceof RevisionLogInterface) {
$revision
->setRevisionLogMessage($message);
$revision
->setRevisionCreationTime(\Drupal::time()
->getRequestTime());
$revision
->setRevisionUserId($this
->currentUser()
->id());
}
return $revision;
}
return $entity;
}
}