Preview button is broken with workflow buttons because the moderation_state is not populated.

Created on 27 June 2020, over 4 years ago
Updated 13 December 2023, about 1 year ago

Ran into bug with the preview button, it kept saying moderation_state was null. Eventually realized I needed to add '#moderation_state' value to the 'preview' button in a form alter hook, and then it worked. Not sure if it's something specific to my installation, but thought it was worth noting.

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

🇺🇸United States wlbryant

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇧🇪Belgium vodde83

    I can confirm that the solution by @ras-ben does indeed fix the issue. I also agree that this shouldn't be required, as the 'Draft' option before the fix was completely unusable.

    However, I did update the fix slightly to this:

    /**
     * Implements hook_form_BASE_FORM_ID_alter() for Node Forms.
     *
     * Fix an issue with previewing moderated Nodes, due to the moderation state not
     * being properly relayed.
     *
     * @see https://www.drupal.org/project/workflow_buttons/issues/3155336
     */
    function MY_MODULE_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id): void {
      // No need to do anything if the 'Preview' action is not available.
      if (!array_key_exists('preview', $form['actions'])) {
        return;
      }
    
      /** @var \Drupal\node\NodeForm $node_form */
      $node_form = $form_state->getFormObject();
      $node = $node_form->getEntity();
    
      // If the Node is a moderated one, relay the current moderation state to the
      // 'Preview' action.
      if ($moderation_state = $node->get('moderation_state')->getString()) {
        $form['actions']['preview']['#moderation_state'] = $moderation_state;
      }
    }
  • Status changed to RTBC over 1 year ago
  • 🇮🇳India shubham_jain

    Hi @vodde83, I confirm that the solution provided by you solves the issue and is much better approach. So, moving it to the RTBC.

  • Status changed to Needs work over 1 year ago
  • 🇩🇰Denmark ras-ben Copenhagen

    A review comment:

    if ($moderation_state = $node->get('moderation_state')->getString()) {
    

    What if the node doesnt have 'moderation_state'? Then, you'll be doing ->getString() on null.

  • 🇧🇪Belgium vodde83

    True that @ras-ben, an additional check $node->hasField('moderation_state') would be even better.

  • 🇯🇵Japan ultrabob Japan

    Would the correct patch for this module be to update workflow_buttons/src/Plugin/Field/FieldWidget/WorkflowButtonsWidget.php->processActions to set a default moderation state for preview if it exists, rather than doing a form alter?

  • 🇯🇵Japan ultrabob Japan

    @mlncn if you have a thought on the best approach, I'd be happy to take a crack at this.

  • 🇪🇪Estonia mikkmiggur

    I created a patch for this module based on the #5 code.

  • Status changed to Fixed about 1 year ago
  • 🇺🇸United States mlncn Minneapolis, MN, USA

    Thank you mikkmiggur, ultrabob, wlbryant, ras-ben, vodde83, and shubham_jain for your work on this!

    Fix applied.

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024