Version 1
[yaffs-website] / web / core / modules / content_moderation / src / Form / EntityModerationForm.php
1 <?php
2
3 namespace Drupal\content_moderation\Form;
4
5 use Drupal\Core\Entity\ContentEntityInterface;
6 use Drupal\Core\Entity\RevisionLogInterface;
7 use Drupal\Core\Form\FormBase;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\content_moderation\ModerationInformationInterface;
10 use Drupal\content_moderation\StateTransitionValidation;
11 use Drupal\workflows\Transition;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * The EntityModerationForm provides a simple UI for changing moderation state.
16  */
17 class EntityModerationForm extends FormBase {
18
19   /**
20    * The moderation information service.
21    *
22    * @var \Drupal\content_moderation\ModerationInformationInterface
23    */
24   protected $moderationInfo;
25
26   /**
27    * The moderation state transition validation service.
28    *
29    * @var \Drupal\content_moderation\StateTransitionValidation
30    */
31   protected $validation;
32
33   /**
34    * EntityModerationForm constructor.
35    *
36    * @param \Drupal\content_moderation\ModerationInformationInterface $moderation_info
37    *   The moderation information service.
38    * @param \Drupal\content_moderation\StateTransitionValidation $validation
39    *   The moderation state transition validation service.
40    */
41   public function __construct(ModerationInformationInterface $moderation_info, StateTransitionValidation $validation) {
42     $this->moderationInfo = $moderation_info;
43     $this->validation = $validation;
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   public static function create(ContainerInterface $container) {
50     return new static(
51       $container->get('content_moderation.moderation_information'),
52       $container->get('content_moderation.state_transition_validation')
53     );
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function getFormId() {
60     return 'content_moderation_entity_moderation_form';
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public function buildForm(array $form, FormStateInterface $form_state, ContentEntityInterface $entity = NULL) {
67     $current_state = $entity->moderation_state->value;
68     $workflow = $this->moderationInfo->getWorkflowForEntity($entity);
69
70     /** @var \Drupal\workflows\Transition[] $transitions */
71     $transitions = $this->validation->getValidTransitions($entity, $this->currentUser());
72
73     // Exclude self-transitions.
74     $transitions = array_filter($transitions, function(Transition $transition) use ($current_state) {
75       return $transition->to()->id() != $current_state;
76     });
77
78     $target_states = [];
79
80     foreach ($transitions as $transition) {
81       $target_states[$transition->to()->id()] = $transition->to()->label();
82     }
83
84     if (!count($target_states)) {
85       return $form;
86     }
87
88     if ($current_state) {
89       $form['current'] = [
90         '#type' => 'item',
91         '#title' => $this->t('Status'),
92         '#markup' => $workflow->getState($current_state)->label(),
93       ];
94     }
95
96     // Persist the entity so we can access it in the submit handler.
97     $form_state->set('entity', $entity);
98
99     $form['new_state'] = [
100       '#type' => 'select',
101       '#title' => $this->t('Moderate'),
102       '#options' => $target_states,
103     ];
104
105     $form['revision_log'] = [
106       '#type' => 'textfield',
107       '#title' => $this->t('Log message'),
108       '#size' => 30,
109     ];
110
111     $form['submit'] = [
112       '#type' => 'submit',
113       '#value' => $this->t('Apply'),
114     ];
115
116     $form['#theme'] = ['entity_moderation_form'];
117
118     return $form;
119   }
120
121   /**
122    * {@inheritdoc}
123    */
124   public function submitForm(array &$form, FormStateInterface $form_state) {
125     /** @var ContentEntityInterface $entity */
126     $entity = $form_state->get('entity');
127
128     $new_state = $form_state->getValue('new_state');
129
130     $entity->set('moderation_state', $new_state);
131
132     if ($entity instanceof RevisionLogInterface) {
133       $entity->setRevisionLogMessage($form_state->getValue('revision_log'));
134       $entity->setRevisionUserId($this->currentUser()->id());
135     }
136     $entity->save();
137
138     drupal_set_message($this->t('The moderation state has been updated.'));
139
140     $new_state = $this->moderationInfo->getWorkflowForEntity($entity)->getState($new_state);
141     // The page we're on likely won't be visible if we just set the entity to
142     // the default state, as we hide that latest-revision tab if there is no
143     // forward revision. Redirect to the canonical URL instead, since that will
144     // still exist.
145     if ($new_state->isDefaultRevisionState()) {
146       $form_state->setRedirectUrl($entity->toUrl('canonical'));
147     }
148   }
149
150 }