You are here

WorkflowNodeNextStateAction.php in Workflow 8

File

src/Plugin/Action/WorkflowNodeNextStateAction.php
View source
<?php

namespace Drupal\workflow\Plugin\Action;

use Drupal\Core\Form\FormStateInterface;

/**
 * Sets an entity to the next state.
 *
 * The only change is the 'type' in the Annotation, so it works on Nodes,
 * and can be seen on admin/content page.
 *
 * @Action(
 *   id = "workflow_node_next_state_action",
 *   label = @Translation("Change a node to next Workflow state"),
 *   type = "node"
 * )
 */
class WorkflowNodeNextStateAction extends WorkflowStateActionBase {

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    return [
      'module' => [
        'workflow',
        'node',
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);

    // Remove to_sid. User can't set it, since we want a dynamic 'next' state.
    unset($form['to_sid']);
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function execute($object = NULL) {
    if (!($transition = $this
      ->getTransitionForExecution($object))) {
      $this
        ->messenger()
        ->addWarning('The object is not valid for this action.');
      return;
    }

    /*
     * Set the new next state.
     */
    $entity = $transition
      ->getTargetEntity();
    $field_name = $transition
      ->getFieldName();
    $user = $transition
      ->getOwner();

    // $comment = $transition->getComment();
    $force = $this->configuration['force'];

    // Get the node's new State Id (which is the next available state).
    $to_sid = $transition
      ->getWorkflow()
      ->getNextSid($entity, $field_name, $user, $force);

    // Add actual data.
    $transition->to_sid = $to_sid;

    // Fire the transition.
    workflow_execute_transition($transition, $force);
  }

}

Classes

Namesort descending Description
WorkflowNodeNextStateAction Sets an entity to the next state.