You are here

public function FacetsSummarySettingsForm::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

modules/facets_summary/src/Form/FacetsSummarySettingsForm.php, line 117

Class

FacetsSummarySettingsForm
Provides a form for configuring the processors of a facet.

Namespace

Drupal\facets_summary\Form

Code

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

  /** @var \Drupal\facets_summary\FacetsSummaryInterface $facets_summary */
  $facets_summary = $this->entity;
  $facet_sources = [];

  // If the form is being rebuilt, rebuild the entity with the current form
  // values.
  if ($form_state
    ->isRebuilding()) {
    $this->entity = $this
      ->buildEntity($form, $form_state);
  }
  $form = parent::form($form, $form_state);

  // Set the page title according to whether we are creating or editing the
  // facet.
  if ($this
    ->getEntity()
    ->isNew()) {
    $form['#title'] = $this
      ->t('Add facets summary');
  }
  else {
    $form['#title'] = $this
      ->t('Facets settings for %label', [
      '%label' => $this
        ->getEntity()
        ->label(),
    ]);
  }
  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 TRUE;
  }
  $form['facet_source_id'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Facet source'),
    '#description' => $this
      ->t('The source where this summary will be built from.'),
    '#options' => $facet_sources,
    '#default_value' => $facets_summary
      ->getFacetSourceId(),
    '#required' => TRUE,
  ];
  $form['name'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Name'),
    '#description' => $this
      ->t('The administrative name used for this summary.'),
    '#default_value' => $facets_summary
      ->label(),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $facets_summary
      ->id(),
    '#maxlength' => 50,
    '#required' => TRUE,
    '#machine_name' => [
      'exists' => [
        $this->facetSummaryStorage,
        'load',
      ],
      'source' => [
        'name',
      ],
    ],
  ];
  return $form;
}