Commit 0687e197 authored by Crell's avatar Crell

Split up the moderation states in the configuration form, so we can rely on...

Split up the moderation states in the configuration form, so we can rely on form API validation better.
parent 0eb0d45a
......@@ -73,27 +73,57 @@ class BundleModerationConfigurationForm extends EntityForm {
}
$states = $this->entityTypeManager->getStorage('moderation_state')->loadMultiple();
$options = [];
/** @var ModerationState $state */
foreach ($states as $key => $state) {
$options[$key] = $state->label() . ' ' . ($state->isPublishedState() ? t('(published)') : t('(non-published)'));
}
$form['allowed_moderation_states'] = [
$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' => t('Allowed moderation states.'),
'#description' => t('The allowed moderation states this content-type can be assigned. You must select at least one published and one non-published state.'),
'#default_value' => $bundle->getThirdPartySetting('workbench_moderation', 'allowed_moderation_states', array_keys($options)),
'#options' => $options,
'#title' => t('Allowed moderation states (Unpublished)'),
'#description' => t('The allowed unpublisehd 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' => t('Allowed moderation states (Published)'),
'#description' => 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],
],
],
];
// This is screwy, but the key of the array needs to be a user-facing string
// so we have to fully render the translatable string to a real string, or
// else PHP chokes on an object used as an array key.
$options = [
$this->t('Unpublished')->render() => $options_unpublished,
$this->t('Published')->render() => $options_published,
];
$form['default_moderation_state'] = [
'#type' => 'select',
'#title' => t('Default moderation state'),
'#empty_option' => t('-- Select --'),
'#options' => $options,
'#description' => t('Select the moderation state for new content'),
'#default_value' => $bundle->getThirdPartySetting('workbench_moderation', 'default_moderation_state', 'draft'),
......@@ -123,7 +153,7 @@ class BundleModerationConfigurationForm extends EntityForm {
public function formBuilderCallback($entity_type, ConfigEntityInterface $bundle, &$form, FormStateInterface $form_state) {
// @todo write a test for this.
$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'))));
$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'));
}
......@@ -131,32 +161,11 @@ class BundleModerationConfigurationForm extends EntityForm {
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// @todo write a test for this.
if ($form_state->getValue('enable_moderation_state')) {
$states = $this->entityTypeManager->getStorage('moderation_state')->loadMultiple();
$published = FALSE;
$non_published = TRUE;
$allowed = array_keys(array_filter($form_state->getValue('allowed_moderation_states')));
foreach ($allowed as $state_id) {
/** @var ModerationState $state */
$state = $states[$state_id];
if ($state->isPublishedState()) {
$published = TRUE;
}
else {
$non_published = TRUE;
}
}
if (!$published || !$non_published) {
$form_state->setErrorByName('allowed_moderation_states', t('You must select at least one published moderation and one non-published state.'));
}
if (($default = $form_state->getValue('default_moderation_state')) && !empty($default)) {
if (!in_array($default, $allowed, TRUE)) {
$form_state->setErrorByName('default_moderation_state', t('The default moderation state must be one of the allowed states.'));
}
}
else {
$form_state->setErrorByName('default_moderation_state', t('You must select a default 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', t('The default moderation state must be one of the allowed states.'));
}
}
}
......
......@@ -5,6 +5,7 @@ namespace Drupal\workbench_moderation\Tests;
use Drupal\Core\Session\AccountInterface;
use Drupal\simpletest\WebTestBase;
use Drupal\user\Entity\Role;
use Drupal\workbench_moderation\Entity\ModerationState;
/**
* Defines a base class for moderation state tests.
......@@ -98,9 +99,14 @@ abstract class ModerationStateTestBase extends WebTestBase {
$edit = [];
if ($moderated) {
$edit['enable_moderation_state'] = 1;
foreach ($allowed_states as $state) {
$edit['allowed_moderation_states[' . $state . ']'] = 1;
/** @var ModerationState $state */
foreach (ModerationState::loadMultiple() as $id => $state) {
$value = (int)in_array($id, $allowed_states);
$key = $state->isPublishedState() ? 'allowed_moderation_states_published[' . $state->id() . ']' : 'allowed_moderation_states_unpublished[' . $state->id() . ']';
$edit[$key] = $value;
}
$edit['default_moderation_state'] = $default_state;
}
$this->drupalPostForm(NULL, $edit, t('Save'));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment