You are here

public function MaestroReassign::buildForm in Maestro 3.x

Same name and namespace in other branches
  1. 8.2 src/Form/MaestroReassign.php \Drupal\maestro\Form\MaestroReassign::buildForm()

This is the reassignment form.

Overrides FormInterface::buildForm

File

src/Form/MaestroReassign.php, line 25

Class

MaestroReassign
The Maestro Reassign form.

Namespace

Drupal\maestro\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $assignmentID = NULL) {

  // first, see if this is a legit assignment ID.
  $assignRecord = \Drupal::entityTypeManager()
    ->getStorage('maestro_production_assignments')
    ->load($assignmentID);
  if ($assignRecord) {
    $queueRecord = MaestroEngine::getQueueEntryById($assignRecord->queue_id
      ->getString());

    // now, display what's in the assignment record and based on the assign type, let this module or others set the allowable selections to be displayed.
    $form = [];
    $form['assignment_id'] = [
      '#type' => 'hidden',
      '#default_value' => $assignmentID,
    ];
    $form['assign_type'] = [
      '#type' => 'hidden',
      '#default_value' => $assignRecord->assign_type
        ->getString(),
    ];
    $form['assignment_table'] = [
      '#type' => 'table',
      '#caption' => $this
        ->t('Current Assignment'),
      '#header' => [
        $this
          ->t('Task'),
        $this
          ->t('By'),
        $this
          ->t('Assigned'),
        $this
          ->t('How Assigned'),
      ],
      // This really shouldn't happen, but it's a catch all.
      '#empty' => $this
        ->t('Nothing to reassign!'),
    ];
    $form['assignment_table'][0]['task'] = [
      '#plain_text' => $queueRecord->task_label
        ->getString(),
    ];
    $form['assignment_table'][0]['by'] = [
      '#plain_text' => $assignRecord->assign_type
        ->getString(),
    ];
    $form['assignment_table'][0]['assigned'] = [
      '#plain_text' => $assignRecord->assign_id
        ->getString(),
    ];
    $form['assignment_table'][0]['by_variable'] = [
      '#plain_text' => $assignRecord->by_variable
        ->getString() == 0 ? $this
        ->t('Fixed') : $this
        ->t('Variable'),
    ];

    // OK, so now, when we reassign this task, we will be changing the assignment to fixed no matter if it is by variable....
    // Developers/Administrator note:  Please note that a by-variable assignment can get augmented on-the-fly if the variable assignment alters
    //                                at some point during it's lifetime.  That means that this task may get assigned, at some point, to a variable value
    //                                along with the fixed assignment if the variable changes via the Maestro API.
    // now provide the reassignment mechanism here.  We provide user and role.  Other modules can provide whatever they want.
    // Provide a user lookup.
    if ($assignRecord->assign_type
      ->getString() == 'user') {
      $form['select_assigned_user'] = [
        '#id' => 'select_assigned_user',
        '#type' => 'entity_autocomplete',
        '#target_type' => 'user',
        '#default_value' => '',
        '#selection_settings' => [
          'include_anonymous' => FALSE,
        ],
        '#title' => $this
          ->t('User to Reassign To'),
        '#required' => FALSE,
      ];
    }
    elseif ($assignRecord->assign_type
      ->getString() == 'role') {
      $form['select_assigned_role'] = [
        '#id' => 'select_assigned_role',
        '#type' => 'textfield',
        '#default_value' => '',
        '#title' => $this
          ->t('Role to Reassign To'),
        '#autocomplete_route_name' => 'maestro.autocomplete.roles',
        '#required' => FALSE,
      ];
    }
    else {

      // TODO: let other modules handle their form elements here.
    }
    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Do Reassignment'),
    ];
    return $form;
  }
  else {
    \Drupal::messenger()
      ->addError(t('Invalid assignment record!'));
    return [
      '#markup' => $this
        ->t('Invalid Assignment Record. Operation Halted.'),
    ];
  }
}