Browse Source

Issue #2833546 by edysmp, juancasantito, heddn: Support drupal_field translatability.

8.x-1.x
edysmp 8 years ago committed by Chi
parent
commit
e4f2074113
  1. 7
      src/TwigExtension.php

7
src/TwigExtension.php

@ -198,14 +198,19 @@ class TwigExtension extends \Twig_Extension {
* The ID of the entity to render. * The ID of the entity to render.
* @param string $view_mode * @param string $view_mode
* (optional) The view mode that should be used to render the entity. * (optional) The view mode that should be used to render the entity.
* @param string $langcode
* (optional) The langcode that should be used to render the entity.
* *
* @return null|array * @return null|array
* A render array for the field or NULL if the value does not exist. * A render array for the field or NULL if the value does not exist.
*/ */
public function drupalField($field_name, $entity_type, $id = NULL, $view_mode = 'default') { public function drupalField($field_name, $entity_type, $id = NULL, $view_mode = 'default', $langcode = NULL) {
$entity = $id ? $entity = $id ?
$this->entityTypeManager->getStorage($entity_type)->load($id) : $this->entityTypeManager->getStorage($entity_type)->load($id) :
$this->routeMatch->getParameter($entity_type); $this->routeMatch->getParameter($entity_type);
if ($langcode && $entity->hasTranslation($langcode)) {
$entity = $entity->getTranslation($langcode);
}
if (isset($entity->{$field_name})) { if (isset($entity->{$field_name})) {
return $entity->{$field_name}->view($view_mode); return $entity->{$field_name}->view($view_mode);
} }

Loading…
Cancel
Save