You are here

public function SavedSearchController::activateSearch in Search API Saved Searches 8

Activates a (currently disabled) saved search.

Parameters

\Drupal\search_api_saved_searches\SavedSearchInterface $search_api_saved_search: The saved search to activate.

Return value

\Symfony\Component\HttpFoundation\RedirectResponse A redirect to either the search page or the site's front page.

Throws

\Drupal\Core\Entity\EntityStorageException Thrown if saving the saved search failed.

1 string reference to 'SavedSearchController::activateSearch'
search_api_saved_searches.routing.yml in ./search_api_saved_searches.routing.yml
search_api_saved_searches.routing.yml

File

src/Controller/SavedSearchController.php, line 49

Class

SavedSearchController
Provides routes related to saved searches.

Namespace

Drupal\search_api_saved_searches\Controller

Code

public function activateSearch(SavedSearchInterface $search_api_saved_search) {

  // Not possible for saved searches that are already active.
  if ($search_api_saved_search
    ->get('status')->value) {
    throw new NotFoundHttpException();
  }
  $search_api_saved_search
    ->set('status', TRUE)
    ->save();
  $this
    ->messenger()
    ->addStatus($this
    ->t('Your saved search was successfully activated.'));
  $path = $search_api_saved_search
    ->getPath();
  if (!$path) {
    $url = Url::fromUri('internal:/', [
      'absolute' => TRUE,
    ]);
  }
  else {
    $url = Url::fromUserInput($path, [
      'absolute' => TRUE,
    ]);
  }
  return new RedirectResponse($url
    ->toString(), 302);
}