annotate core/lib/Drupal/Core/ParamConverter/EntityConverter.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 4c8ae668cc8c
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\ParamConverter;
Chris@0 4
Chris@0 5 use Drupal\Core\Entity\EntityInterface;
Chris@0 6 use Drupal\Core\Entity\EntityManagerInterface;
Chris@0 7 use Drupal\Core\TypedData\TranslatableInterface;
Chris@0 8 use Symfony\Component\Routing\Route;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Parameter converter for upcasting entity IDs to full objects.
Chris@0 12 *
Chris@0 13 * This is useful in cases where the dynamic elements of the path can't be
Chris@0 14 * auto-determined; for example, if your path refers to multiple of the same
Chris@0 15 * type of entity ("example/{node1}/foo/{node2}") or if the path can act on any
Chris@0 16 * entity type ("example/{entity_type}/{entity}/foo").
Chris@0 17 *
Chris@0 18 * In order to use it you should specify some additional options in your route:
Chris@0 19 * @code
Chris@0 20 * example.route:
Chris@0 21 * path: foo/{example}
Chris@0 22 * options:
Chris@0 23 * parameters:
Chris@0 24 * example:
Chris@0 25 * type: entity:node
Chris@0 26 * @endcode
Chris@0 27 *
Chris@0 28 * If you want to have the entity type itself dynamic in the url you can
Chris@0 29 * specify it like the following:
Chris@0 30 * @code
Chris@0 31 * example.route:
Chris@0 32 * path: foo/{entity_type}/{example}
Chris@0 33 * options:
Chris@0 34 * parameters:
Chris@0 35 * example:
Chris@0 36 * type: entity:{entity_type}
Chris@0 37 * @endcode
Chris@0 38 */
Chris@0 39 class EntityConverter implements ParamConverterInterface {
Chris@0 40
Chris@0 41 /**
Chris@0 42 * Entity manager which performs the upcasting in the end.
Chris@0 43 *
Chris@0 44 * @var \Drupal\Core\Entity\EntityManagerInterface
Chris@0 45 */
Chris@0 46 protected $entityManager;
Chris@0 47
Chris@0 48 /**
Chris@0 49 * Constructs a new EntityConverter.
Chris@0 50 *
Chris@0 51 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
Chris@0 52 * The entity manager.
Chris@0 53 */
Chris@0 54 public function __construct(EntityManagerInterface $entity_manager) {
Chris@0 55 $this->entityManager = $entity_manager;
Chris@0 56 }
Chris@0 57
Chris@0 58 /**
Chris@0 59 * {@inheritdoc}
Chris@0 60 */
Chris@0 61 public function convert($value, $definition, $name, array $defaults) {
Chris@0 62 $entity_type_id = $this->getEntityTypeFromDefaults($definition, $name, $defaults);
Chris@0 63 if ($storage = $this->entityManager->getStorage($entity_type_id)) {
Chris@0 64 $entity = $storage->load($value);
Chris@0 65 // If the entity type is translatable, ensure we return the proper
Chris@0 66 // translation object for the current context.
Chris@0 67 if ($entity instanceof EntityInterface && $entity instanceof TranslatableInterface) {
Chris@0 68 $entity = $this->entityManager->getTranslationFromContext($entity, NULL, ['operation' => 'entity_upcast']);
Chris@0 69 }
Chris@0 70 return $entity;
Chris@0 71 }
Chris@0 72 }
Chris@0 73
Chris@0 74 /**
Chris@0 75 * {@inheritdoc}
Chris@0 76 */
Chris@0 77 public function applies($definition, $name, Route $route) {
Chris@0 78 if (!empty($definition['type']) && strpos($definition['type'], 'entity:') === 0) {
Chris@0 79 $entity_type_id = substr($definition['type'], strlen('entity:'));
Chris@0 80 if (strpos($definition['type'], '{') !== FALSE) {
Chris@0 81 $entity_type_slug = substr($entity_type_id, 1, -1);
Chris@0 82 return $name != $entity_type_slug && in_array($entity_type_slug, $route->compile()->getVariables(), TRUE);
Chris@0 83 }
Chris@0 84 return $this->entityManager->hasDefinition($entity_type_id);
Chris@0 85 }
Chris@0 86 return FALSE;
Chris@0 87 }
Chris@0 88
Chris@0 89 /**
Chris@0 90 * Determines the entity type ID given a route definition and route defaults.
Chris@0 91 *
Chris@0 92 * @param mixed $definition
Chris@0 93 * The parameter definition provided in the route options.
Chris@0 94 * @param string $name
Chris@0 95 * The name of the parameter.
Chris@0 96 * @param array $defaults
Chris@0 97 * The route defaults array.
Chris@0 98 *
Chris@0 99 * @return string
Chris@0 100 * The entity type ID.
Chris@0 101 *
Chris@0 102 * @throws \Drupal\Core\ParamConverter\ParamNotConvertedException
Chris@0 103 * Thrown when the dynamic entity type is not found in the route defaults.
Chris@0 104 */
Chris@0 105 protected function getEntityTypeFromDefaults($definition, $name, array $defaults) {
Chris@0 106 $entity_type_id = substr($definition['type'], strlen('entity:'));
Chris@0 107
Chris@0 108 // If the entity type is dynamic, it will be pulled from the route defaults.
Chris@0 109 if (strpos($entity_type_id, '{') === 0) {
Chris@0 110 $entity_type_slug = substr($entity_type_id, 1, -1);
Chris@0 111 if (!isset($defaults[$entity_type_slug])) {
Chris@0 112 throw new ParamNotConvertedException(sprintf('The "%s" parameter was not converted because the "%s" parameter is missing', $name, $entity_type_slug));
Chris@0 113 }
Chris@0 114 $entity_type_id = $defaults[$entity_type_slug];
Chris@0 115 }
Chris@0 116 return $entity_type_id;
Chris@0 117 }
Chris@0 118
Chris@0 119 }