/** * Determines the entity. * * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The route match. * @param \Drupal\Core\Entity\EntityInterface $_entity * (optional) The entity, set in * \Drupal\Core\Entity\Enhancer\EntityRouteEnhancer. * * @return \Drupal\Core\Entity\EntityInterface|null * The entity, if it is passed in directly or if the first parameter of the * active route is an entity; otherwise, NULL. */ protected function doGetEntity(RouteMatchInterface $route_match, EntityInterface $_entity = NULL) { if ($_entity) { $entity = $_entity; } else { // Let's look up in the route object for the name of upcasted values. foreach ($route_match->getParameters() as $parameter) { if ($parameter instanceof EntityInterface) { $entity = $parameter; break; } } } if (isset($entity)) { return $this->entityRepository->getTranslationFromContext($entity); } }
/** * {@inheritdoc} */ protected function init() { // Collect the args from the path manually. We can't pass them in from the // controller base because that class has to work for a variable number // of args. $attribs = $this->request->attributes->get('_raw_variables'); // Set data sources as properties. $this->entityType = $attribs->get('entityType'); $this->entityId = $attribs->get('entityId'); $this->fieldName = $attribs->get('fieldName'); $this->displayName = $attribs->get('displayName'); // Grab the loaded entity as well, translated appropriately. $entity_base = $this->entityTypeManager->getStorage($this->entityType)->load($this->entityId); $this->entity = $this->entityRepository->getTranslationFromContext($entity_base); if (is_object($this->entity) && $this->entity instanceof EntityInterface) { // All looks good. return; } throw new \Exception(t('Cannot instantiate field-based Juicebox gallery as no entity can be loaded.')); }
/** * {@inheritdoc} */ public function convert($value, $definition, $name, array $defaults) { list(, $entity_type_id) = explode(':', $definition['type'], 2); $entity = $this->entityTypeManager->getStorage($entity_type_id)->loadRevision($value); return $this->entityRepository->getTranslationFromContext($entity); }