You are here

public function FacetCloneForm::form in Facets 8

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/FacetCloneForm.php, line 68

Class

FacetCloneForm
Provides a form for creating and editing facets.

Namespace

Drupal\facets\Form

Code

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

  /** @var \Drupal\facets\FacetInterface $facet */
  $facet = $this
    ->getEntity();
  if (strpos($facet
    ->getFacetSourceId(), 'search_api:') === FALSE) {

    // We don't know how to clone other kinds of facets.
    $this
      ->messenger()
      ->addMessage($this
      ->t('We can only clone Search API based facets.'));
    return [];
  }

  /** @var \Drupal\search_api\Display\DisplayInterface[] $facet_source_definitions */
  $facet_source_definitions = $this->facetSourcePluginManager
    ->getDefinitions();

  // Get the base table from the facet source's view.
  $facet_source_id = $facet
    ->getFacetSourceId();
  $search_api_display_id = $facet_source_definitions[$facet_source_id]['display_id'];

  /** @var \Drupal\search_api\Display\DisplayInterface $current_display */
  $current_display = $this->displayPluginManager
    ->createInstance($search_api_display_id);
  $current_index = $current_display
    ->getIndex()
    ->id();

  // Create a list of all other search api displays that have the same index.
  $options = [];
  foreach ($facet_source_definitions as $source) {
    $current_display = $this->displayPluginManager
      ->createInstance($source['display_id']);
    if ($current_display
      ->getIndex()
      ->id() !== $current_index) {
      continue;
    }
    $options[$source['id']] = $source['label'];
  }
  $form['destination_facet_source'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t("Clone the facet to this facet source:"),
    '#options' => $options,
    '#required' => TRUE,
  ];
  $form['name'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Name'),
    '#description' => $this
      ->t('The administrative name used for this facet.'),
    '#default_value' => $this
      ->t('Duplicate of @label', [
      '@label' => $this->entity
        ->label(),
    ]),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#maxlength' => 50,
    '#required' => TRUE,
    '#default_value' => '',
    '#machine_name' => [
      'exists' => [
        $this->facetStorage,
        'load',
      ],
      'source' => [
        'name',
      ],
    ],
  ];
  return $form;
}