You are here

function search_api_solr_form_search_api_index_form_alter in Search API Solr 8.2

Same name and namespace in other branches
  1. 8.3 search_api_solr.module \search_api_solr_form_search_api_index_form_alter()
  2. 8 search_api_solr.module \search_api_solr_form_search_api_index_form_alter()
  3. 4.x search_api_solr.module \search_api_solr_form_search_api_index_form_alter()

Implements hook_form_FORM_alter.

File

./search_api_solr.module, line 133

Code

function search_api_solr_form_search_api_index_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // We need to restrict by form ID here because this function is also called
  // via hook_form_BASE_FORM_ID_alter (which is wrong, e.g. in the case of the
  // form ID search_api_field_config).
  if (in_array($form_id, [
    'search_api_index_form',
    'search_api_index_edit_form',
  ])) {
    if (isset($form['server'])) {
      $form['server']['#element_validate'][] = 'search_api_solr_form_search_api_index_form_validate_server';
    }
    $settings = [];

    /** @var \Drupal\search_api\IndexInterface $index */
    $index = $form_state
      ->getFormObject()
      ->getEntity();
    if (!$index
      ->isNew()) {
      $settings = $index
        ->getThirdPartySettings('search_api_solr');
    }
    $settings += search_api_solr_default_index_third_party_settings();
    $form['third_party_settings']['search_api_solr'] = [
      '#tree' => TRUE,
      '#type' => 'details',
      '#title' => t('Solr specific index options'),
      '#collapsed' => TRUE,
    ];
    $form['third_party_settings']['search_api_solr']['finalize'] = [
      '#type' => 'checkbox',
      '#title' => t('Finalize index before first search'),
      '#description' => t('If enabled, other modules could hook in to apply "finalizations" to the index after updates or deletions happend to index items.'),
      '#default_value' => $settings['finalize'],
    ];
    $form['third_party_settings']['search_api_solr']['commit_before_finalize'] = [
      '#type' => 'checkbox',
      '#title' => t('Wait for commit before first finalization'),
      '#description' => t('If enabled, Solr will be be forced to flush all commits before any "finalizations" will be applied.'),
      '#default_value' => $settings['commit_before_finalize'],
      '#states' => [
        'invisible' => [
          ':input[name="third_party_settings[search_api_solr][finalize]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['third_party_settings']['search_api_solr']['commit_after_finalize'] = [
      '#type' => 'checkbox',
      '#title' => t('Wait for commit after last finalization'),
      '#description' => t('If enabled, Solr will be be forced to flush all commits after the last "finalizations" have been applied.'),
      '#default_value' => $settings['commit_after_finalize'],
      '#states' => [
        'invisible' => [
          ':input[name="third_party_settings[search_api_solr][finalize]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['third_party_settings']['search_api_solr']['highlighter'] = [
      '#tree' => TRUE,
      '#type' => 'details',
      '#title' => t('Highlighter'),
      '#collapsed' => TRUE,
      '#description' => t('If "Retrieve result data from Solr" and "Highlight retrieved data" are selected for the Solr backend on the server edit page, these highlighting settings will be used.'),
    ];
    $form['third_party_settings']['search_api_solr']['highlighter']['maxAnalyzedChars'] = [
      '#type' => 'number',
      '#min' => 0,
      '#title' => t('maxAnalyzedChars'),
      '#description' => t('Specifies the number of characters into a document that Solr should look for suitable snippets.'),
      '#default_value' => $settings['highlighter']['maxAnalyzedChars'],
    ];
    $form['third_party_settings']['search_api_solr']['highlighter']['fragmenter'] = [
      '#type' => 'select',
      '#options' => [
        'gap' => 'gap',
        'regex' => 'regex',
      ],
      '#title' => t('fragmenter'),
      '#description' => t('Specifies a text snippet generator for highlighted text. The standard fragmenter is gap, which creates fixed-sized fragments with gaps for multi-valued fields. Another option is regex, which tries to create fragments that resemble a specified regular expression. This parameter accepts per-field overrdes.'),
      '#default_value' => $settings['highlighter']['fragmenter'],
    ];
    $form['third_party_settings']['search_api_solr']['highlighter']['regex'] = [
      '#tree' => TRUE,
      '#type' => 'details',
      '#title' => t('regex'),
      '#collapsed' => FALSE,
      '#states' => [
        'invisible' => [
          'select[name="third_party_settings[search_api_solr][highlighter][fragmenter]"]' => [
            'value' => 'gap',
          ],
        ],
      ],
    ];
    $form['third_party_settings']['search_api_solr']['highlighter']['regex']['slop'] = [
      '#type' => 'number',
      '#step' => 0.1,
      '#min' => 0,
      '#title' => t('regex.slop'),
      '#description' => t('When using the regex fragmenter, this parameter defines the factor by which the fragmenter can stray from the ideal fragment size (given by fragsize) to accommodate a regular expression. For instance, a slop of 0.2 with fragsize=100 should yield fragments between 80 and 120 characters in length. It is usually good to provide a slightly smaller fragsize value when using the regex fragmenter.'),
      '#default_value' => $settings['highlighter']['regex']['slop'],
    ];
    $form['third_party_settings']['search_api_solr']['highlighter']['regex']['pattern'] = [
      '#type' => 'textfield',
      '#title' => t('regex.pattern'),
      '#description' => t('Specifies the regular expression for fragmenting. This could be used to extract sentences.'),
      '#default_value' => $settings['highlighter']['regex']['pattern'],
    ];
    $form['third_party_settings']['search_api_solr']['highlighter']['regex']['maxAnalyzedChars'] = [
      '#type' => 'number',
      '#min' => 0,
      '#title' => t('regex.maxAnalyzedChars'),
      '#description' => t('Instructs Solr to analyze only this many characters from a field when using the regex fragmenter (after which, the fragmenter produces fixed-sized fragments). Applying a complicated regex to a huge field is computationally expensive.'),
      '#default_value' => $settings['highlighter']['regex']['maxAnalyzedChars'],
    ];
    $form['third_party_settings']['search_api_solr']['highlighter']['usePhraseHighlighter'] = [
      '#type' => 'checkbox',
      '#title' => t('usePhraseHighlighter'),
      '#description' => t('If set, Solr will highlight phrase queries (and other advanced position-sensitive queries) accurately. If false, the parts of the phrase will be highlighted everywhere instead of only when it forms the given phrase.'),
      '#default_value' => $settings['highlighter']['usePhraseHighlighter'],
    ];
    $form['third_party_settings']['search_api_solr']['highlighter']['highlightMultiTerm'] = [
      '#type' => 'checkbox',
      '#title' => t('highlightMultiTerm'),
      '#description' => t('If set, Solr will highlight wildcard queries (and other MultiTermQuery subclasses). If false, they won\'t be highlighted at all.'),
      '#default_value' => $settings['highlighter']['highlightMultiTerm'],
    ];
    $form['third_party_settings']['search_api_solr']['highlighter']['preserveMulti'] = [
      '#type' => 'checkbox',
      '#title' => t('preserveMulti'),
      '#description' => t('If set, multi-valued fields will return all values in the order they were saved in the index. If false, only values that match the highlight request will be returned.'),
      '#default_value' => $settings['highlighter']['preserveMulti'],
    ];
    $form['third_party_settings']['search_api_solr']['highlighter']['highlight']['mergeContiguous'] = [
      '#type' => 'checkbox',
      '#title' => t('mergeContiguous'),
      '#description' => t('Instructs Solr to collapse contiguous fragments into a single fragment. A value of true indicates contiguous fragments will be collapsed into single fragment. This parameter accepts per-field overrides. The default value, false, is also the backward-compatible setting.'),
      '#default_value' => $settings['highlighter']['highlight']['mergeContiguous'],
    ];
    $form['third_party_settings']['search_api_solr']['highlighter']['highlight']['requireFieldMatch'] = [
      '#type' => 'checkbox',
      '#title' => t('requireFieldMatch'),
      '#description' => t('If set, highlights terms only if they appear in the specified field. If not set, terms are highlighted in all requested fields regardless of which field matched the query.'),
      '#default_value' => $settings['highlighter']['highlight']['requireFieldMatch'],
    ];
    $form['third_party_settings']['search_api_solr']['highlighter']['highlight']['snippets'] = [
      '#type' => 'number',
      '#min' => 0,
      '#title' => t('snippets'),
      '#description' => t('Specifies maximum number of highlighted snippets to generate per field. It is possible for any number of snippets from zero to this value to be generated. This parameter accepts per-field overrides.'),
      '#default_value' => $settings['highlighter']['highlight']['snippets'],
    ];
    $form['third_party_settings']['search_api_solr']['highlighter']['highlight']['fragsize'] = [
      '#type' => 'number',
      '#min' => 0,
      '#title' => t('fragsize'),
      '#description' => t('Specifies the size, in characters, of fragments to consider for highlighting. 0 indicates that no fragmenting should be considered and the whole field value should be used. This parameter accepts per-field overrides.'),
      '#default_value' => $settings['highlighter']['highlight']['fragsize'],
    ];
    $form['third_party_settings']['search_api_solr']['advanced'] = [
      '#tree' => TRUE,
      '#type' => 'details',
      '#title' => t('Advanced'),
    ];
    $form['third_party_settings']['search_api_solr']['advanced']['index_prefix'] = [
      '#type' => 'textfield',
      '#title' => t('Index prefix'),
      '#description' => t("By default, the index ID in the Solr server is the same as the index's machine name in Drupal. This setting will let you specify an additional prefix. Only use alphanumeric characters and underscores. Since changing the prefix makes the currently indexed data inaccessible, you should not change this variable when no data is indexed."),
      '#default_value' => $settings['advanced']['index_prefix'],
    ];
  }
}