You are here

public function SharedTempStore::setIfOwner in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/TempStore/SharedTempStore.php \Drupal\Core\TempStore\SharedTempStore::setIfOwner()
  2. 10 core/lib/Drupal/Core/TempStore/SharedTempStore.php \Drupal\Core\TempStore\SharedTempStore::setIfOwner()

Stores a particular key/value pair in this SharedTempStore.

Only stores the given key/value pair if it does not exist yet or is owned by $this->owner.

Parameters

string $key: The key of the data to store.

mixed $value: The data to store.

Return value

bool TRUE if the data was set, or FALSE if it already exists and is not owned by $this->user.

Throws

\Drupal\Core\TempStore\TempStoreException Thrown when a lock for the backend storage could not be acquired.

File

core/lib/Drupal/Core/TempStore/SharedTempStore.php, line 174

Class

SharedTempStore
Stores and retrieves temporary data for a given owner.

Namespace

Drupal\Core\TempStore

Code

public function setIfOwner($key, $value) {
  if ($this
    ->setIfNotExists($key, $value)) {
    return TRUE;
  }
  if (($object = $this->storage
    ->get($key)) && $object->owner == $this->owner) {
    $this
      ->set($key, $value);
    return TRUE;
  }
  return FALSE;
}