You are here

function varbase_workflow_form_alter in Varbase Workflow 2.0.x

Implements hook_form_alter().

File

./varbase_workflow.module, line 16
Contains varbase_workflow.module.

Code

function varbase_workflow_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'node_type_add_form' || $form_id == 'node_type_edit_form') {
    $default_workflow_option = '_none';

    // List of workflows.workflow.* config in the site.
    $content_moderation_workflow_options = [
      '_none' => t('- none -'),
    ];
    $config_factory = \Drupal::service('config.factory');
    $workflows = $config_factory
      ->listAll('workflows.workflow.');
    foreach ($workflows as $workflow) {
      $content_moderation_workflow_options[$workflow] = $config_factory
        ->getEditable($workflow)
        ->get('label');
      if ($workflow == 'workflows.workflow.varbase_simple_workflow' && $default_workflow_option == '_none') {
        $default_workflow_option = 'workflows.workflow.varbase_simple_workflow';
      }
    }
    $workflows_configuration_page = Link::fromTextAndUrl(t('Workflows configuration page'), new Url('entity.workflow.collection'));
    $form['workflow']['content_moderation_workflow'] = [
      '#type' => 'select',
      '#title' => t('Content moderation workflow'),
      '#default_value' => $default_workflow_option,
      '#options' => $content_moderation_workflow_options,
      '#description' => t('Select the workflow you would like to use for this content type. Once selected, you can only change it for this content type from the @link.', [
        '@link' => $workflows_configuration_page
          ->toString(),
      ]),
    ];
    if ($form_id == 'node_type_add_form') {
      foreach (array_keys($form['actions']) as $action) {
        if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
          $form['actions'][$action]['#submit'][] = '_varbase_workflow_node_type_add_form';
        }
      }
    }
    else {
      $form['workflow']['content_moderation_workflow']['#attributes']['readonly'] = 'readonly';
      $form['workflow']['content_moderation_workflow']['#attributes']['disabled'] = 'disabled';
      $form['workflow']['content_moderation_workflow']['#default_value'] = '_none';
      foreach ($workflows as $workflow) {
        $workflow_type_settings = $config_factory
          ->getEditable($workflow)
          ->get('type_settings');
        $node_type = $form_state
          ->getFormObject()
          ->getEntity()
          ->get('type');
        if (isset($workflow_type_settings['entity_types']) && isset($workflow_type_settings['entity_types']['node'])) {
          if (in_array($node_type, $workflow_type_settings['entity_types']['node'])) {
            $form['workflow']['content_moderation_workflow']['#default_value'] = $workflow;
          }
        }
      }
    }
  }
  elseif (preg_match('/^node_.*._form$/', $form_id) && isset($form['moderation_state'])) {
    if (isset($form['publish_state'])) {
      $form['publish_state']['#access'] = FALSE;
      $form['publish_state']['widget'][0]['#default_value'] = 'published';
    }
    if (isset($form['unpublish_state'])) {
      $form['unpublish_state']['#access'] = FALSE;
      $form['unpublish_state']['widget'][0]['#default_value'] = 'archived';
    }
  }
  return $form;
}