Skip to content

Commit 8a0a426

Browse files
committed
Issue #25: Allow node types to identify allowed moderation states.
1 parent 403f689 commit 8a0a426

File tree

1 file changed

+51
-6
lines changed

1 file changed

+51
-6
lines changed

content_moderation.module

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -936,13 +936,44 @@ function content_moderation_form_node_type_form_alter(&$form, $form_state) {
936936
),
937937
);
938938

939+
// Allow specific states for each node type. Default to all states for
940+
// backwards-compatibility.
941+
if (!isset($node_type->settings['allowed_states'])) {
942+
$default_set = FALSE;
943+
$moderation_states_default = array_keys($options);
944+
}
945+
else {
946+
$default_set = TRUE;
947+
$moderation_states_default = $node_type->settings['allowed_states'];
948+
}
949+
$form['content_moderation']['allowed_states'] = array(
950+
'#type' => 'checkboxes',
951+
'#title' => t('Allowed moderation states'),
952+
'#options' => $options,
953+
'#default_value' => $moderation_states_default,
954+
'#description' => t(''),
955+
'#states' => array(
956+
'visible' => array(':input[name="moderation_enabled"]' => array('checked' => TRUE)),
957+
),
958+
);
959+
960+
// Reduce the options for default to only those that are allowed.
961+
// @todo also use #ajax to update these options real-time.
962+
$moderation_states_default_options = $options;
963+
if ($default_set) {
964+
foreach ($options as $key => $label) {
965+
if (!in_array($key, $node_type->settings['allowed_states'])) {
966+
unset($moderation_states_default_options[$key]);
967+
}
968+
}
969+
}
939970
// This select element is hidden when moderation is not enabled.
940971
$form['content_moderation']['content_moderation_default_state'] = array(
941972
'#title' => t('Default moderation state'),
942973
'#type' => 'select',
943-
'#options' => $options,
974+
'#options' => $moderation_states_default_options,
944975
'#default_value' => !isset($node_type->settings['content_moderation_default_state']) ? NULL : $node_type->settings['content_moderation_default_state'],
945-
'#description' => t('Set the default moderation state for this content type. Users with additional moderation permissions will be able to set the moderation state when creating or editing nodes.'),
976+
'#description' => t('Set the default moderation state for this content type. Paople with additional moderation permissions will be able to change this state when creating or editing content.'),
946977
'#states' => array(
947978
'visible' => array(':input[name="moderation_enabled"]' => array('checked' => TRUE)),
948979
),
@@ -960,6 +991,15 @@ function content_moderation_node_type_form_validate($form, &$form_state) {
960991
$form_state['values']['status_default'] = 0;
961992
$form_state['values']['revision_enabled'] = 1;
962993
}
994+
// Ensure the default state is an allowed state.
995+
if (isset($form_state['values']['content_moderation_default_state']) && isset($node_type->settings['allowed_states'])) {
996+
$node_type = $form['#node_type'];
997+
$default = $form_state['values']['content_moderation_default_state'];
998+
$allowed = $node_type->settings['allowed_states'];
999+
if (!in_array($default, $allowed)) {
1000+
form_set_error('content_moderation_default_state');
1001+
}
1002+
}
9631003
}
9641004

9651005
/**
@@ -969,6 +1009,7 @@ function content_moderation_node_type_form_submit($form, &$form_state) {
9691009
if (isset($form_state['values']['moderation_enabled'])) {
9701010
$config = config('node.type.' . $form_state['values']['type']);
9711011
$config->set('settings.moderation_enabled', $form_state['values']['moderation_enabled']);
1012+
$config->set('settings.allowed_states', $form_state['values']['allowed_states']);
9721013
$config->set('settings.content_moderation_default_state', $form_state['values']['content_moderation_default_state']);
9731014
$config->save();
9741015
}
@@ -1012,25 +1053,29 @@ function content_moderation_form_node_form_alter(&$form, $form_state) {
10121053
// can not change the moderation state.
10131054
if ($states = content_moderation_states_next($moderation_state, $user, $form['#node'])) {
10141055
$states[$moderation_state] = t('@state (Current)', array('@state' => content_moderation_state_label($moderation_state)));
1056+
10151057
$states_sorted = array();
1016-
foreach (array_keys(content_moderation_states()) as $state) {
1017-
if (array_key_exists($state, $states)) {
1058+
$node_type_settings = config_get('node.type.' . $form['type']['#value'], 'settings');
1059+
$allowed_states = $node_type_settings['allowed_states'];
1060+
foreach (array_keys(content_moderation_states()) as $key => $state) {
1061+
if (in_array($state, $allowed_states)) {
10181062
$states_sorted[$state] = $states[$state];
10191063
}
10201064
}
1065+
10211066
$states = $states_sorted;
10221067

10231068
$form['revision_information']['content_moderation_state_new'] = array(
10241069
'#title' => t('Moderation state'),
10251070
'#type' => 'select',
10261071
'#options' => $states,
1027-
'#description' => t('Set the moderation state for this content.'),
10281072
'#access' => $states ? TRUE : FALSE,
1073+
'#weight' => -1,
10291074
);
10301075

10311076
// If the user has access to the pre-set default state, make it the default
10321077
// here. Otherwise, don't set a default in this case.
1033-
$default_state = config_get('node.type.' . $form['type']['#value'], 'settings.content_moderation_default_state');
1078+
$default_state = $node_type_settings['content_moderation_default_state'];
10341079
if ($default_state && array_key_exists($default_state, $states)) {
10351080
$form['revision_information']['content_moderation_state_new']['#default_value'] = $default_state;
10361081
}

0 commit comments

Comments
 (0)