You are here

public function SolrDocument::buildConfigurationForm in Search API Solr 4.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/search_api/datasource/SolrDocument.php \Drupal\search_api_solr\Plugin\search_api\datasource\SolrDocument::buildConfigurationForm()
  2. 8.2 src/Plugin/search_api/datasource/SolrDocument.php \Drupal\search_api_solr\Plugin\search_api\datasource\SolrDocument::buildConfigurationForm()

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides PluginFormInterface::buildConfigurationForm

1 call to SolrDocument::buildConfigurationForm()
SolrMultisiteDocument::buildConfigurationForm in src/Plugin/search_api/datasource/SolrMultisiteDocument.php
Form constructor.
1 method overrides SolrDocument::buildConfigurationForm()
SolrMultisiteDocument::buildConfigurationForm in src/Plugin/search_api/datasource/SolrMultisiteDocument.php
Form constructor.

File

src/Plugin/search_api/datasource/SolrDocument.php, line 208

Class

SolrDocument
Represents a datasource which exposes external Solr Documents.

Namespace

Drupal\search_api_solr\Plugin\search_api\datasource

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {

  // Get the available fields from the server (if a server has already been
  // set).
  $fields = $single_valued_fields = [];
  foreach ($this
    ->getPropertyDefinitions() as $name => $property) {
    $fields[$name] = $property
      ->getLabel();
    if (!$property
      ->isList()) {
      $single_valued_fields[$name] = $property
        ->getLabel();
    }
  }
  $form['id_field'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('ID field'),
    '#required' => TRUE,
    '#description' => $this
      ->t('Enter the name of the field from your Solr schema that contains unique ID values.'),
    '#default_value' => $this->configuration['id_field'],
  ];

  // If there is already a valid server, we can transform the text field into
  // a select box.
  if ($single_valued_fields) {
    $form['id_field']['#type'] = 'select';
    $form['id_field']['#options'] = $single_valued_fields;
    $form['id_field']['#description'] = $this
      ->t('Select the Solr index field that contains unique ID values.');
  }
  $form['advanced'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Advanced configuration'),
  ];
  $form['advanced']['request_handler'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Request handler'),
    '#description' => $this
      ->t("Enter the name of a requestHandler from the core's solrconfig.xml file.  This should only be necessary if you need to specify a handler to use other than the default."),
    '#default_value' => $this->configuration['request_handler'],
  ];
  $form['advanced']['default_query'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Default query'),
    '#description' => $this
      ->t("Enter a default query parameter. This is only necessary if a default query cannot be specified in the solrconfig.xml file."),
    '#default_value' => $this->configuration['default_query'],
  ];
  $form['advanced']['label_field'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label field'),
    '#description' => $this
      ->t('Enter the name of the field from your Solr schema that should be considered the label (if any).'),
    '#default_value' => $this->configuration['label_field'],
  ];
  $form['advanced']['language_field'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Language field'),
    '#description' => $this
      ->t('Enter the name of the field from your Solr schema that should be considered the language (if any).'),
    '#default_value' => $this->configuration['language_field'],
  ];
  $form['advanced']['url_field'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('URL field'),
    '#description' => $this
      ->t('Enter the name of the field from your Solr schema that should be considered the URL (if any).'),
    '#default_value' => $this->configuration['url_field'],
  ];

  // If there is already a valid server, we can transform the text fields into
  // select boxes.
  if ($fields) {
    $fields = [
      '' => $this
        ->t('None'),
    ] + $fields;
    $form['advanced']['label_field']['#type'] = 'select';
    $form['advanced']['label_field']['#options'] = $fields;
    $form['advanced']['label_field']['#description'] = $this
      ->t('Select the Solr index field that should be considered the label (if any).');
    $form['advanced']['language_field']['#type'] = 'select';
    $form['advanced']['language_field']['#options'] = $fields;
    $form['advanced']['language_field']['#description'] = $this
      ->t("Select the Solr index field that contains the document's language code (if any).");
    $form['advanced']['url_field']['#type'] = 'select';
    $form['advanced']['url_field']['#options'] = $fields;
    $form['advanced']['url_field']['#description'] = $this
      ->t("Select the Solr index field that contains the document's URL (if any).");
  }
  return $form;
}