I want to allow authenticated users to fill out a node (Draft) and then, when they are ready, submit the node (to give it the Submitted state). An admin will later decide whether or not to publish the node.
I'm experimenting with the improvements to the Content Moderation module on Drupal 8.4-rc2 (new UI is in the 8.4 series, so 8.3 and before will look different).
I have the following workflow states defined:
- Draft
- Submitted
- Published
There is a transition called Submit to move from the Draft state to the Submitted state. Authenticated users have permission to use this transition.
The problem is that the core UI for this looks very complicated.
I want to hide this moderation UI and instead provide users with a single button, "Submit for approval."
I hid the moderation and revision UI from auth users (only admin have permission to edit workflows) like this:
function mymodule_form_content_moderation_entity_moderation_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$user = \Drupal::currentUser();
if ( empty($user) || !($user->hasPermission('administer workflows')) ) {
$form['#access'] = FALSE;
}
}
function mymodule_form_node_mycontenttype_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$user = \Drupal::currentUser();
if ( empty($user) || !($user->hasPermission('administer workflows')) ) {
$form['revision_information']['#access'] = FALSE;
$form['moderation_state']['#access'] = FALSE;
}
}
function mymodule_form_node_mycontenttype_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$user = \Drupal::currentUser();
if ( empty($user) || !($user->hasPermission('administer workflows')) ) {
$form['revision_information']['#access'] = FALSE;
$form['moderation_state']['#access'] = FALSE;
}
}
So this cleans up the UI and hides all the Content Moderation options.
But how do I create a button to move from one state to another? Specifically, I want the bottom of the form to look like this:
- Save button (saves the node in the same (Draft) workflow state)
- New button that saves the node and activates the transition to the Submitted state
This is basically the same thing as what core does when it provides an option to Save as published and a second option to Save as unpublished-- I just want to do this with my workflow states.
$form['moderation_state']['#access'] = TRUE;
,$form['moderation_state']['#type'] = 'hidden';
,$form['moderation_state']['#value'] = '[NEXT STATE ID]';
. Though I would also implement some feedback to indicate the post changed state and was not just saved.