You are here

function search_api_admin_index_status_form in Search API 7

Form constructor for an index status form.

Should only be used for enabled indexes which aren't read-only.

Parameters

SearchApiIndex $index: The index whose status should be displayed.

array $status: The indexing status of the index, as returned by search_api_index_status().

See also

search_api_admin_index_status_form_validate()

search_api_admin_index_status_form_submit()

1 string reference to 'search_api_admin_index_status_form'
search_api_admin_index_view in ./search_api.admin.inc
Page callback for displaying an index's status.

File

./search_api.admin.inc, line 1146
Administration page callbacks for the Search API module.

Code

function search_api_admin_index_status_form(array $form, array &$form_state, SearchApiIndex $index, array $status) {
  $form['#attached']['css'][] = drupal_get_path('module', 'search_api') . '/search_api.admin.css';
  $form_state['index'] = $index;
  $form['index'] = array(
    '#type' => 'fieldset',
    '#title' => t('Index now'),
  );
  $form['index']['#attributes']['class'][] = 'container-inline';
  $allow_indexing = $status['indexed'] < $status['total'];
  $all = t('all', array(), array(
    'context' => 'items to index',
  ));
  $limit = array(
    '#type' => 'textfield',
    '#default_value' => $all,
    '#size' => 4,
    '#attributes' => array(
      'class' => array(
        'search-api-limit',
      ),
    ),
    '#disabled' => !$allow_indexing,
  );
  $batch_size = empty($index->options['cron_limit']) ? SEARCH_API_DEFAULT_CRON_LIMIT : $index->options['cron_limit'];
  $batch_size = $batch_size > 0 ? $batch_size : $all;
  $batch_size = array(
    '#type' => 'textfield',
    '#default_value' => $batch_size,
    '#size' => 4,
    '#attributes' => array(
      'class' => array(
        'search-api-batch-size',
      ),
    ),
    '#disabled' => !$allow_indexing,
  );

  // 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 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 = t('Index @limit items in batches of @batch_size items');
  $sentence = preg_split('/@(limit|batch_size)/', $sentence, -1, PREG_SPLIT_DELIM_CAPTURE);
  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 {

    // PANIC!
    $limit['#title'] = t('Number of items to index');
    $form['index']['limit'] = $limit;
    $batch_size['#title'] = t('Number of items per batch run');
    $form['index']['batch_size'] = $batch_size;
  }
  $form['index']['button'] = array(
    '#type' => 'submit',
    '#value' => t('Index now'),
    '#disabled' => !$allow_indexing,
  );
  $form['index']['total'] = array(
    '#type' => 'value',
    '#value' => $status['total'],
  );
  $form['index']['remaining'] = array(
    '#type' => 'value',
    '#value' => $status['total'] - $status['indexed'],
  );
  $form['index']['all'] = array(
    '#type' => 'value',
    '#value' => $all,
  );
  $form['reindex'] = array(
    '#type' => 'submit',
    '#value' => t('Queue all items for reindexing'),
    '#prefix' => '<div>',
    '#suffix' => '</div>',
  );
  $form['clear'] = array(
    '#type' => 'submit',
    '#value' => t('Clear all indexed data'),
    '#prefix' => '<div>',
    '#suffix' => '</div>',
  );
  return $form;
}