3 namespace Drupal\content_moderation\Form;
5 use Drupal\content_moderation\Plugin\WorkflowType\ContentModeration;
6 use Drupal\workflows\WorkflowInterface;
7 use Drupal\Core\Entity\EntityForm;
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\Core\Form\FormStateInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Form for configuring moderation usage on a given entity bundle.
15 class BundleModerationConfigurationForm extends EntityForm {
18 * Entity Type Manager service.
20 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
22 protected $entityTypeManager;
27 public function __construct(EntityTypeManagerInterface $entity_type_manager) {
28 $this->entityTypeManager = $entity_type_manager;
34 public static function create(ContainerInterface $container) {
35 return new static($container->get('entity_type.manager'));
41 * Blank out the base form ID so that form alters that use the base form ID to
42 * target both add and edit forms don't pick up this form.
44 public function getBaseFormId() {
51 public function form(array $form, FormStateInterface $form_state) {
52 /* @var \Drupal\Core\Config\Entity\ConfigEntityInterface $bundle */
53 $bundle = $this->getEntity();
54 $bundle_of_entity_type = $this->entityTypeManager->getDefinition($bundle->getEntityType()->getBundleOf());
55 /* @var \Drupal\workflows\WorkflowInterface[] $workflows */
56 $workflows = $this->entityTypeManager->getStorage('workflow')->loadMultiple();
58 $options = array_map(function (WorkflowInterface $workflow) {
59 return $workflow->label();
60 }, array_filter($workflows, function (WorkflowInterface $workflow) {
61 return $workflow->status() && $workflow->getTypePlugin() instanceof ContentModeration;
64 $selected_workflow = array_reduce($workflows, function ($carry, WorkflowInterface $workflow) use ($bundle_of_entity_type, $bundle) {
65 $plugin = $workflow->getTypePlugin();
66 if ($plugin instanceof ContentModeration && $plugin->appliesToEntityTypeAndBundle($bundle_of_entity_type->id(), $bundle->id())) {
67 return $workflow->id();
73 '#title' => $this->t('Select the workflow to apply'),
74 '#default_value' => $selected_workflow,
75 '#options' => $options,
80 $form['original_workflow'] = [
82 '#value' => $selected_workflow,
87 '#value' => $bundle->id(),
90 $form['entity_type'] = [
92 '#value' => $bundle_of_entity_type->id(),
95 // Add a special message when moderation is being disabled.
96 if ($selected_workflow) {
97 $form['enable_workflow_note'] = [
99 '#description' => $this->t('After disabling moderation, any existing forward drafts will be accessible via the "Revisions" tab.'),
100 '#access' => !empty($selected_workflow)
104 return parent::form($form, $form_state);
110 public function submitForm(array &$form, FormStateInterface $form_state) {
111 // If moderation is enabled, revisions MUST be enabled as well. Otherwise we
112 // can't have forward revisions.
113 drupal_set_message($this->t('Your settings have been saved.'));
119 public function save(array $form, FormStateInterface $form_state) {
120 $entity_type_id = $form_state->getValue('entity_type');
121 $bundle_id = $form_state->getValue('bundle');
122 $new_workflow_id = $form_state->getValue('workflow');
123 $original_workflow_id = $form_state->getValue('original_workflow');
124 if ($new_workflow_id === $original_workflow_id) {
128 if ($original_workflow_id) {
129 /* @var \Drupal\workflows\WorkflowInterface $workflow */
130 $workflow = $this->entityTypeManager->getStorage('workflow')->load($original_workflow_id);
131 $workflow->getTypePlugin()->removeEntityTypeAndBundle($entity_type_id, $bundle_id);
134 if ($new_workflow_id) {
135 /* @var \Drupal\workflows\WorkflowInterface $workflow */
136 $workflow = $this->entityTypeManager->getStorage('workflow')->load($new_workflow_id);
137 $workflow->getTypePlugin()->addEntityTypeAndBundle($entity_type_id, $bundle_id);
145 protected function actions(array $form, FormStateInterface $form_state) {
146 $actions['submit'] = [
148 '#value' => $this->t('Save'),
149 '#submit' => ['::submitForm', '::save'],