You are here

public function SearchApiAlterAddCombined::configurationForm in Search API Combined Fields 7

Implements SearchApiAlterCallbackInterface::configurationForm().

Overrides SearchApiAbstractAlterCallback::configurationForm

File

./callback_add_combined.inc, line 8

Class

SearchApiAlterAddCombined
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';
  $form['#attached']['css'][] = drupal_get_path('module', 'search_api_combined') . '/search_api_combined.css';
  $fields = $this->index
    ->getFields(FALSE);
  $field_options = array();
  foreach ($fields as $name => $field) {
    $field_options[$name] = t('@label (@name)', array(
      '@label' => $field['name'],
      '@name' => $name,
    ));
  }
  $facets = array(
    '' => t('None'),
  ) + $field_options;
  $additional = empty($this->options['fields']) ? array() : $this->options['fields'];
  $form['#id'] = 'edit-callbacks-search-api-alter-add-combined-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 a combination of one or more existing fields.</p>' . '<p>To add a new combined 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 combined fields.</p>'),
  );
  $form['fields']['#prefix'] = '<div id="search-api-alter-add-combined-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]['multivalue'] = array(
      '#type' => 'radios',
      '#title' => t('Multi-value field'),
      '#options' => array(
        0 => t('No'),
        1 => t('Yes'),
      ),
      '#default_value' => TRUE === isset($field['multivalue']) ? $field['multivalue'] : 1,
      '#required' => TRUE,
      '#description' => t('Whether the combined field is a multi-valued field'),
    );
    $form['fields'][$name]['type'] = array(
      '#type' => 'select',
      '#title' => t('Data type'),
      '#options' => search_api_default_field_types(),
      '#default_value' => TRUE === isset($field['type']) ? $field['type'] : 'integer',
      '#required' => TRUE,
      '#description' => t('Data type to save field as'),
    );
    $form['fields'][$name]['imitate'] = array(
      '#type' => 'select',
      '#title' => t('Imitate field'),
      '#options' => $facets,
      '#default_value' => $field['imitate'],
      '#required' => FALSE,
      '#description' => t('Treat this field as another type of field, useful for making a combined taxonomy field behave as such for FacetAPI'),
    );
    $form['fields'][$name]['fields'] = 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-combined-fields',
        ),
      ),
      '#required' => TRUE,
    );
    $form['fields'][$name]['actions'] = array(
      '#type' => 'actions',
      'remove' => array(
        '#type' => 'submit',
        '#value' => t('Remove field'),
        '#submit' => array(
          '_search_api_add_combined_field_submit',
        ),
        '#limit_validation_errors' => array(),
        '#name' => 'search_api_add_combined_remove_' . $name,
        '#ajax' => array(
          'callback' => '_search_api_add_combined_field_ajax',
          'wrapper' => 'search-api-alter-add-combined-field-settings',
        ),
      ),
    );
  }
  $form['actions']['#type'] = 'actions';
  $form['actions']['add_field_combined'] = array(
    '#type' => 'submit',
    '#value' => t('Add new combined field'),
    '#submit' => array(
      '_search_api_add_combined_field_submit',
    ),
    '#limit_validation_errors' => array(),
    '#ajax' => array(
      'callback' => '_search_api_add_combined_field_ajax',
      'wrapper' => 'search-api-alter-add-combined-field-settings',
    ),
  );
  return $form;
}