You are here

function hook_field_widget_workflow_default_form_alter in Workflow 8

Same name and namespace in other branches
  1. 7.2 workflow.api.php \hook_field_widget_workflow_default_form_alter()
1 call to hook_field_widget_workflow_default_form_alter()
workflow_devel_field_widget_workflow_default_form_alter in modules/workflow_devel/workflow_devel.module
@inheritdoc
1 function implements hook_field_widget_workflow_default_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_field_widget_workflow_default_form_alter in modules/workflow_devel/workflow_devel.module
@inheritdoc

File

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

Code

function hook_field_widget_workflow_default_form_alter(&$element, FormStateInterface $form_state, $context) {

  // A hook specific for the 'workflow_default' widget.
  // D7: This hook is introduced in Drupal 7.8.
  // D8: This name is specified in the annotation of WorkflowDefaultWidget.
  workflow_debug(__FILE__, __FUNCTION__, __LINE__, '', '');

  // A widget on an entity form.
  if ('workflow_default' != $context['widget']
    ->getPluginId()) {
    return;
  }

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

  /** @var \Drupal\workflow\Entity\WorkflowTransitionInterface $transition */
  if (!isset($element['#default_value'])) {
    return;
  }
  if (!($transition = $element['#default_value'])) {
    return;
  }

  // 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.
    $element['workflow_scheduling']['#access'] = FALSE;

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