SavedSearchCreateForm.php in Search API Saved Searches 8
File
src/Form/SavedSearchCreateForm.php
View source
<?php
namespace Drupal\search_api_saved_searches\Form;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
class SavedSearchCreateForm extends ContentEntityForm {
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$bundle = $this->entity
->bundle();
$id = "search-api-saved-searches-save-{$bundle}-form-wrapper";
$form['#prefix'] = "<div id=\"{$id}\">";
$form['#suffix'] = '</div>';
return $form;
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this
->t('Save search');
$bundle = $this->entity
->bundle();
$id = "search-api-saved-searches-save-{$bundle}-form-wrapper";
$actions['submit']['#ajax'] = [
'callback' => '::saveFormAjax',
'wrapper' => $id,
'method' => 'replace',
'effect' => 'fade',
];
$actions['submit']['#executes_submit_callback'] = TRUE;
return $actions;
}
public function saveFormAjax(array $form, FormStateInterface $form_state) {
return $form_state
->getErrors() ? $form : [
'#type' => 'status_messages',
];
}
public function save(array $form, FormStateInterface $form_state) {
$return = parent::save($form, $form_state);
if ($return === SAVED_NEW) {
$search = $this->entity;
$enabled = $this->entity
->get('status')->value;
if ($enabled) {
if ($search
->get('notify_interval')->value < 0) {
$this
->messenger()
->addStatus($this
->t('Your saved search was successfully created.'));
}
else {
$this
->messenger()
->addStatus($this
->t('Your saved search was successfully created. You will receive notifications for new results in the future.'));
}
}
else {
$this
->messenger()
->addStatus($this
->t('Your saved search was successfully created. You will soon receive an e-mail with a confirmation link to activate it.'));
}
}
return $return;
}
}