You are here

function _search_api_saved_searches_claim_anonymous_searches in Search API Saved Searches 8

Reacts to the creation or activation of a new user account.

Associates all anonymously created saved searches with the same mail address with that user account.

Parameters

\Drupal\user\UserInterface $account: The user account in question.

2 calls to _search_api_saved_searches_claim_anonymous_searches()
search_api_saved_searches_user_insert in ./search_api_saved_searches.module
Implements hook_ENTITY_TYPE_insert() for type "user".
search_api_saved_searches_user_update in ./search_api_saved_searches.module
Implements hook_ENTITY_TYPE_update() for type "user".

File

./search_api_saved_searches.module, line 160
Allows visitors to bookmark searches and get notifications for new results.

Code

function _search_api_saved_searches_claim_anonymous_searches(UserInterface $account) {
  if (!$account
    ->isActive()) {
    return;
  }

  // Special case: This will silently fail if no saved search types use the
  // "E-mail" plugin – which is fine by us.
  $searches = _search_api_saved_searches_load_searches(0, $account
    ->getEmail());

  /** @var \Drupal\search_api_saved_searches\SavedSearchInterface $search */
  foreach ($searches as $search) {
    $search
      ->setOwner($account);
    try {
      $search
        ->save();
    } catch (EntityStorageException $e) {
      $args['@search_id'] = $search
        ->id();
      watchdog_exception('search_api_saved_searches', $e, '%type while trying to save saved search #@search_id: @message in %function (line %line of %file).', $args);
    }
  }
}