You are here

public function SearchApiAlterAddAggregation::configurationForm in Search API 7

Implements SearchApiAlterCallbackInterface::configurationForm().

Overrides SearchApiAbstractAlterCallback::configurationForm

File

includes/callback_add_aggregation.inc, line 34
Contains SearchApiAlterAddAggregation.

Class

SearchApiAlterAddAggregation
Search API data alteration callback that adds an URL field for all items.

Code

public function configurationForm() {
  $form['#attached']['css'][] = drupal_get_path('module', 'search_api') . '/search_api.admin.css';
  $fields = $this->index
    ->getFields(FALSE);
  $field_options = array();
  $field_properties = array();
  foreach ($fields as $name => $field) {
    $field_options[$name] = check_plain($field['name']);
    $field_properties[$name] = array(
      '#attributes' => array(
        'title' => $name,
      ),
      '#description' => check_plain($field['description']),
    );
  }
  $additional = empty($this->options['fields']) ? array() : $this->options['fields'];
  $types = $this
    ->getTypes();
  $type_descriptions = $this
    ->getTypes('description');
  $tmp = array();
  foreach ($types as $type => $name) {
    $tmp[$type] = array(
      '#type' => 'item',
      '#description' => $type_descriptions[$type],
    );
  }
  $type_descriptions = $tmp;
  $form['#id'] = 'edit-callbacks-search-api-alter-add-aggregation-settings';
  $form['description'] = array(
    '#markup' => t('<p>This data alteration lets you define additional fields that will be added to this index. ' . 'Each of these new fields will be an aggregation of one or more existing fields.</p>' . '<p>To add a new aggregated field, click the "Add new field" button and then fill out the form.</p>' . '<p>To remove a previously defined field, click the "Remove field" button.</p>' . '<p>You can also change the names or contained fields of existing aggregated fields.</p>'),
  );
  $form['fields']['#prefix'] = '<div id="search-api-alter-add-aggregation-field-settings">';
  $form['fields']['#suffix'] = '</div>';
  if (isset($this->changes)) {
    $form['fields']['#prefix'] .= '<div class="messages warning">All changes in the form will not be saved until the <em>Save configuration</em> button at the form bottom is clicked.</div>';
  }
  foreach ($additional as $name => $field) {
    $form['fields'][$name] = array(
      '#type' => 'fieldset',
      '#title' => $field['name'] ? $field['name'] : t('New field'),
      '#collapsible' => TRUE,
      '#collapsed' => (bool) $field['name'],
    );
    $form['fields'][$name]['name'] = array(
      '#type' => 'textfield',
      '#title' => t('New field name'),
      '#default_value' => $field['name'],
      '#required' => TRUE,
    );
    $form['fields'][$name]['type'] = array(
      '#type' => 'select',
      '#title' => t('Aggregation type'),
      '#options' => $types,
      '#default_value' => $field['type'],
      '#required' => TRUE,
    );
    $form['fields'][$name]['type_descriptions'] = $type_descriptions;
    $type_selector = ':input[name="callbacks[search_api_alter_add_aggregation][settings][fields][' . $name . '][type]"]';
    foreach (array_keys($types) as $type) {
      $form['fields'][$name]['type_descriptions'][$type]['#states']['visible'][$type_selector]['value'] = $type;
    }
    $form['fields'][$name]['separator'] = array(
      '#type' => 'textfield',
      '#title' => t('Fulltext separator'),
      '#description' => t('For aggregation type "Fulltext", set the text that should be used to separate the aggregated field values. Use "\\t" for tabs and "\\n" for newline characters.'),
      '#default_value' => addcslashes(isset($field['separator']) ? $field['separator'] : "\n\n", "\0..\37\\"),
      '#states' => array(
        'visible' => array(
          $type_selector => array(
            'value' => 'fulltext',
          ),
        ),
      ),
    );
    $form['fields'][$name]['fields'] = array_merge($field_properties, array(
      '#type' => 'checkboxes',
      '#title' => t('Contained fields'),
      '#options' => $field_options,
      '#default_value' => drupal_map_assoc($field['fields']),
      '#attributes' => array(
        'class' => array(
          'search-api-alter-add-aggregation-fields',
        ),
      ),
      '#required' => TRUE,
    ));
    $form['fields'][$name]['actions'] = array(
      '#type' => 'actions',
      'remove' => array(
        '#type' => 'submit',
        '#value' => t('Remove field'),
        '#submit' => array(
          '_search_api_add_aggregation_field_submit',
        ),
        '#limit_validation_errors' => array(),
        '#name' => 'search_api_add_aggregation_remove_' . $name,
        '#ajax' => array(
          'callback' => '_search_api_add_aggregation_field_ajax',
          'wrapper' => 'search-api-alter-add-aggregation-field-settings',
        ),
      ),
    );
  }
  $form['actions']['#type'] = 'actions';
  $form['actions']['add_field'] = array(
    '#type' => 'submit',
    '#value' => t('Add new field'),
    '#submit' => array(
      '_search_api_add_aggregation_field_submit',
    ),
    '#limit_validation_errors' => array(),
    '#ajax' => array(
      'callback' => '_search_api_add_aggregation_field_ajax',
      'wrapper' => 'search-api-alter-add-aggregation-field-settings',
    ),
  );
  return $form;
}