You are here

public function StatusForm::buildForm in Simple XML sitemap 4.x

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/StatusForm.php, line 90

Class

StatusForm
Class StatusForm

Namespace

Drupal\simple_sitemap\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) : array {
  $form['#attached']['library'][] = 'simple_sitemap/sitemaps';
  $form['status'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Sitemap status'),
    '#markup' => '<div class="description">' . $this
      ->t('Sitemaps can be regenerated on demand here.') . '</div>',
  ];
  $form['status']['actions'] = [
    '#prefix' => '<div class="clearfix"><div class="form-item">',
    '#suffix' => '</div></div>',
  ];
  $form['status']['actions']['rebuild_queue_submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Rebuild queue'),
    '#submit' => [
      self::class . '::rebuildQueue',
    ],
    '#validate' => [],
  ];
  $form['status']['actions']['regenerate_submit'] = [
    '#type' => 'submit',
    '#value' => $this->queueWorker
      ->generationInProgress() ? $this
      ->t('Resume generation') : $this
      ->t('Rebuild queue & generate'),
    '#submit' => [
      self::class . '::generateSitemap',
    ],
    '#validate' => [],
  ];
  $form['status']['progress'] = [
    '#prefix' => '<div class="clearfix">',
    '#suffix' => '</div>',
  ];
  $form['status']['progress']['title']['#markup'] = $this
    ->t('Progress of sitemap regeneration');
  $total_count = $this->queueWorker
    ->getInitialElementCount();
  if (!empty($total_count)) {
    $indexed_count = $this->queueWorker
      ->getProcessedElementCount();
    $percent = round(100 * $indexed_count / $total_count);

    // With all results processed, there still may be some stashed results to be indexed.
    $percent = $percent === 100 && $this->queueWorker
      ->generationInProgress() ? 99 : $percent;
    $index_progress = [
      '#theme' => 'progress_bar',
      '#percent' => $percent,
      '#message' => $this
        ->t('@indexed out of @total queue items have been processed.<br>Each sitemap is published after all of its items have been processed.', [
        '@indexed' => $indexed_count,
        '@total' => $total_count,
      ]),
    ];
    $form['status']['progress']['bar']['#markup'] = render($index_progress);
  }
  else {
    $form['status']['progress']['bar']['#markup'] = '<div class="description">' . $this
      ->t('There are no items to be indexed.') . '</div>';
  }
  return $form;
}