Browse Source

Patching up UUID param converter (#6)

pull/756/head
dannylamb 8 years ago committed by Nick Ruest
parent
commit
d0abe9aee9
  1. 8
      src/FedoraResourceHtmlRouteProvider.php
  2. 36
      src/ParamConverter/UuidEntityConverter.php

8
src/FedoraResourceHtmlRouteProvider.php

@ -41,7 +41,7 @@ class FedoraResourceHtmlRouteProvider extends AdminHtmlRouteProvider implements
} }
if ($uuid_route = $this->getUuidRoute($entity_type)) { if ($uuid_route = $this->getUuidRoute($entity_type)) {
$collection->add("islandora.{$entity_type_id}.uuid", $uuid_route); $collection->add("entity.{$entity_type_id}.uuid", $uuid_route);
} }
@ -62,12 +62,12 @@ class FedoraResourceHtmlRouteProvider extends AdminHtmlRouteProvider implements
$route = new Route($entity_type->getLinkTemplate('uuid')); $route = new Route($entity_type->getLinkTemplate('uuid'));
$route $route
->addDefaults([ ->addDefaults([
'_entity_view' => 'fedora_resource.full', '_entity_view' => "{$entity_type_id}.full",
'_title_callback' => '\Drupal\Core\Entity\Controller\EntityController::title', '_title_callback' => '\Drupal\Core\Entity\Controller\EntityController::title',
]) ])
->setRequirement('_entity_access', $entity_type_id . '.view') ->setRequirement('_entity_access', "{$entity_type_id}.view")
->setOption('parameters', [ ->setOption('parameters', [
$entity_type_id => ['type' => 'entity:' . $entity_type_id], $entity_type_id => ['type' => "uuid"],
]) ])
// Fetch UUID pattern from Uuid class(constant) // Fetch UUID pattern from Uuid class(constant)
->setRequirement($entity_type_id, '^' . Uuid::VALID_PATTERN . '$'); ->setRequirement($entity_type_id, '^' . Uuid::VALID_PATTERN . '$');

36
src/ParamConverter/UuidEntityConverter.php

@ -19,51 +19,29 @@ namespace Drupal\islandora\ParamConverter;
use Drupal\Core\ParamConverter\EntityConverter; use Drupal\Core\ParamConverter\EntityConverter;
use Drupal\Core\ParamConverter\ParamConverterInterface; use Drupal\Core\ParamConverter\ParamConverterInterface;
use Drupal\Component\Uuid\Uuid; use Drupal\Component\Uuid\Uuid;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Route;
/** /**
* Converts an UUID param (route) into an Entity. * Converts an UUID param (route) into an Entity.
* *
* @ingroup islandora * @ingroup islandora
*/ */
class UuidEntityConverter extends EntityConverter implements ParamConverterInterface { class UuidEntityConverter extends EntityConverter {
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function convert($value, $definition, $name, array $defaults) { public function convert($value, $definition, $name, array $defaults) {
return $this->entityManager->loadEntityByUuid($name, $value);
$entity_type_id = $this->getEntityTypeFromDefaults($definition, $name, $defaults);
if ($storage = $this->entityManager->getStorage($entity_type_id)) {
// Try to load fedora entity by UUID or ID depending on $value.
if (!(is_int($value) || ctype_digit((string) $value)) && Uuid::isValid($value)) {
$entities = $storage->loadByProperties(['uuid' => $value]);
$entity = ($entities) ? reset($entities) : NULL;
}
else {
$entity = parent::convert($value, $definition, $name, $defaults);
}
}
return $entity;
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function applies($definition, $name, Route $route) { public function applies($definition, $name, Route $route) {
/*if (!empty($definition['type']) && strpos($definition['type'], 'entity:') === 0) { return (!empty($definition['type']) && $definition['type'] == 'uuid');
$entity_type_id = substr($definition['type'], strlen('entity:'));
if (strpos($definition['type'], '{') !== FALSE) {
$entity_type_slug = substr($entity_type_id, 1, -1);
return $name != $entity_type_slug && in_array($entity_type_slug, $route->compile()->getVariables(), TRUE);
}
return $this->entityManager->hasDefinition($entity_type_id);
}
return FALSE;*/
return parent::applies($definition, $name, $route); // TODO: Change the autogenerated stub
} }
} }

Loading…
Cancel
Save