/** * {@inheritdoc} */ public function build(RouteMatchInterface $route_match) { $breadcrumb = new Breadcrumb(); $breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '<front>')); $term = $route_match->getParameter('taxonomy_term'); // Breadcrumb needs to have terms cacheable metadata as a cacheable // dependency even though it is not shown in the breadcrumb because e.g. its // parent might have changed. $breadcrumb->addCacheableDependency($term); // @todo This overrides any other possible breadcrumb and is a pure // hard-coded presumption. Make this behavior configurable per // vocabulary or term. $parents = $this->termStorage->loadAllParents($term->id()); // Remove current term being accessed. array_shift($parents); foreach (array_reverse($parents) as $term) { $term = $this->entityManager->getTranslationFromContext($term); $breadcrumb->addCacheableDependency($term); $breadcrumb->addLink(Link::createFromRoute($term->getName(), 'entity.taxonomy_term.canonical', array('taxonomy_term' => $term->id()))); } // This breadcrumb builder is based on a route parameter, and hence it // depends on the 'route' cache context. $breadcrumb->addCacheContexts(['route']); return $breadcrumb; }
/** * {@inheritdoc} */ public function getFormLangcode(array &$form_state) { if (empty($form_state['langcode'])) { // Imply a 'view' operation to ensure users edit entities in the same // language they are displayed. This allows to keep contextual editing // working also for multilingual entities. $form_state['langcode'] = $this->entityManager->getTranslationFromContext($this->entity)->language()->id; } return $form_state['langcode']; }
/** * {@inheritdoc} */ public function convert($value, $definition, $name, array $defaults) { $entity_type_id = $this->getEntityTypeFromDefaults($definition, $name, $defaults); if ($storage = $this->entityManager->getStorage($entity_type_id)) { $entity = $storage->loadRevision($value); // If the entity type is translatable, ensure we return the proper // translation object for the current context. if ($entity instanceof EntityInterface && $entity instanceof TranslatableInterface) { $entity = $this->entityManager->getTranslationFromContext($entity, NULL, array('operation' => 'entity_upcast')); } return $entity; } }
/** * {@inheritdoc} */ public function build(RouteMatchInterface $route_match) { $term = $route_match->getParameter('taxonomy_term'); // @todo This overrides any other possible breadcrumb and is a pure // hard-coded presumption. Make this behavior configurable per // vocabulary or term. $breadcrumb = array(); while ($parents = $this->termStorage->loadParents($term->id())) { $term = array_shift($parents); $term = $this->entityManager->getTranslationFromContext($term); $breadcrumb[] = Link::createFromRoute($term->getName(), 'entity.taxonomy_term.canonical', array('taxonomy_term' => $term->id())); } $breadcrumb[] = Link::createFromRoute($this->t('Home'), '<front>'); $breadcrumb = array_reverse($breadcrumb); return $breadcrumb; }
/** * Loads the entity associated with this menu link. * * @return \Drupal\menu_link_content\MenuLinkContentInterface * The menu link content entity. * * @throws \Drupal\Component\Plugin\Exception\PluginException * If the entity ID and UUID are both invalid or missing. */ protected function getEntity() { if (empty($this->entity)) { $entity = NULL; $storage = $this->entityManager->getStorage('menu_link_content'); if (!empty($this->pluginDefinition['metadata']['entity_id'])) { $entity_id = $this->pluginDefinition['metadata']['entity_id']; // Make sure the current ID is in the list, since each plugin empties // the list after calling loadMultiple(). Note that the list may include // multiple IDs added earlier in each plugin's constructor. static::$entityIdsToLoad[$entity_id] = $entity_id; $entities = $storage->loadMultiple(array_values(static::$entityIdsToLoad)); $entity = isset($entities[$entity_id]) ? $entities[$entity_id] : NULL; static::$entityIdsToLoad = array(); } if (!$entity) { // Fallback to the loading by the UUID. $uuid = $this->getUuid(); $loaded_entities = $storage->loadByProperties(array('uuid' => $uuid)); $entity = reset($loaded_entities); } if (!$entity) { throw new PluginException("Entity not found through the menu link plugin definition and could not fallback on UUID '{$uuid}'"); } // Clone the entity object to avoid tampering with the static cache. $this->entity = clone $entity; $the_entity = $this->entityManager->getTranslationFromContext($this->entity); /** @var \Drupal\menu_link_content\MenuLinkContentInterface $the_entity */ $this->entity = $the_entity; $this->entity->setInsidePlugin(); } return $this->entity; }
/** * {@inheritdoc} */ public function build(RouteMatchInterface $route_match) { $breadcrumb = new Breadcrumb(); $breadcrumb->addCacheContexts(['route']); // Add all parent forums to breadcrumbs. /** @var Node $node */ $node = $route_match->getParameter('node'); $breadcrumb->addCacheableDependency($node); // Add all parent forums to breadcrumbs. /** @var \Drupal\Taxonomy\TermInterface $term */ $term = !empty($node->field_channel) ? $node->field_channel->entity : ''; $links = []; if ($term) { $breadcrumb->addCacheableDependency($term); $channels = $this->termStorage->loadAllParents($term->id()); foreach (array_reverse($channels) as $term) { $term = $this->entityManager->getTranslationFromContext($term); $breadcrumb->addCacheableDependency($term); $links[] = Link::createFromRoute($term->getName(), 'entity.taxonomy_term.canonical', array('taxonomy_term' => $term->id())); } } if (!$links || '/' . $links[0]->getUrl()->getInternalPath() != $this->configFactory->get('system.site')->get('page.front')) { array_unshift($links, Link::createFromRoute($this->t('Home'), '<front>')); } return $breadcrumb->setLinks($links); }
/** * Returns the entity translation matching the configured row language. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity object the field value being processed is attached to. * @param \Drupal\views\ResultRow $row * The result row the field value being processed belongs to. * * @return \Drupal\Core\Entity\FieldableEntityInterface * The entity translation object for the specified row. */ public function getEntityTranslation(EntityInterface $entity, ResultRow $row) { // We assume the same language should be used for all entity fields // belonging to a single row, even if they are attached to different entity // types. Below we apply language fallback to ensure a valid value is always // picked. $langcode = $this->getEntityTranslationRenderer()->getLangcode($row); return $this->entityManager->getTranslationFromContext($entity, $langcode); }
/** * {@inheritdoc} */ public function getFormLangcode(FormStateInterface $form_state) { if (!$form_state->has('langcode')) { // Imply a 'view' operation to ensure users edit entities in the same // language they are displayed. This allows to keep contextual editing // working also for multilingual entities. $form_state->set('langcode', $this->entityManager->getTranslationFromContext($this->entity)->language()->getId()); } return $form_state->get('langcode'); }
/** * Provides a page to render a single entity. * * @param \Drupal\Core\Entity\EntityInterface $_entity * The Entity to be rendered. Note this variable is named $_entity rather * than $entity to prevent collisions with other named placeholders in the * route. * @param string $view_mode * (optional) The view mode that should be used to display the entity. * Defaults to 'full'. * @param string $langcode * (optional) For which language the entity should be rendered, defaults to * the current content language. * * @return array * A render array as expected by drupal_render(). */ public function view(EntityInterface $_entity, $view_mode = 'full', $langcode = NULL) { $page = $this->entityManager->getViewBuilder($_entity->getEntityTypeId())->view($_entity, $view_mode, $langcode); // If the entity's label is rendered using a field formatter, set the // rendered title field formatter as the page title instead of the default // plain text title. This allows attributes set on the field to propagate // correctly (e.g. RDFa, in-place editing). if ($_entity instanceof ContentEntityInterface) { $label_field = $_entity->getEntityType()->getKey('label'); if ($label_field && $_entity->getFieldDefinition($label_field)->getDisplayOptions('view')) { // We must render the label field, because rendering the entity may be // a cache hit, in which case we can't extract the rendered label field // from the $page renderable array. $build = $this->entityManager->getTranslationFromContext($_entity)->get($label_field)->view($view_mode); $page['#title'] = drupal_render($build, TRUE); } } return $page; }
/** * {@inheritdoc} */ public function viewMultiple(array $entities = array(), $view_mode = 'full', $langcode = NULL) { $build_list = array('#sorted' => TRUE, '#pre_render' => array(array($this, 'buildMultiple'))); $weight = 0; foreach ($entities as $key => $entity) { // Ensure that from now on we are dealing with the proper translation // object. $entity = $this->entityManager->getTranslationFromContext($entity, $langcode); // Set build defaults. $build_list[$key] = $this->getBuildDefaults($entity, $view_mode); $entityType = $this->entityTypeId; $this->moduleHandler()->alter(array($entityType . '_build_defaults', 'entity_build_defaults'), $build_list[$key], $entity, $view_mode); $build_list[$key]['#weight'] = $weight++; } return $build_list; }
/** * Initializes form language code values. * * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */ protected function initFormLangcodes(FormStateInterface $form_state) { // Store the entity default language to allow checking whether the form is // dealing with the original entity or a translation. if (!$form_state->has('entity_default_langcode')) { $form_state->set('entity_default_langcode', $this->entity->getUntranslated()->language()->getId()); } // This value might have been explicitly populated to work with a particular // entity translation. If not we fall back to the most proper language based // on contextual information. if (!$form_state->has('langcode')) { // Imply a 'view' operation to ensure users edit entities in the same // language they are displayed. This allows to keep contextual editing // working also for multilingual entities. $form_state->set('langcode', $this->entityManager->getTranslationFromContext($this->entity)->language()->getId()); } }
/** * 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 ($entity) { return $this->entityManager->getTranslationFromContext($entity); } }
/** * {@inheritdoc} */ public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) { $target_type = $this->configuration['target_type']; $query = $this->buildEntityQuery($match, $match_operator); if ($limit > 0) { $query->range(0, $limit); } $result = $query->execute(); if (empty($result)) { return array(); } $options = array(); $entities = $this->entityManager->getStorage($target_type)->loadMultiple($result); foreach ($entities as $entity_id => $entity) { $bundle = $entity->bundle(); $options[$bundle][$entity_id] = Html::escape($this->entityManager->getTranslationFromContext($entity)->label()); } return $options; }
/** * Return the code of the language the field should be displayed in. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity object the field value being processed is attached to. * @param \Drupal\views\ResultRow $row * The result row the field value being processed belongs to. * * @return string * The field language code. */ protected function getFieldLangcode(EntityInterface $entity, ResultRow $row) { if ($this->getFieldDefinition()->isTranslatable()) { // Even if the current field is not attached to the main entity, we use it // to determine the field language, as we assume the same language should // be used for all values belonging to a single row, when possible. Below // we apply language fallback to ensure a valid value is always picked. $langcode = $this->getEntityTranslationRenderer()->getLangcode($row); // Give the Entity Field API a chance to fallback to a different language // (or LanguageInterface::LANGCODE_NOT_SPECIFIED), in case the field has // no data for the selected language. FieldItemListInterface::view() does // this as well, but since the returned language code is used before // calling it, the fallback needs to happen explicitly. $langcode = $this->entityManager->getTranslationFromContext($entity, $langcode)->language()->getId(); return $langcode; } else { return LanguageInterface::LANGCODE_NOT_SPECIFIED; } }
/** * Loads Principles Array. */ protected function loadPrinciples() { $this->principles = array(); $nids = $this->principleStorage->getPrinciples(); if ($nids) { $principle_links = $this->principleStorage->loadMultiple($nids); $nodes = $this->entityManager->getStorage('node')->loadMultiple($nids); // @todo: use route name for links, not system path. foreach ($principle_links as $link) { $nid = $link['nid']; if (isset($nodes[$nid]) && $nodes[$nid]->isPublished()) { $entity = $nodes[$nid]; $item = array(); $item['uuid'] = $entity->uuid(); $item['title'] = $this->entityManager->getTranslationFromContext($entity)->label(); $item['type'] = $entity->bundle(); $item['url'] = $entity->url(); // $link['url_info'] = $nodes[$nid]->urlInfo(); $this->principles[$link['nid']] = (object) $item; } } } }
/** * Return the language code of the language the field should be displayed in, * according to the settings. */ function field_langcode(EntityInterface $entity) { if ($this->getFieldDefinition()->isTranslatable()) { $default_langcode = language_default()->id; $langcode = str_replace(array('***CURRENT_LANGUAGE***', '***DEFAULT_LANGUAGE***'), array($this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->id, $default_langcode), $this->view->display_handler->options['field_langcode']); // Give the Entity Field API a chance to fallback to a different language // (or LanguageInterface::LANGCODE_NOT_SPECIFIED), in case the field has // no data for the selected language. FieldItemListInterface::view() does // this as well, but since the returned language code is used before // calling it, the fallback needs to happen explicitly. $langcode = $this->entityManager->getTranslationFromContext($entity, $langcode)->language()->id; return $langcode; } else { return LanguageInterface::LANGCODE_NOT_SPECIFIED; } }
/** * Return the language code of the language the field should be displayed in, * according to the settings. */ function field_langcode(EntityInterface $entity) { if ($this->getFieldDefinition()->isTranslatable()) { $langcode = $this->view->display_handler->options['field_langcode']; $substitutions = static::queryLanguageSubstitutions(); if (isset($substitutions[$langcode])) { $langcode = $substitutions[$langcode]; } // Give the Entity Field API a chance to fallback to a different language // (or LanguageInterface::LANGCODE_NOT_SPECIFIED), in case the field has // no data for the selected language. FieldItemListInterface::view() does // this as well, but since the returned language code is used before // calling it, the fallback needs to happen explicitly. $langcode = $this->entityManager->getTranslationFromContext($entity, $langcode)->language()->getId(); return $langcode; } else { return LanguageInterface::LANGCODE_NOT_SPECIFIED; } }
/** * {@inheritdoc} */ public function getTranslationFromContext(EntityInterface $entity, $langcode = NULL, $context = []) { return $this->entityManager->getTranslationFromContext($entity, $langcode, $context); }