View source
<?php
namespace Drupal\maestro\Form;
use Drupal\user\Entity\User;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\maestro\Engine\MaestroEngine;
class MaestroReassign extends FormBase {
public function getFormId() {
return 'maestro_reassignment_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $assignmentID = NULL) {
$assignRecord = \Drupal::entityTypeManager()
->getStorage('maestro_production_assignments')
->load($assignmentID);
if ($assignRecord) {
$queueRecord = MaestroEngine::getQueueEntryById($assignRecord->queue_id
->getString());
$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'),
],
'#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'),
];
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 {
}
$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.'),
];
}
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$assign_type = $form_state
->getValue('assign_type');
if ($assign_type == 'user') {
$user = $form_state
->getValue('select_assigned_user');
if (!isset($user)) {
$form_state
->setErrorByName('select_assigned_user', $this
->t('You must choose a user to reassign to'));
}
}
elseif ($assign_type == 'role') {
$role = $form_state
->getValue('select_assigned_role');
if (!isset($role)) {
$form_state
->setErrorByName('select_assigned_role', $this
->t('You must choose a role to reassign to'));
}
}
else {
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$assign_type = $form_state
->getValue('assign_type');
$entity = '';
if ($assign_type == 'user') {
$uid = $form_state
->getValue('select_assigned_user');
$account = User::load($uid);
$entity = $account
->getDisplayName();
}
elseif ($assign_type == 'role') {
$entity = $form_state
->getValue('select_assigned_role');
}
else {
}
if (isset($entity)) {
$assignmentID = $form_state
->getValue('assignment_id');
$assignRecord = \Drupal::entityTypeManager()
->getStorage('maestro_production_assignments')
->load($assignmentID);
if ($assignRecord) {
$assignRecord
->set('assign_id', $entity);
$assignRecord
->set('by_variable', '0');
$assignRecord
->save();
}
}
}
}