You are here

public function IndexSolrMultisiteCloneForm::form in Search API Solr 4.x

Same name and namespace in other branches
  1. 8.3 src/Form/IndexSolrMultisiteCloneForm.php \Drupal\search_api_solr\Form\IndexSolrMultisiteCloneForm::form()

Throws

\Drupal\search_api\SearchApiException

1 method overrides IndexSolrMultisiteCloneForm::form()
IndexSolrMultisiteUpdateForm::form in src/Form/IndexSolrMultisiteUpdateForm.php

File

src/Form/IndexSolrMultisiteCloneForm.php, line 21

Class

IndexSolrMultisiteCloneForm
Provides a form for the Index entity.

Namespace

Drupal\search_api_solr\Form

Code

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

  // If the form is being rebuilt, rebuild the entity with the current form
  // values.
  if ($form_state
    ->isRebuilding()) {

    // When the form is being built for an AJAX response the ID is not present
    // in $form_state. To ensure our entity is always valid, we're adding the
    // ID back.
    if (!$this->entity
      ->isNew()) {
      $form_state
        ->setValue('id', $this->entity
        ->id());
    }
    $this->entity = $this
      ->buildEntity($form, $form_state);
  }
  if (!$this->entity
    ->isNew()) {

    /** @var \Drupal\search_api\ServerInterface $server */
    $server = $this->entity
      ->getServerInstance();

    /** @var \Drupal\search_api_solr\SolrBackendInterface $backend */
    $backend = $server
      ->getBackend();

    /** @var \Drupal\search_api\IndexInterface $index */
    $index = $this->entity
      ->createDuplicate();
    $fields = $index
      ->getFields();
    $solr_field_names = $backend
      ->getSolrFieldNames($this->entity);
    foreach ($this->pluginHelper
      ->createDatasourcePlugins($index, [
      'solr_multisite_document',
    ]) as $datasource_id => $datasource) {
      if ($datasource
        ->isHidden()) {
        continue;
      }
      $index
        ->setDatasources([
        $datasource_id => $datasource,
      ]);
    }
    foreach ($fields as $field_id => $field) {
      $field
        ->setDatasourceId('solr_multisite_document');
      $field
        ->setConfiguration([]);
      $field
        ->setPropertyPath($solr_field_names[$field_id]);
    }
    $index
      ->setFields($fields);
    $target_index = $backend
      ->getTargetedIndexId($this->entity);
    $target_index_machine_name = $this->entity
      ->id();
    $this->entity = $index;
  }
  $form = parent::form($form, $form_state);
  $arguments = [
    '%label' => $index
      ->label(),
  ];
  $form['#title'] = $this
    ->t('Clone search index %label', $arguments);
  $this
    ->buildEntityForm($form, $form_state, $index);
  $site_hash = Utility::getSiteHash();
  $form['name']['#default_value'] = 'Multisite ' . $form['name']['#default_value'] . ' ' . $site_hash;
  $form['datasources']['#default_value'] = [
    'solr_multisite_document',
  ];
  $form['datasource_configs']['solr_multisite_document']['target_index']['#default_value'] = $target_index;
  $form['datasource_configs']['solr_multisite_document']['target_index_machine_name']['#default_value'] = $target_index_machine_name;
  $form['datasource_configs']['solr_multisite_document']['target_hash']['#default_value'] = $site_hash;
  $form['options']['read_only']['#default_value'] = TRUE;
  $form['status']['#default_value'] = FALSE;
  return $form;
}