View source
<?php
namespace Drupal\workbench_moderation\Form;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\workbench_moderation\Entity\ModerationState;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BundleModerationConfigurationForm extends EntityForm {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'));
}
public function getBaseFormId() {
return NULL;
}
public function form(array $form, FormStateInterface $form_state) {
$bundle = $form_state
->getFormObject()
->getEntity();
$form['enable_moderation_state'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable moderation states.'),
'#description' => $this
->t('Content of this type must transition through moderation states in order to be published.'),
'#default_value' => $bundle
->getThirdPartySetting('workbench_moderation', 'enabled', FALSE),
];
if ($bundle
->getThirdPartySetting('workbench_moderation', 'enabled', FALSE)) {
$form['enable_moderation_state_note'] = [
'#type' => 'item',
'#description' => $this
->t('After disabling moderation, any existing forward drafts will be accessible via the "Revisions" tab.'),
'#states' => [
'visible' => [
':input[name=enable_moderation_state]' => [
'checked' => FALSE,
],
],
],
];
}
$states = $this->entityTypeManager
->getStorage('moderation_state')
->loadMultiple();
$label = function (ModerationState $state) {
return $state
->label();
};
$options_published = array_map($label, array_filter($states, function (ModerationState $state) {
return $state
->isPublishedState();
}));
$options_unpublished = array_map($label, array_filter($states, function (ModerationState $state) {
return !$state
->isPublishedState();
}));
$form['allowed_moderation_states_unpublished'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Allowed moderation states (Unpublished)'),
'#description' => $this
->t('The allowed unpublished moderation states this content-type can be assigned.'),
'#default_value' => $bundle
->getThirdPartySetting('workbench_moderation', 'allowed_moderation_states', array_keys($options_unpublished)),
'#options' => $options_unpublished,
'#required' => TRUE,
'#states' => [
'visible' => [
':input[name=enable_moderation_state]' => [
'checked' => TRUE,
],
],
],
];
$form['allowed_moderation_states_published'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Allowed moderation states (Published)'),
'#description' => $this
->t('The allowed published moderation states this content-type can be assigned.'),
'#default_value' => $bundle
->getThirdPartySetting('workbench_moderation', 'allowed_moderation_states', array_keys($options_published)),
'#options' => $options_published,
'#required' => TRUE,
'#states' => [
'visible' => [
':input[name=enable_moderation_state]' => [
'checked' => TRUE,
],
],
],
];
$options = [
$this
->t('Unpublished')
->render() => $options_unpublished,
$this
->t('Published')
->render() => $options_published,
];
$form['default_moderation_state'] = [
'#type' => 'select',
'#title' => $this
->t('Default moderation state'),
'#options' => $options,
'#description' => $this
->t('Select the moderation state for new content'),
'#default_value' => $bundle
->getThirdPartySetting('workbench_moderation', 'default_moderation_state', 'draft'),
'#states' => [
'visible' => [
':input[name=enable_moderation_state]' => [
'checked' => TRUE,
],
],
],
];
$form['#entity_builders'][] = [
$this,
'formBuilderCallback',
];
return parent::form($form, $form_state);
}
public function formBuilderCallback($entity_type, ConfigEntityInterface $bundle, array &$form, FormStateInterface $form_state) {
$bundle
->setThirdPartySetting('workbench_moderation', 'enabled', $form_state
->getValue('enable_moderation_state'));
$bundle
->setThirdPartySetting('workbench_moderation', 'allowed_moderation_states', array_keys(array_filter($form_state
->getValue('allowed_moderation_states_published') + $form_state
->getValue('allowed_moderation_states_unpublished'))));
$bundle
->setThirdPartySetting('workbench_moderation', 'default_moderation_state', $form_state
->getValue('default_moderation_state'));
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getValue('enable_moderation_state')) {
$allowed = array_keys(array_filter($form_state
->getValue('allowed_moderation_states_published') + $form_state
->getValue('allowed_moderation_states_unpublished')));
if (($default = $form_state
->getValue('default_moderation_state')) && !in_array($default, $allowed, TRUE)) {
$form_state
->setErrorByName('default_moderation_state', $this
->t('The default moderation state must be one of the allowed states.'));
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getValue('enable_moderation_state')) {
$bundle = $form_state
->getFormObject()
->getEntity();
$this->entityTypeManager
->getHandler($bundle
->getEntityType()
->getBundleOf(), 'moderation')
->onBundleModerationConfigurationFormSubmit($bundle);
}
parent::submitForm($form, $form_state);
$this
->messenger()
->addMessage($this
->t('Your settings have been saved.'));
}
}