3 namespace Drupal\search\Form;
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Plugin\PluginFormInterface;
8 use Drupal\search\SearchPageRepositoryInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
12 * Provides a base form for search pages.
14 abstract class SearchPageFormBase extends EntityForm {
17 * The entity being used by this form.
19 * @var \Drupal\search\SearchPageInterface
24 * The search plugin being configured.
26 * @var \Drupal\search\Plugin\SearchInterface
31 * The search page repository.
33 * @var \Drupal\search\SearchPageRepositoryInterface
35 protected $searchPageRepository;
38 * Constructs a new search form.
40 * @param \Drupal\search\SearchPageRepositoryInterface $search_page_repository
41 * The search page repository.
43 public function __construct(SearchPageRepositoryInterface $search_page_repository) {
44 $this->searchPageRepository = $search_page_repository;
50 public static function create(ContainerInterface $container) {
52 $container->get('search.search_page_repository')
59 public function getBaseFormId() {
60 return 'search_entity_form';
66 public function buildForm(array $form, FormStateInterface $form_state) {
67 $this->plugin = $this->entity->getPlugin();
68 return parent::buildForm($form, $form_state);
74 public function form(array $form, FormStateInterface $form_state) {
76 '#type' => 'textfield',
77 '#title' => $this->t('Label'),
78 '#description' => $this->t('The label for this search page.'),
79 '#default_value' => $this->entity->label(),
80 '#maxlength' => '255',
84 '#type' => 'machine_name',
85 '#default_value' => $this->entity->id(),
86 '#disabled' => !$this->entity->isNew(),
89 'exists' => [$this, 'exists'],
93 '#type' => 'textfield',
94 '#title' => $this->t('Path'),
95 '#field_prefix' => 'search/',
96 '#default_value' => $this->entity->getPath(),
97 '#maxlength' => '255',
102 '#value' => $this->entity->get('plugin'),
105 if ($this->plugin instanceof PluginFormInterface) {
106 $form += $this->plugin->buildConfigurationForm($form, $form_state);
109 return parent::form($form, $form_state);
113 * Determines if the search page entity already exists.
116 * The search configuration ID.
119 * TRUE if the search configuration exists, FALSE otherwise.
121 public function exists($id) {
122 $entity = $this->entityTypeManager->getStorage('search_page')->getQuery()
123 ->condition('id', $id)
125 return (bool) $entity;
131 public function validateForm(array &$form, FormStateInterface $form_state) {
132 parent::validateForm($form, $form_state);
134 // Ensure each path is unique.
135 $path = $this->entityTypeManager->getStorage('search_page')->getQuery()
136 ->condition('path', $form_state->getValue('path'))
137 ->condition('id', $form_state->getValue('id'), '<>')
140 $form_state->setErrorByName('path', $this->t('The search page path must be unique.'));
143 if ($this->plugin instanceof PluginFormInterface) {
144 $this->plugin->validateConfigurationForm($form, $form_state);
151 public function submitForm(array &$form, FormStateInterface $form_state) {
152 parent::submitForm($form, $form_state);
154 if ($this->plugin instanceof PluginFormInterface) {
155 $this->plugin->submitConfigurationForm($form, $form_state);
157 return $this->entity;
163 public function save(array $form, FormStateInterface $form_state) {
164 $this->entity->save();
166 $form_state->setRedirectUrl($this->entity->urlInfo('collection'));