function TempstoreConverter::convert

Same name in other branches
  1. 8.x-3.x src/ParamConverter/TempstoreConverter.php \Drupal\ctools\ParamConverter\TempstoreConverter::convert()

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);
    [
        ,
        $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;
    }
}