You are here

function apachesolr_reference_field_settings_form in Apachesolr Reference 7

Implements hook_field_settings_form().

File

./apachesolr_reference.module, line 504
functionality for creating reference fields to apache solr objects.

Code

function apachesolr_reference_field_settings_form($field, $instance, $has_data) {
  $settings = $field['settings'];
  $solr_environments = _apachesolr_reference_solr_environments();
  $options = array();
  foreach ($solr_environments as $env_id => $env) {
    $options[$env_id] = $env['name'];
  }
  $form['use_default'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use default Environment'),
    '#default_value' => $settings['use_default'],
  );
  $form['solr_env'] = array(
    '#type' => 'select',
    '#title' => t('SOLR Environment'),
    '#description' => t('Select the SOLR Environment to query.'),
    '#options' => $options,
    '#default_value' => $settings['solr_env'],
    '#required' => TRUE,
    '#states' => array(
      // Disable the settings when the default environment is used.
      'disabled' => array(
        ':input[name="field[settings][use_default]"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['field_query'] = array(
    '#type' => 'textfield',
    '#title' => t('Base Field Query'),
    '#description' => t('The Base SOLR Query to use when searching for objects. ex. bundle:(article)'),
    '#default_value' => $settings['field_query'],
    '#required' => FALSE,
  );

  // TODO: make this dynamic so that additional fields can be added.
  $form['search_fields'] = array(
    '#type' => 'fieldset',
    '#title' => t('SOLR Search Feilds'),
    '#description' => t('Map which fields from SOLR mare used for searching.'),
  );
  $form['search_fields']['id'] = array(
    '#type' => 'textfield',
    '#title' => t('ID'),
    '#description' => t('The field that hold the unique id value.'),
    '#default_value' => $settings['search_fields']['id'],
    '#required' => TRUE,
  );
  $form['search_fields']['label'] = array(
    '#type' => 'textfield',
    '#title' => t('Label'),
    '#description' => t('The field that holds the Display label value.'),
    '#default_value' => $settings['search_fields']['label'],
    '#required' => TRUE,
  );

  // TODO: make this checkboxes? powered by the SOLR module perhaps?
  $form['field_list'] = array(
    '#type' => 'textarea',
    '#title' => t('Field List'),
    '#description' => t('A comma seperated list of additional SOLR fields to return, to be used by field formatter.'),
    '#default_value' => $settings['field_list'],
  );
  module_load_include('module', 'apachesolr');
  $solr_env = _apachesolr_reference_default_solr_environment($settings);
  if ($solr_env && ($solr = apachesolr_get_solr($solr_env))) {
    $solr_fields = $solr
      ->getFields(0);
    $rows = array();
    foreach ($solr_fields as $solr_field_name => $solr_field) {

      // We do not allow to display 'sort_*' fields.
      if (strpos($solr_field_name, 'sort_') === 0) {
        continue;
      }
      $rows[] = array(
        $solr_field_name,
      );
    }
    $headers = array(
      'Field Name',
    );
    $form['available_fields'] = array(
      '#type' => 'fieldset',
      '#title' => t('Available Fields'),
      '#description' => t('Fields that are selectable from the SOLR environment.'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['available_fields']['field_table'] = array(
      '#markup' => theme('table', array(
        'header' => $headers,
        'rows' => $rows,
      )),
    );
  }
  return $form;
}