You are here

public function SimplesitemapSitemapsForm::buildForm in Simple XML sitemap 8.3

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/SimplesitemapSitemapsForm.php, line 70

Class

SimplesitemapSitemapsForm
Class SimplesitemapSitemapsForm @package Drupal\simple_sitemap\Form

Namespace

Drupal\simple_sitemap\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['simple_sitemap_settings']['#prefix'] = FormHelper::getDonationText();
  $form['simple_sitemap_settings']['#attached']['library'][] = 'simple_sitemap/sitemaps';
  $queue_worker = $this->generator
    ->getQueueWorker();
  $form['simple_sitemap_settings']['status'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Sitemap status'),
    '#markup' => '<div class="description">' . $this
      ->t('Sitemaps can be regenerated on demand here.') . '</div>',
    '#description' => $this
      ->t('Variants can be configured <a href="@url">here</a>.', [
      '@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap/variants',
    ]),
  ];
  $form['simple_sitemap_settings']['status']['actions'] = [
    '#prefix' => '<div class="clearfix"><div class="form-item">',
    '#suffix' => '</div></div>',
  ];
  $form['simple_sitemap_settings']['status']['actions']['rebuild_queue_submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Rebuild queue'),
    '#submit' => [
      '::rebuildQueue',
    ],
    '#validate' => [],
  ];
  $form['simple_sitemap_settings']['status']['actions']['regenerate_submit'] = [
    '#type' => 'submit',
    '#value' => $queue_worker
      ->generationInProgress() ? $this
      ->t('Resume generation') : $this
      ->t('Rebuild queue & generate'),
    '#submit' => [
      '::generateSitemap',
    ],
    '#validate' => [],
  ];
  $form['simple_sitemap_settings']['status']['progress'] = [
    '#prefix' => '<div class="clearfix">',
    '#suffix' => '</div>',
  ];
  $form['simple_sitemap_settings']['status']['progress']['title']['#markup'] = $this
    ->t('Progress of sitemap regeneration');
  $total_count = $queue_worker
    ->getInitialElementCount();
  if (!empty($total_count)) {
    $indexed_count = $queue_worker
      ->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 && $queue_worker
      ->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 variant is published after all of its items have been processed.', [
        '@indexed' => $indexed_count,
        '@total' => $total_count,
      ]),
    ];
    $form['simple_sitemap_settings']['status']['progress']['bar']['#markup'] = render($index_progress);
  }
  else {
    $form['simple_sitemap_settings']['status']['progress']['bar']['#markup'] = '<div class="description">' . $this
      ->t('There are no items to be indexed.') . '</div>';
  }
  $sitemap_manager = $this->generator
    ->getSitemapManager();
  $sitemap_settings = [
    'base_url' => $this->generator
      ->getSetting('base_url', ''),
    'default_variant' => $this->generator
      ->getSetting('default_variant', NULL),
  ];
  $sitemap_statuses = $this
    ->fetchSitemapInstanceInfo();
  $published_timestamps = $this
    ->fetchSitemapInstancePublishedTimestamps();
  foreach ($sitemap_manager
    ->getSitemapTypes() as $type_name => $type_definition) {
    if (!empty($variants = $sitemap_manager
      ->getSitemapVariants($type_name, FALSE))) {
      $sitemap_generator = $sitemap_manager
        ->getSitemapGenerator($type_definition['sitemapGenerator'])
        ->setSettings($sitemap_settings);
      $form['simple_sitemap_settings']['status']['types'][$type_name] = [
        '#type' => 'details',
        '#title' => '<em>' . $type_definition['label'] . '</em> ' . $this
          ->t('sitemaps'),
        '#open' => !empty($variants) && count($variants) <= 5,
        '#description' => !empty($type_definition['description']) ? '<div class="description">' . $type_definition['description'] . '</div>' : '',
      ];
      $form['simple_sitemap_settings']['status']['types'][$type_name]['table'] = [
        '#type' => 'table',
        '#header' => [
          $this
            ->t('Variant'),
          $this
            ->t('Status'),
          $this
            ->t('Link count'),
        ],
        '#attributes' => [
          'class' => [
            'form-item',
            'clearfix',
          ],
        ],
      ];
      foreach ($variants as $variant_name => $variant_definition) {
        if (!isset($sitemap_statuses[$variant_name])) {
          $row['name']['data']['#markup'] = '<span title="' . $variant_name . '">' . $this
            ->t($variant_definition['label']) . '</span>';
          $row['status'] = $this
            ->t('pending');
          $row['count'] = '';
        }
        else {
          switch ($sitemap_statuses[$variant_name]['status']) {
            case 0:
              $row['name']['data']['#markup'] = '<span title="' . $variant_name . '">' . $this
                ->t($variant_definition['label']) . '</span>';
              $row['status'] = $this
                ->t('generating');
              $row['count'] = '';
              break;
            case 1:
            case 2:
              $row['name']['data']['#markup'] = $this
                ->t('<a href="@url" target="_blank">@variant</a>', [
                '@url' => $sitemap_generator
                  ->setSitemapVariant($variant_name)
                  ->getSitemapUrl(),
                '@variant' => $this
                  ->t($variant_definition['label']),
              ]);
              $row['status'] = $this
                ->t($sitemap_statuses[$variant_name]['status'] === 1 ? 'published on @time' : 'published on @time, regenerating', [
                '@time' => $this->dateFormatter
                  ->format($published_timestamps[$variant_name]),
              ]);

              // Once the sitemap has been regenerated after
              // simple_sitemap_update_8305() there will always be a link
              // count.
              $row['count'] = $sitemap_statuses[$variant_name]['link_count'] > 0 ? $sitemap_statuses[$variant_name]['link_count'] : $this
                ->t('unavailable');
              break;
          }
        }
        $form['simple_sitemap_settings']['status']['types'][$type_name]['table']['#rows'][$variant_name] = isset($row) ? $row : [];
        unset($sitemap_statuses[$variant_name]);
      }
    }
  }
  if (empty($form['simple_sitemap_settings']['status']['types'])) {
    $form['simple_sitemap_settings']['status']['types']['#markup'] = $this
      ->t('No variants have been defined');
  }
  return $form;
}