function EntityConverter::getRouteRequirement

Returns a regex requirement for a route parameter.

Parameters

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

string $name: The name of the parameter.

Return value

string|null A regex pattern (without delimiters or anchors) suitable for Route::setRequirement(), or NULL if no requirement can be determined.

Overrides ParamConverterRouteRequirementInterface::getRouteRequirement

File

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

Class

EntityConverter
Parameter converter for upcasting entity IDs to full objects.

Namespace

Drupal\Core\ParamConverter

Code

public function getRouteRequirement($definition, $name) : ?string {
  $entity_type_id = substr($definition['type'], strlen('entity:'));
  // Cannot determine requirements for dynamic entity types at build time.
  if (str_contains($entity_type_id, '{')) {
    return NULL;
  }
  $entity_type = $this->entityTypeManager
    ->getDefinition($entity_type_id, FALSE);
  // Config entities: derive regex from config schema constraints.
  if ($entity_type instanceof ConfigEntityTypeInterface) {
    return static::PRINTABLE_ASCII;
  }
  return NULL;
}

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