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
diff --git a/web/modules/contrib/search_api_synonym/src/Form/SynonymForm.php b/web/modules/contrib/search_api_synonym/src/Form/SynonymForm.php
new file mode 100644 (file)
index 0000000..87cd8a9
--- /dev/null
@@ -0,0 +1,59 @@
+<?php
+
+namespace Drupal\search_api_synonym\Form;
+
+use Drupal\Core\Entity\ContentEntityForm;
+use Drupal\Core\Form\FormStateInterface;
+
+/**
+ * Form controller for Synonym edit forms.
+ *
+ * @ingroup search_api_synonym
+ */
+class SynonymForm extends ContentEntityForm {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function buildForm(array $form, FormStateInterface $form_state) {
+    // Trim whitespaces from synonyms and save back into the form state.
+    /* @var \Drupal\search_api_synonym\SynonymInterface $entity */
+    $entity = $this->entity;
+    $synonyms = $entity->getSynonymsFormatted();
+    if (!empty($synonyms)) {
+      $entity->setSynonyms($synonyms);
+    }
+
+    return parent::buildForm($form, $form_state);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function save(array $form, FormStateInterface $form_state) {
+    // Trim whitespaces from synonyms and save back into the form state.
+    /* @var \Drupal\search_api_synonym\SynonymInterface $entity */
+    $entity = $this->entity;
+    $trimmed = array_map('trim', explode(',', $entity->getSynonyms()));
+    $entity->setSynonyms(implode(',', $trimmed));
+
+    // Save synonym.
+    $status = parent::save($form, $form_state);
+
+    switch ($status) {
+      case SAVED_NEW:
+        drupal_set_message($this->t('Created the %label Synonym.', [
+          '%label' => $entity->label(),
+        ]));
+        break;
+
+      default:
+        drupal_set_message($this->t('Saved the %label Synonym.', [
+          '%label' => $entity->label(),
+        ]));
+    }
+
+    $form_state->setRedirect($entity->toUrl('collection')->getRouteName());
+  }
+
+}