You are here

function apachesolr_multilingual_form_apachesolr_search_bias_form_alter in Apache Solr Multilingual 6.3

Same name and namespace in other branches
  1. 7 apachesolr_multilingual.module \apachesolr_multilingual_form_apachesolr_search_bias_form_alter()

Implements hook_form_apachesolr_search_bias_form_alter().

Parameters

$form:

$state: _id

Return value

mixed

File

./apachesolr_multilingual.module, line 285
Multilingual search using Apache Solr.

Code

function apachesolr_multilingual_form_apachesolr_search_bias_form_alter(&$form, &$form_state) {
  $environment = apachesolr_multilingual_environment_load($form['#env_id']);
  $fields = apachesolr_search_get_fields($environment);
  $unspecified_types = $types = array_unique(array_values(apachesolr_multilingual_get_dynamic_text_field_prefixes_and_types()));
  $defaults = array();
  foreach (array_keys(apachesolr_multilingual_language_list()) as $language_id) {
    foreach ($unspecified_types as $type) {
      if (strpos($type, 'text') === 0) {

        // matches text_und, too
        // Register language specific text types to add all fields based on this
        // these types to the bias form.
        $types[] = $type . '_' . $language_id;
      }
    }

    // get the current weights
    $defaults['i18n_content_' . $language_id] = '1.0';
    $defaults['i18n_ts_' . $language_id . '_comments'] = '0.5';
    $defaults['i18n_tos_' . $language_id . '_content_extra'] = '0.1';
    $defaults['i18n_label_' . $language_id] = '5.0';
    $defaults['i18n_tos_' . $language_id . '_name'] = '3.0';
    $defaults['i18n_taxonomy_names_' . $language_id] = '2.0';
    $defaults['i18n_tags_' . $language_id . '_h1'] = '5.0';
    $defaults['i18n_tags_' . $language_id . '_h2_h3'] = '3.0';
    $defaults['i18n_tags_' . $language_id . '_h4_h5_h6'] = '2.0';
    $defaults['i18n_tags_' . $language_id . '_inline'] = '1.0';
    $defaults['i18n_tags_' . $language_id . '_a'] = '0';
    $defaults['i18n_tus_' . $language_id . '_content'] = '0';
    $defaults['i18n_tus_' . $language_id . '_comments'] = '0';
    $defaults['i18n_tus_' . $language_id . '_label'] = '0';
    $defaults['i18n_tus_' . $language_id . '_teaser'] = '0';
    $defaults['i18n_tus_' . $language_id . '_path_alias'] = '0';
  }
  $qf = apachesolr_environment_variable_get($environment['env_id'], 'field_bias', $defaults);
  if (!$qf) {
    $qf = $defaults;
  }
  $incomplete = array();
  if ($fields) {
    foreach ($fields as $field_name => $field) {

      // Only indexed fields are searchable.
      if (in_array($field->type, $types) && $field->schema[0] == 'I') {
        $form['field_bias'][$field_name]['#access'] = TRUE;
        $form['field_bias'][$field_name]['#default_value'] = isset($qf[$field_name]) ? $qf[$field_name] : '0';
        $form['field_bias'][$field_name]['#description'] = in_array($field->type, $unspecified_types) ? t('Unspecified language: recommendation is set this bias to %omit.', array(
          '%omit' => t('Omit'),
        )) : '';
        if (!isset($qf[$field_name])) {
          $incomplete[] = $field_name;
        }
      }
    }

    // Make sure all the default fields are included, even if they have
    // no indexed content.
    $additional_vars_count = 0;
    foreach ($defaults as $field_name => $weight) {
      $form['field_bias'][$field_name] = array(
        '#type' => 'select',
        '#options' => $form['field_bias']['content']['#options'],
        '#title' => filter_xss(apachesolr_field_name_map($field_name)),
        '#default_value' => isset($qf[$field_name]) ? $qf[$field_name] : $defaults[$field_name],
      );
      if (!isset($qf[$field_name])) {
        $incomplete[] = $field_name;
      }
      $additional_vars_count++;
    }

    // Based on a default value of 1000 for max_input_vars this is a rough
    // estimation to warn the user about exceeding this setting.
    if ($additional_vars_count > ini_get('max_input_vars') - 400 || $additional_vars_count > ini_get('max_input_vars') * 0.6) {
      drupal_set_message(t('This settings form contains a lot of variables. If you are unable to save the settings try to increase the value of "max_input_vars" in your php.ini. If you use the suhosin extension you have to adjust the values of "suhosin.post.max_vars" and "suhosin.request.max_vars" as well.'), 'warning');
    }
    ksort($form['field_bias']);
    if (!empty($incomplete)) {
      foreach ($incomplete as $field_name) {

        // This field has never been configured before. Mark the field as
        // erroneous. This makes it easier to find the field even if we have a
        // lot of fields in this form.
        $form['field_bias'][$field_name]['#attributes']['class'] = 'error';

        // TODO use a theme function to format this message.
        $form['field_bias'][$field_name]['#description'] = (isset($form['field_bias'][$field_name]['#description']) ? $form['field_bias'][$field_name]['#description'] : '') . ' <span class="error">' . t('This field is new and needs to be configured.') . '</span>';
      }

      // The form contains at least one field that has never been configured
      // before. Display a corresponding message.
      drupal_set_message(t('This solr index contains new searchable fields. These fields are ignored until you configured and saved the !field_biases.', array(
        '!field_biases' => t('Field biases'),
      )), 'warning');
    }
  }
}