You are here

public function MaestroValidityCheck::buildForm in Maestro 8.2

Same name and namespace in other branches
  1. 3.x modules/maestro_template_builder/src/Form/MaestroValidityCheck.php \Drupal\maestro_template_builder\Form\MaestroValidityCheck::buildForm()

Ajax callback for add-new-form button click.

Overrides FormInterface::buildForm

File

modules/maestro_template_builder/src/Form/MaestroValidityCheck.php, line 69

Class

MaestroValidityCheck

Namespace

Drupal\maestro_template_builder\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $templateMachineName = '') {
  $template = MaestroEngine::getTemplate($templateMachineName);

  // Need to validate this template to ensure that it exists.
  if ($template == NULL) {
    $form = [
      '#title' => $this
        ->t('Error!'),
      '#markup' => $this
        ->t("The template you are attempting to add a task to doesn't exist"),
    ];
    return $form;
  }
  $items = MaestroEngine::performTemplateValidityCheck($templateMachineName);
  $form = [
    '#title' => $this
      ->t('Validity Check'),
  ];

  // Failures.
  if (count($items['failures'])) {
    $form['#prefix'] = '<div id="template-validity-check" class="messages messages--error">';
    foreach ($items['failures'] as $item) {
      $form['#prefix'] .= '<div class="template-validity-check-issue">';
      $form['#prefix'] .= '<span class="template-validity-check-label">' . $this
        ->t('Task ID') . ': </span>' . $item['taskID'] . "<br>";
      $form['#prefix'] .= '<span class="template-validity-check-label">' . $this
        ->t('Task Label') . ': </span>' . $item['taskLabel'] . "<br>";
      $form['#prefix'] .= '<span class="template-validity-check-label">' . $this
        ->t('Failure Note') . ': </span>' . $item['reason'] . "<br>";
      $form['#prefix'] .= '</div>';
    }
    $form['#prefix'] .= '</div>';
  }
  else {
    $form['#prefix'] = '<div id="template-validity-check" class="messages messages--status">' . $this
      ->t('Validity Check Passed') . "</div>";
  }

  // Information.
  if (count($items['information'])) {
    $form['#prefix'] .= '<div id="template-validity-check-information" class="messages messages--warning">';
    foreach ($items['information'] as $item) {
      $form['#prefix'] .= '<div class="template-validity-check-issue">';
      $form['#prefix'] .= '<span class="template-validity-check-label">' . $this
        ->t('Task ID') . ': </span>' . $item['taskID'] . "<br>";
      $form['#prefix'] .= '<span class="template-validity-check-label">' . $this
        ->t('Task Label') . ': </span>' . $item['taskLabel'] . "<br>";
      $form['#prefix'] .= '<span class="template-validity-check-label">' . $this
        ->t('Information Note') . ': </span>' . $item['reason'] . "<br>";
      $form['#prefix'] .= '</div>';
    }
    $form['#prefix'] .= '</div>';
  }
  $form['template_machine_name'] = [
    '#type' => 'hidden',
    '#default_value' => $templateMachineName,
  ];
  $form['actions']['cancel'] = [
    '#type' => 'button',
    '#value' => $this
      ->t('Save Template Validity'),
    '#required' => TRUE,
    '#ajax' => [
      'callback' => [
        $this,
        'cancelForm',
      ],
      'wrapper' => '',
    ],
  ];
  return $form;
}