You are here

function hook_form_workflow_transition_form_alter in Workflow 8

Same name and namespace in other branches
  1. 7.2 workflow.api.php \hook_form_workflow_transition_form_alter()

Implements hook_form_BASE_FORM_ID_alter().

Use this hook to alter the form. It is only suited if you only use View Page or Workflow Tab. If you change the state on the Entity Edit page (form), you need the hook hook_form_alter(). See below for more info.

1 call to hook_form_workflow_transition_form_alter()
workflow_devel_form_workflow_transition_form_alter in modules/workflow_devel/workflow_devel.module
@inheritdoc
1 function implements hook_form_workflow_transition_form_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

workflow_devel_form_workflow_transition_form_alter in modules/workflow_devel/workflow_devel.module
@inheritdoc

File

./workflow.api.php, line 322
Hooks provided by the workflow module.

Code

function hook_form_workflow_transition_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // workflow_debug(__FILE__, __FUNCTION__, __LINE__, $form_id, '');
  // The WorkflowTransitionForm (E.g., Workflow History tab, Block).
  // It has its own handling.
  // @todo Populate the WorkflowTransitionForm with a Widget, so we have 1 way-of-working.
  // Let's take a (changeable) reference to the element.
  $workflow_element =& $form;

  // This object contains all you need. You may find it in one of two locations.

  /** @var \Drupal\workflow\Entity\WorkflowTransitionInterface $transition */
  $transition = $form['workflow_transition']['#value'];

  // An example of customizing/overriding the workflow widget.
  // Beware, until now, you must do this twice: on the widget and on the form.
  if ($transition
    ->getOwnerId() == 1) {
    \Drupal::messenger()
      ->addWarning('(Test/Devel message) I got you, user 1,
      you will never schedule again, and you WILL document each state change!');

    // Let's prohibit scheduling for user 1.
    $workflow_element['workflow_scheduling']['#access'] = FALSE;

    // Let's prohibit scheduling for user 1.
    if ($workflow_element['comment']['#access'] == TRUE) {
      $workflow_element['comment']['#required'] = TRUE;
    }
  }

  // Get the Entity.

  /** @var \Drupal\Core\Entity\EntityInterface $entity */
  $entity = NULL;

  //$entity = $form['workflow_entity']['#value'];
  $entity_type = 'node';

  // $form['workflow_entity_type']['#value'];
  $entity_bundle = '';

  // $entity->bundle();
  $sid = '';
  if ($entity) {
    $entity_type = $entity
      ->getEntityTypeId();
    $entity_bundle = $entity
      ->bundle();

    // Get the current State ID.
    $sid = workflow_node_current_state($entity, $field_name = NULL);

    // Get the State object, if needed.
    $state = WorkflowState::load($sid);
  }

  // Change the form, depending on the state ID. @todo Update D8 machine name.
  // In the upcoming version 7.x-2.4, States should have a machine_name, too.
  if ($entity_type == 'node' && $entity_bundle == 'MY_NODE_TYPE') {
    switch ($sid) {
      case '2':

        // Change form element, form validate and form submit for state '2'.
        break;
      case '3':

        // Change form element, form validate and form submit for state '3'.
        break;
    }
  }
}