function EntityConverter::convert

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/ParamConverter/EntityConverter.php \Drupal\Core\ParamConverter\EntityConverter::convert()
  2. 8.9.x core/lib/Drupal/Core/ParamConverter/EntityConverter.php \Drupal\Core\ParamConverter\EntityConverter::convert()
  3. 10 core/lib/Drupal/Core/ParamConverter/EntityConverter.php \Drupal\Core\ParamConverter\EntityConverter::convert()

Overrides ParamConverterInterface::convert

1 call to EntityConverter::convert()
AdminPathConfigEntityConverter::convert in core/lib/Drupal/Core/ParamConverter/AdminPathConfigEntityConverter.php
Converts path variables to their corresponding objects.
2 methods override EntityConverter::convert()
AdminPathConfigEntityConverter::convert in core/lib/Drupal/Core/ParamConverter/AdminPathConfigEntityConverter.php
Converts path variables to their corresponding objects.
EntityUuidConverter::convert in core/modules/jsonapi/src/ParamConverter/EntityUuidConverter.php
Converts path variables to their corresponding objects.

File

core/lib/Drupal/Core/ParamConverter/EntityConverter.php, line 115

Class

EntityConverter
Parameter converter for upcasting entity IDs to full objects.

Namespace

Drupal\Core\ParamConverter

Code

public function convert($value, $definition, $name, array $defaults) {
    $entity_type_id = $this->getEntityTypeFromDefaults($definition, $name, $defaults);
    // If the entity type is revisionable and the parameter has the
    // "load_latest_revision" flag, load the active variant.
    if (!empty($definition['load_latest_revision'])) {
        $entity = $this->entityRepository
            ->getActive($entity_type_id, $value);
        if (!empty($definition['bundle']) && $entity instanceof EntityInterface && !in_array($entity->bundle(), $definition['bundle'], TRUE)) {
            return NULL;
        }
        return $entity;
    }
    $contexts = [
        'operation' => 'entity_upcast',
    ];
    $entity = $this->entityRepository
        ->getCanonical($entity_type_id, $value, $contexts);
    if (!empty($definition['bundle']) && $entity instanceof EntityInterface && !in_array($entity->bundle(), $definition['bundle'], TRUE)) {
        return NULL;
    }
    return $entity;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.