You are here

public function WorkflowStateActionBase::buildConfigurationForm in Workflow 8

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides PluginFormInterface::buildConfigurationForm

2 calls to WorkflowStateActionBase::buildConfigurationForm()
WorkflowNodeGivenStateAction::buildConfigurationForm in src/Plugin/Action/WorkflowNodeGivenStateAction.php
Form constructor.
WorkflowNodeNextStateAction::buildConfigurationForm in src/Plugin/Action/WorkflowNodeNextStateAction.php
Form constructor.
2 methods override WorkflowStateActionBase::buildConfigurationForm()
WorkflowNodeGivenStateAction::buildConfigurationForm in src/Plugin/Action/WorkflowNodeGivenStateAction.php
Form constructor.
WorkflowNodeNextStateAction::buildConfigurationForm in src/Plugin/Action/WorkflowNodeNextStateAction.php
Form constructor.

File

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

Class

WorkflowStateActionBase
Sets an entity to a new, given state.

Namespace

Drupal\workflow\Plugin\Action

Code

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;
}