Skip to content

Instantly share code, notes, and snippets.

@fubhy
Created July 17, 2013 16:00
Show Gist options
  • Save fubhy/6021940 to your computer and use it in GitHub Desktop.
Save fubhy/6021940 to your computer and use it in GitHub Desktop.
+ public function convert($definition, $name, array $defaults, Request $request) {
+ // Only continue if there is a value for the given parameter.
+ if (!isset($defaults[$name])) {
+ return;
+ }
+ $entity_type = substr($definition['type'], strlen('entity:'));
+ if ($storage = $this->entityManager->getStorageController($entity_type)) {
+ return $storage->load($defaults[$name]);
+ }
+ }
public function process(array &$variables, Route $route, array &$converted) {
$variable_names = $route->compile()->getVariables();
$options = $route->getOptions();
$configuredTypes = isset($options['converters']) ? $options['converters'] : array();
$entityTypes = array_keys($this->entityManager->getDefinitions());
foreach ($variable_names as $name) {
// Do not process this variable if it's already marked as converted.
if (in_array($name, $converted)) {
continue;
}
// Obtain entity type to convert to from the route configuration or just
// use the variable name as default.
if (array_key_exists($name, $configuredTypes)) {
$type = $configuredTypes[$name];
}
else {
$type = $name;
}
if (in_array($type, $entityTypes)) {
$value = $variables[$name];
$storageController = $this->entityManager->getStorageController($type);
$entity = $storageController->load($value);
$variables[$name] = $entity;
// Mark this variable as converted.
$converted[] = $name;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment