SearchPageFormBase.php 4.58 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\search\Form\SearchPageFormBase.
 */

namespace Drupal\search\Form;

10
use Drupal\Core\Entity\EntityForm;
11
use Drupal\Core\Entity\Query\QueryFactory;
12
use Drupal\Core\Form\FormStateInterface;
13 14 15 16 17
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\search\SearchPageRepositoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
18
 * Provides a base form for search pages.
19
 */
20
abstract class SearchPageFormBase extends EntityForm {
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

  /**
   * The entity being used by this form.
   *
   * @var \Drupal\search\SearchPageInterface
   */
  protected $entity;

  /**
   * The search plugin being configured.
   *
   * @var \Drupal\search\Plugin\SearchInterface
   */
  protected $plugin;

  /**
   * The entity query factory.
   *
   * @var \Drupal\Core\Entity\Query\QueryFactory
   */
  protected $entityQuery;

  /**
   * The search page repository.
   *
   * @var \Drupal\search\SearchPageRepositoryInterface
   */
  protected $searchPageRepository;

  /**
   * Constructs a new search form.
   *
   * @param \Drupal\Core\Entity\Query\QueryFactory $entity_query
   *   The entity query.
   * @param \Drupal\search\SearchPageRepositoryInterface $search_page_repository
   *   The search page repository.
   */
  public function __construct(QueryFactory $entity_query, SearchPageRepositoryInterface $search_page_repository) {
    $this->entityQuery = $entity_query;
    $this->searchPageRepository = $search_page_repository;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity.query'),
      $container->get('search.search_page_repository')
    );
  }

  /**
   * {@inheritdoc}
   */
76
  public function getBaseFormId() {
77 78 79 80 81 82
    return 'search_entity_form';
  }

  /**
   * {@inheritdoc}
   */
83
  public function buildForm(array $form, FormStateInterface $form_state) {
84 85 86 87 88 89 90
    $this->plugin = $this->entity->getPlugin();
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
91
  public function form(array $form, FormStateInterface $form_state) {
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    $form['label'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('Label'),
      '#description' => $this->t('The label for this search page.'),
      '#default_value' => $this->entity->label(),
      '#maxlength' => '255',
    );

    $form['id'] = array(
      '#type' => 'machine_name',
      '#default_value' => $this->entity->id(),
      '#disabled' => !$this->entity->isNew(),
      '#maxlength' => 64,
      '#machine_name' => array(
        'exists' => array($this, 'exists'),
      ),
    );
    $form['path'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('Path'),
      '#field_prefix' => 'search/',
      '#default_value' => $this->entity->getPath(),
      '#maxlength' => '255',
    );
    $form['plugin'] = array(
      '#type' => 'value',
      '#value' => $this->entity->get('plugin'),
    );

    if ($this->plugin instanceof PluginFormInterface) {
      $form += $this->plugin->buildConfigurationForm($form, $form_state);
    }

    return parent::form($form, $form_state);
  }

  /**
   * Determines if the search page entity already exists.
   *
   * @param string $id
   *   The search configuration ID.
   *
   * @return bool
   *   TRUE if the search configuration exists, FALSE otherwise.
   */
  public function exists($id) {
    $entity = $this->entityQuery->get('search_page')
      ->condition('id', $id)
      ->execute();
    return (bool) $entity;
  }

  /**
   * {@inheritdoc}
   */
147
  public function validate(array $form, FormStateInterface $form_state) {
148 149 150 151
    parent::validate($form, $form_state);

    // Ensure each path is unique.
    $path = $this->entityQuery->get('search_page')
152 153
      ->condition('path', $form_state->getValue('path'))
      ->condition('id', $form_state->getValue('id'), '<>')
154 155
      ->execute();
    if ($path) {
156
      $form_state->setErrorByName('path', $this->t('The search page path must be unique.'));
157 158 159 160 161 162 163 164 165 166
    }

    if ($this->plugin instanceof PluginFormInterface) {
      $this->plugin->validateConfigurationForm($form, $form_state);
    }
  }

  /**
   * {@inheritdoc}
   */
167 168
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
169 170 171 172 173 174 175 176 177 178

    if ($this->plugin instanceof PluginFormInterface) {
      $this->plugin->submitConfigurationForm($form, $form_state);
    }
    return $this->entity;
  }

  /**
   * {@inheritdoc}
   */
179
  public function save(array $form, FormStateInterface $form_state) {
180 181
    $this->entity->save();

182
    $form_state->setRedirectUrl($this->entity->urlInfo('collection'));
183 184 185
  }

}