You are here

function maestro_form_alter in Maestro 8.2

Same name and namespace in other branches
  1. 3.x maestro.module \maestro_form_alter()

Implements hook_form_alter().

File

./maestro.module, line 164
Provides glue logic, hook implementation and core set process variable functions.

Code

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

  // We are detecting if we have a form where the incoming URL has a parameter called "maestro" in it.
  $queueID = intval(\Drupal::request()->query
    ->get('queueid', 0));
  $isMaestro = intval(\Drupal::request()->query
    ->get('maestro', 0));

  // nodeIsInUse signifies if we're looking at a node edit page and it is attached to a
  // maestro workflow that is still active.
  $nodeIsInUse = FALSE;

  // Lets see if the node is being used by a maestro workflow.
  $build_info = $form_state
    ->getBuildInfo();
  if (array_key_exists('base_form_id', $build_info) && $build_info['base_form_id'] == 'node_form') {
    $form_obj = $form_state
      ->getFormObject();
    $node = $form_obj
      ->getEntity();
    $bundle = 'node';
    $type = $node
      ->getType();
    $query = \Drupal::entityTypeManager()
      ->getStorage('maestro_entity_identifiers')
      ->getQuery();
    $query
      ->condition('entity_type', $bundle)
      ->condition('bundle', $type)
      ->condition('entity_id', $node
      ->id());
    $entity_ids = $query
      ->execute();
    if (count($entity_ids)) {
      $record = \Drupal::entityTypeManager()
        ->getStorage('maestro_entity_identifiers')
        ->load(current($entity_ids));
      if ($record) {
        $processID = $record->process_id
          ->getString();
        $processRecord = MaestroEngine::getProcessEntryById($processID);
        if ($processRecord->complete
          ->getString() == '0') {
          $nodeIsInUse = TRUE;
        }
      }
    }
  }
  if ($nodeIsInUse && !($isMaestro > 0 && $queueID > 0)) {

    // Do we signal the user that saving this node outside of a maestro controlled flow may cause issues?
    // do we use a hook to let devs set the signal?
    // do we have another option on the task definition?
    $form['maestro_information'] = [
      '#type' => 'fieldset',
      '#title' => t('Information about this content'),
      '#markup' => t('This content is attached to an active workflow. Saving this form may alter the active workflow that relies on this content.
          Saving the content will not complete any open workflow tasks.'),
      '#weight' => -100,
    ];
  }
  $templateTask = MaestroEngine::getTemplateTaskByQueueID($queueID);
  if ($isMaestro > 0 && $queueID > 0 && MaestroEngine::canUserExecuteTask($queueID, \Drupal::currentUser()
    ->id()) && $templateTask['tasktype'] == 'MaestroContentType') {
    $storage = $form_state
      ->getStorage();

    // Populate this form if its a content type.
    if ($isMaestro == 1) {
      $contentTypes = \Drupal::entityTypeManager()
        ->getStorage('node_type')
        ->loadMultiple();
      if (array_key_exists('form_display', $storage)) {
        $thisForm = $storage['form_display']
          ->get('bundle');
      }
      else {
        $thisForm = NULL;
      }

      // We have a content type match.
      if (array_key_exists($thisForm, $contentTypes) && $templateTask['data']['content_type'] == $thisForm) {

        // Add our fields to the content type.
        $form['maestro'] = [
          '#tree' => TRUE,
        ];
        $form['maestro']['type'] = [
          '#type' => 'hidden',
          '#default_value' => $thisForm,
          '#required' => TRUE,
        ];
        $form['maestro']['queue_id'] = [
          '#type' => 'hidden',
          '#default_value' => $queueID,
          '#required' => TRUE,
        ];
        $form['maestro']['process_id'] = [
          '#type' => 'hidden',
          '#default_value' => MaestroEngine::getProcessIdFromQueueId($queueID),
          '#required' => TRUE,
        ];

        // Add our own submit handler to the submit, publish and unpublish handlers.
        $form['actions']['submit']['#submit'][] = 'maestro_content_type_task_submit';
        $form['actions']['publish']['#submit'][] = 'maestro_content_type_task_submit';
        $form['actions']['unpublish']['#submit'][] = 'maestro_content_type_task_submit';

        // Now we add our own button that helps signify if we want to NOT end this task, only if the template says so.
        $task = MaestroEngine::getTemplateTaskByQueueID($queueID);
        if (array_key_exists('save_edit_later', $task['data']) && $task['data']['save_edit_later'] == 1) {
          $form['actions']['save_edit_later'] = [
            '#type' => 'submit',
            '#value' => t('Save and Edit Later'),
            '#submit' => $form['actions']['submit']['#submit'],
          ];
        }
      }
    }
  }
}