Added the Search API Synonym module to deal specifically with licence and license...
[yaffs-website] / web / modules / contrib / search_api_synonym / src / Form / SynonymForm.php
1 <?php
2
3 namespace Drupal\search_api_synonym\Form;
4
5 use Drupal\Core\Entity\ContentEntityForm;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Form controller for Synonym edit forms.
10  *
11  * @ingroup search_api_synonym
12  */
13 class SynonymForm extends ContentEntityForm {
14
15   /**
16    * {@inheritdoc}
17    */
18   public function buildForm(array $form, FormStateInterface $form_state) {
19     // Trim whitespaces from synonyms and save back into the form state.
20     /* @var \Drupal\search_api_synonym\SynonymInterface $entity */
21     $entity = $this->entity;
22     $synonyms = $entity->getSynonymsFormatted();
23     if (!empty($synonyms)) {
24       $entity->setSynonyms($synonyms);
25     }
26
27     return parent::buildForm($form, $form_state);
28   }
29
30   /**
31    * {@inheritdoc}
32    */
33   public function save(array $form, FormStateInterface $form_state) {
34     // Trim whitespaces from synonyms and save back into the form state.
35     /* @var \Drupal\search_api_synonym\SynonymInterface $entity */
36     $entity = $this->entity;
37     $trimmed = array_map('trim', explode(',', $entity->getSynonyms()));
38     $entity->setSynonyms(implode(',', $trimmed));
39
40     // Save synonym.
41     $status = parent::save($form, $form_state);
42
43     switch ($status) {
44       case SAVED_NEW:
45         drupal_set_message($this->t('Created the %label Synonym.', [
46           '%label' => $entity->label(),
47         ]));
48         break;
49
50       default:
51         drupal_set_message($this->t('Saved the %label Synonym.', [
52           '%label' => $entity->label(),
53         ]));
54     }
55
56     $form_state->setRedirect($entity->toUrl('collection')->getRouteName());
57   }
58
59 }