Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / search / src / Form / SearchPageFormBase.php
1 <?php
2
3 namespace Drupal\search\Form;
4
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;
10
11 /**
12  * Provides a base form for search pages.
13  */
14 abstract class SearchPageFormBase extends EntityForm {
15
16   /**
17    * The entity being used by this form.
18    *
19    * @var \Drupal\search\SearchPageInterface
20    */
21   protected $entity;
22
23   /**
24    * The search plugin being configured.
25    *
26    * @var \Drupal\search\Plugin\SearchInterface
27    */
28   protected $plugin;
29
30   /**
31    * The search page repository.
32    *
33    * @var \Drupal\search\SearchPageRepositoryInterface
34    */
35   protected $searchPageRepository;
36
37   /**
38    * Constructs a new search form.
39    *
40    * @param \Drupal\search\SearchPageRepositoryInterface $search_page_repository
41    *   The search page repository.
42    */
43   public function __construct(SearchPageRepositoryInterface $search_page_repository) {
44     $this->searchPageRepository = $search_page_repository;
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public static function create(ContainerInterface $container) {
51     return new static(
52       $container->get('search.search_page_repository')
53     );
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function getBaseFormId() {
60     return 'search_entity_form';
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public function buildForm(array $form, FormStateInterface $form_state) {
67     $this->plugin = $this->entity->getPlugin();
68     return parent::buildForm($form, $form_state);
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function form(array $form, FormStateInterface $form_state) {
75     $form['label'] = [
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',
81     ];
82
83     $form['id'] = [
84       '#type' => 'machine_name',
85       '#default_value' => $this->entity->id(),
86       '#disabled' => !$this->entity->isNew(),
87       '#maxlength' => 64,
88       '#machine_name' => [
89         'exists' => [$this, 'exists'],
90       ],
91     ];
92     $form['path'] = [
93       '#type' => 'textfield',
94       '#title' => $this->t('Path'),
95       '#field_prefix' => 'search/',
96       '#default_value' => $this->entity->getPath(),
97       '#maxlength' => '255',
98       '#required' => TRUE,
99     ];
100     $form['plugin'] = [
101       '#type' => 'value',
102       '#value' => $this->entity->get('plugin'),
103     ];
104
105     if ($this->plugin instanceof PluginFormInterface) {
106       $form += $this->plugin->buildConfigurationForm($form, $form_state);
107     }
108
109     return parent::form($form, $form_state);
110   }
111
112   /**
113    * Determines if the search page entity already exists.
114    *
115    * @param string $id
116    *   The search configuration ID.
117    *
118    * @return bool
119    *   TRUE if the search configuration exists, FALSE otherwise.
120    */
121   public function exists($id) {
122     $entity = $this->entityTypeManager->getStorage('search_page')->getQuery()
123       ->condition('id', $id)
124       ->execute();
125     return (bool) $entity;
126   }
127
128   /**
129    * {@inheritdoc}
130    */
131   public function validateForm(array &$form, FormStateInterface $form_state) {
132     parent::validateForm($form, $form_state);
133
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'), '<>')
138       ->execute();
139     if ($path) {
140       $form_state->setErrorByName('path', $this->t('The search page path must be unique.'));
141     }
142
143     if ($this->plugin instanceof PluginFormInterface) {
144       $this->plugin->validateConfigurationForm($form, $form_state);
145     }
146   }
147
148   /**
149    * {@inheritdoc}
150    */
151   public function submitForm(array &$form, FormStateInterface $form_state) {
152     parent::submitForm($form, $form_state);
153
154     if ($this->plugin instanceof PluginFormInterface) {
155       $this->plugin->submitConfigurationForm($form, $form_state);
156     }
157     return $this->entity;
158   }
159
160   /**
161    * {@inheritdoc}
162    */
163   public function save(array $form, FormStateInterface $form_state) {
164     $this->entity->save();
165
166     $form_state->setRedirectUrl($this->entity->urlInfo('collection'));
167   }
168
169 }