You are here

public function IndexStatusForm::buildForm in Search API 8

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 FormInterface::buildForm

File

src/Form/IndexStatusForm.php, line 85

Class

IndexStatusForm
Provides a form for indexing, clearing, etc., an index.

Namespace

Drupal\search_api\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, IndexInterface $index = NULL) {
  if (!isset($index)) {
    return [];
  }
  $form['#index'] = $index;
  if ($index
    ->status() && $index
    ->hasValidTracker()) {
    $form['#attached']['library'][] = 'search_api/drupal.search_api.admin_css';
    if (!$this
      ->getIndexTaskManager()
      ->isTrackingComplete($index)) {
      $form['tracking'] = [
        '#type' => 'details',
        '#title' => $this
          ->t('Track items for index'),
        '#description' => $this
          ->t('Not all items have been tracked for this index. This means the displayed index status is incomplete and not all items will currently be indexed.'),
        '#open' => TRUE,
        '#attributes' => [
          'class' => [
            'container-inline',
          ],
        ],
      ];
      $form['tracking']['index_now'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Track now'),
        '#name' => 'track_now',
      ];
    }

    // Add the "Index now" form.
    $form['index'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Start indexing now'),
      '#open' => TRUE,
      '#attributes' => [
        'class' => [
          'container-inline',
        ],
      ],
    ];
    $has_remaining_items = $index
      ->getTrackerInstance()
      ->getRemainingItemsCount() > 0;
    $all_value = $this
      ->t('all', [], [
      'context' => 'items to index',
    ]);
    $limit = [
      '#type' => 'textfield',
      '#default_value' => $all_value,
      '#size' => 4,
      '#attributes' => [
        'class' => [
          'search-api-limit',
        ],
      ],
      '#disabled' => !$has_remaining_items,
    ];
    $batch_size = [
      '#type' => 'textfield',
      '#default_value' => $index
        ->getOption('cron_limit', $this
        ->config('search_api.settings')
        ->get('default_cron_limit')),
      '#size' => 4,
      '#attributes' => [
        'class' => [
          'search-api-batch-size',
        ],
      ],
      '#disabled' => !$has_remaining_items,
    ];

    // Here it gets complicated. We want to build a sentence from the form
    // input elements, but to translate that we have to make the two form
    // elements (for limit and batch size) pseudo-variables in the $this->t()
    // call.
    // Since we can't pass them directly, we split the translated sentence
    // (which still has the two tokens), figure out their order and then put
    // the pieces together again using the form elements' #prefix and #suffix
    // properties.
    $sentence = preg_split('/@(limit|batch_size)/', $this
      ->t('Index @limit items in batches of @batch_size items'), -1, PREG_SPLIT_DELIM_CAPTURE);

    // Check if the sentence contains the expected amount of parts.
    if (count($sentence) === 5) {
      $first = $sentence[1];
      $form['index'][$first] = ${$first};
      $form['index'][$first]['#prefix'] = $sentence[0];
      $form['index'][$first]['#suffix'] = $sentence[2];
      $second = $sentence[3];
      $form['index'][$second] = ${$second};
      $form['index'][$second]['#suffix'] = "{$sentence[4]} ";
    }
    else {

      // Sentence is broken. Use fallback method instead.
      $limit['#title'] = $this
        ->t('Number of items to index');
      $form['index']['limit'] = $limit;
      $batch_size['#title'] = $this
        ->t('Number of items per batch run');
      $form['index']['batch_size'] = $batch_size;
    }

    // Add the value "all" so it can be used by the validation.
    $form['index']['all'] = [
      '#type' => 'value',
      '#value' => $all_value,
    ];
    $form['index']['index_now'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Index now'),
      '#disabled' => !$has_remaining_items,
      '#name' => 'index_now',
    ];

    // Add actions for reindexing and for clearing the index.
    $form['actions']['#type'] = 'actions';
    $form['actions']['reindex'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Queue all items for reindexing'),
      '#name' => 'reindex',
      '#button_type' => 'danger',
    ];
    $form['actions']['clear'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Clear all indexed data'),
      '#name' => 'clear',
      '#button_type' => 'danger',
    ];
    $form['actions']['rebuild_tracker'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Rebuild tracking information'),
      '#name' => 'rebuild_tracker',
      '#button_type' => 'danger',
    ];
  }
  return $form;
}