You are here

public function TempstoreConverter::convert in Chaos Tool Suite (ctools) 8.3

Converts path variables to their corresponding objects.

Parameters

mixed $value: The raw value.

mixed $definition: The parameter definition provided in the route options.

string $name: The name of the parameter.

array $defaults: The route defaults array.

Return value

mixed|null The converted parameter value.

Overrides ParamConverterInterface::convert

File

src/ParamConverter/TempstoreConverter.php, line 109

Class

TempstoreConverter
Parameter converter for pulling entities out of the tempstore.

Namespace

Drupal\ctools\ParamConverter

Code

public function convert($value, $definition, $name, array $defaults) {
  $tempstore_id = !empty($definition['tempstore_id']) ? $definition['tempstore_id'] : $defaults['tempstore_id'];
  $machine_name = $this
    ->convertVariable($value, $defaults);
  list(, $parts) = explode(':', $definition['type'], 2);
  $parts = explode(':', $parts);
  foreach ($parts as $key => $part) {
    $parts[$key] = $this
      ->convertVariable($part, $defaults);
  }
  $cached_values = $this->tempstore
    ->get($tempstore_id)
    ->get($machine_name);

  // Entity type upcasting is most common, so we just assume that here.
  // @todo see if there's a better way to do this.
  if (!$cached_values && $this->entityTypeManager
    ->hasDefinition($name)) {
    $value = $this->entityTypeManager
      ->getStorage($name)
      ->load($machine_name);
    return $value;
  }
  elseif (!$cached_values) {
    return NULL;
  }
  else {
    $value = NestedArray::getValue($cached_values, $parts, $key_exists);
    return $key_exists ? $value : NULL;
  }
}