You are here

public function FacetSettingsForm::buildEntityForm in Facets 8

Builds the form for editing and creating a facet.

Parameters

array $form: The form array for the complete form.

\Drupal\Core\Form\FormStateInterface $form_state: The current form state.

\Drupal\facets\FacetInterface $facet: The facets facet entity that is being created or edited.

1 call to FacetSettingsForm::buildEntityForm()
FacetSettingsForm::form in src/Form/FacetSettingsForm.php
Gets the actual form array to be built.

File

src/Form/FacetSettingsForm.php, line 116

Class

FacetSettingsForm
Provides a form for creating and editing facets.

Namespace

Drupal\facets\Form

Code

public function buildEntityForm(array &$form, FormStateInterface $form_state, FacetInterface $facet) {
  $facet_sources = [];
  foreach ($this->facetSourcePluginManager
    ->getDefinitions() as $facet_source_id => $definition) {
    $facet_sources[$definition['id']] = !empty($definition['label']) ? $definition['label'] : $facet_source_id;
  }
  if (count($facet_sources) == 0) {
    $form['#markup'] = $this
      ->t('You currently have no facet sources defined. You should start by adding a facet source before creating facets.');
    return;
  }
  $form['facet_source_id'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Facet source'),
    '#description' => $this
      ->t('The source where this facet can find its fields.'),
    '#options' => $facet_sources,
    '#default_value' => $facet
      ->getFacetSourceId(),
    '#required' => TRUE,
    '#ajax' => [
      'trigger_as' => [
        'name' => 'facet_source_configure',
      ],
      'callback' => '::buildAjaxFacetSourceConfigForm',
      'wrapper' => 'facets-facet-sources-config-form',
      'method' => 'replace',
      'effect' => 'fade',
    ],
  ];
  $form['facet_source_configs'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'facets-facet-sources-config-form',
    ],
    '#tree' => TRUE,
  ];
  $form['facet_source_configure_button'] = [
    '#type' => 'submit',
    '#name' => 'facet_source_configure',
    '#value' => $this
      ->t('Configure facet source'),
    '#limit_validation_errors' => [
      [
        'facet_source_id',
      ],
    ],
    '#submit' => [
      '::submitAjaxFacetSourceConfigForm',
    ],
    '#ajax' => [
      'callback' => '::buildAjaxFacetSourceConfigForm',
      'wrapper' => 'facets-facet-sources-config-form',
    ],
    '#attributes' => [
      'class' => [
        'js-hide',
      ],
    ],
  ];
  $this
    ->buildFacetSourceConfigForm($form, $form_state);
  $form['name'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Name'),
    '#description' => $this
      ->t('The administrative name used for this facet.'),
    '#default_value' => $facet
      ->label(),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $facet
      ->id(),
    '#maxlength' => 50,
    '#required' => TRUE,
    '#machine_name' => [
      'exists' => [
        $this->facetStorage,
        'load',
      ],
      'source' => [
        'name',
      ],
    ],
    '#disabled' => !$facet
      ->isNew(),
  ];
}