You are here

abstract class WorkflowStateActionBase in Workflow 8

Sets an entity to a new, given state.

Example Annotation @ Action( id = "workflow_given_state_action", label =

Plugin annotation


@Translation("Change a node to new Workflow state"),
  type = "workflow"
)

Hierarchy

Expanded class hierarchy of WorkflowStateActionBase

File

src/Plugin/Action/WorkflowStateActionBase.php, line 25

Namespace

Drupal\workflow\Plugin\Action
View source
abstract class WorkflowStateActionBase extends ConfigurableActionBase implements ContainerFactoryPluginInterface {

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

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition);
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    $configuration = parent::defaultConfiguration();
    $configuration += $this->configuration;
    $configuration += [
      'field_name' => '',
      'to_sid' => '',
      'comment' => "New state is set by a triggered Action.",
      'force' => 0,
    ];
    return $configuration;
  }

  /**
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *
   * @return \Drupal\workflow\Entity\WorkflowTransitionInterface
   */
  protected function getTransitionForExecution(EntityInterface $entity) {
    $user = workflow_current_user();
    if (!$entity) {
      \Drupal::logger('workflow_action')
        ->notice('Unable to get current entity - entity is not defined.', []);
      return NULL;
    }

    // Get the entity type and numeric ID.
    $entity_id = $entity
      ->id();
    if (!$entity_id) {
      \Drupal::logger('workflow_action')
        ->notice('Unable to get current entity ID - entity is not yet saved.', []);
      return NULL;
    }

    // In 'after saving new content', the node is already saved. Avoid second insert.
    // @todo Clone?
    $entity
      ->enforceIsNew(FALSE);
    $config = $this->configuration;
    $field_name = workflow_get_field_name($entity, $config['field_name']);
    $current_sid = workflow_node_current_state($entity, $field_name);
    if (!$current_sid) {
      \Drupal::logger('workflow_action')
        ->notice('Unable to get current workflow state of entity %id.', [
        '%id' => $entity_id,
      ]);
      return NULL;
    }
    $to_sid = isset($config['to_sid']) ? $config['to_sid'] : '';

    // Get the Comment. Parse the $comment variables.
    $comment_string = $this->configuration['comment'];
    $comment = $this
      ->t($comment_string, [
      '%title' => $entity
        ->label(),
      // "@" and "%" will automatically run check_plain().
      '%state' => workflow_get_sid_name($to_sid),
      '%user' => $user
        ->getDisplayName(),
    ]);
    $force = $this->configuration['force'];
    $transition = WorkflowTransition::create([
      $current_sid,
      'field_name' => $field_name,
    ]);
    $transition
      ->setTargetEntity($entity);
    $transition
      ->setValues($to_sid, $user
      ->id(), \Drupal::time()
      ->getRequestTime(), $comment);
    $transition
      ->force($force);
    return $transition;
  }

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

    // If we are on admin/config/system/actions and use CREATE AN ADVANCED ACTION
    // Then $context only contains:
    // - $context['actions_label'] = "Change workflow state of post to new state";
    // - $context['actions_type'] = "entity".
    //
    // If we are on a VBO action form, then $context only contains:
    // - $context['entity_type'] = "node";
    // - $context['view'] = "(Object) view";
    // - $context['settings'] = "[]".
    $config = $this->configuration;
    $field_name = $config['field_name'];
    $wids = workflow_get_workflow_names();
    if (empty($field_name) && count($wids) > 1) {
      $this
        ->messenger()
        ->addWarning('You have multiple workflows in the system. Please first select the field name
          and save the form. Then, revisit the form to set the correct state value.');
    }
    $wid = count($wids) ? array_keys($wids)[0] : '';
    if (!empty($field_name)) {
      $fields = _workflow_info_fields($entity = NULL, $entity_type = '', $entity_bundle = '', $field_name);
      $wid = count($fields) ? reset($fields)
        ->getSetting('workflow_type') : '';
    }

    // Get the common Workflow, or create a dummy Workflow.

    /** @var \Drupal\workflow\Entity\Workflow $workflow */
    $workflow = $wid ? Workflow::load($wid) : Workflow::create([
      'id' => 'dummy_action',
      'label' => 'dummy_action',
    ]);
    $current_state = $workflow
      ->getCreationState();

    /*
        // @todo D8-port for VBO
        // Show the current state and the Workflow form to allow state changing.
        // N.B. This part is replicated in hook_node_view, workflow_tab_page, workflow_vbo.
        if ($workflow) {
     $field = _workflow_info_field($field_name, $workflow);
     $field_name = $field['field_name'];
     $field_id = $field['id'];
     $instance = field_info_instance($entity_type, $field_name, $entity_bundle);

     // Hide the submit button. VBO has its own 'next' button.
     $instance['widget']['settings']['submit_function'] = '';
     if (!$field_id) {
       // This is a Workflow Node workflow. Set widget options as in v7.x-1.2
       $field['settings']['widget']['comment'] = $workflow->options['comment_log_node']; // 'comment_log_tab' is removed;
       $field['settings']['widget']['current_status'] = TRUE;
       // As stated above, the options list is probably very long, so let's use select list.
       $field['settings']['widget']['options'] = 'select';
       // Do not show the default [Update workflow] button on the form.
       $instance['widget']['settings']['submit_function'] = '';
     }
        }

        // Add the form/widget to the formatter, and include the nid and field_id in the form id,
        // to allow multiple forms per page (in listings, with hook_forms() ).
        // Ultimately, this is a wrapper for WorkflowDefaultWidget.
        // $form['workflow_current_state'] = workflow_state_formatter($entity_type, $entity, $field, $instance);
        $form_id = implode('_', [
     'workflow_transition_form',
     $entity_type,
     $entity_id,
     $field_id
        ]);
    */
    $user = workflow_current_user();
    $transition = WorkflowTransition::create([
      $current_state,
      'field_name' => $field_name,
    ]);
    $transition
      ->setValues($to_sid = $config['to_sid'], $user
      ->id(), \Drupal::time()
      ->getRequestTime(), $comment = $config['comment'], $force = $config['force']);

    // Add the WorkflowTransitionForm to the page. @todo
    $element = [];

    // Just to be explicit.
    $element['#default_value'] = $transition;

    // Avoid Action Buttons. That removes the options box&more. No Buttons in config screens!
    $original_options = $transition
      ->getWorkflow()
      ->getSetting('options');
    $transition
      ->getWorkflow()
      ->setSetting('options', 'select');

    // Generate and add the Workflow form element.
    $element = WorkflowTransitionElement::transitionElement($element, $form_state, $form);

    // Just to be sure, reset the options box setting.
    $transition
      ->getWorkflow()
      ->setSetting('options', $original_options);

    // Make adaptations for VBO-form:
    $element['field_name']['#access'] = TRUE;
    $element['force']['#access'] = TRUE;
    $element['to_sid']['#description'] = $this
      ->t('Please select the state that should be assigned when this action runs.');
    $element['comment']['#title'] = $this
      ->t('Message');
    $element['comment']['#description'] = $this
      ->t('This message will be written
      into the workflow history log when the action runs.
      You may include the following variables: %state, %title, %user.');
    $form['workflow_transition_action_config'] = $element;
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $configuration = $form_state
      ->getValue('workflow_transition_action_config');

    // Remove the transition: generates an error upon saving the action definition.
    unset($configuration['workflow_transition']);
    $this->configuration = $configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
    $access = AccessResult::allowed();
    return $return_as_object ? $access : $access
      ->isAllowed();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ActionBase::executeMultiple public function Executes the plugin for an array of objects. Overrides ActionInterface::executeMultiple 3
ConfigurableActionBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
ConfigurableActionBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
ConfigurableActionBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 2
ConfigurableActionBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 6
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
ExecutableInterface::execute public function Executes the plugin. 20
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
WorkflowStateActionBase::access public function Checks object access. Overrides ActionInterface::access
WorkflowStateActionBase::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm 2
WorkflowStateActionBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides ConfigurableActionBase::calculateDependencies 2
WorkflowStateActionBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
WorkflowStateActionBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableActionBase::defaultConfiguration
WorkflowStateActionBase::getTransitionForExecution protected function
WorkflowStateActionBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm