Commit ace96deb authored by Alex Bronstein's avatar Alex Bronstein
Browse files

Issue #2073217 by andypost, Berdir, plach: Remove the $langcode parameter from...

Issue #2073217 by andypost, Berdir, plach: Remove the $langcode parameter from the entity view/render system
parent bfb117c5
Loading
Loading
Loading
Loading
+2 −2
Changes for core/includes/entity.inc: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -360,7 +360,7 @@ function entity_page_label(EntityInterface $entity, $langcode = NULL) {
function entity_view(EntityInterface $entity, $view_mode, $langcode = NULL, $reset = FALSE) {
  $render_controller = \Drupal::entityManager()->getViewBuilder($entity->getEntityTypeId());
  if ($reset) {
    $render_controller->resetCache(array($entity->id()));
    $render_controller->resetCache([$entity]);
  }
  return $render_controller->view($entity, $view_mode, $langcode);
}
@@ -397,7 +397,7 @@ function entity_view(EntityInterface $entity, $view_mode, $langcode = NULL, $res
function entity_view_multiple(array $entities, $view_mode, $langcode = NULL, $reset = FALSE) {
  $render_controller = \Drupal::entityManager()->getViewBuilder(reset($entities)->getEntityTypeId());
  if ($reset) {
    $render_controller->resetCache(array_keys($entities));
    $render_controller->resetCache($entities);
  }
  return $render_controller->viewMultiple($entities, $view_mode, $langcode);
}
+2 −5
Changes for core/lib/Drupal/Core/Entity/Controller/EntityViewController.php: 2 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -91,17 +91,14 @@ public function buildTitle(array $page) {
   * @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) {
  public function view(EntityInterface $_entity, $view_mode = 'full') {
    $page = $this->entityManager
      ->getViewBuilder($_entity->getEntityTypeId())
      ->view($_entity, $view_mode, $langcode);
      ->view($_entity, $view_mode);

    $page['#pre_render'][] = [$this, 'buildTitle'];
    $page['#entity_type'] = $_entity->getEntityTypeId();
+13 −27
Changes for core/lib/Drupal/Core/Entity/EntityViewBuilder.php: 13 added lines, 27 removed lines.
Original line number Diff line number Diff line
@@ -12,10 +12,9 @@
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\TypedData\TranslatableInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\TypedData\TranslatableInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
@@ -120,7 +119,6 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la
    $build_list = array(
      '#sorted' => TRUE,
      '#pre_render' => array(array($this, 'buildMultiple')),
      '#langcode' => $langcode ?: $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId(),
    );
    $weight = 0;
    foreach ($entities as $key => $entity) {
@@ -129,10 +127,9 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la
      $entity = $this->entityManager->getTranslationFromContext($entity, $langcode);

      // Set build defaults.
      $entity_langcode = $entity->language()->getId();
      $build_list[$key] = $this->getBuildDefaults($entity, $view_mode, $entity_langcode);
      $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, $entity_langcode);
      $this->moduleHandler()->alter(array($entityType . '_build_defaults', 'entity_build_defaults'), $build_list[$key], $entity, $view_mode);

      $build_list[$key]['#weight'] = $weight++;
    }
@@ -147,22 +144,18 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la
   *   The entity for which the defaults should be provided.
   * @param string $view_mode
   *   The view mode that should be used.
   * @param string $langcode
   *   For which language the entity should be prepared, defaults to
   *   the current content language.
   *
   * @return array
   */
  protected function getBuildDefaults(EntityInterface $entity, $view_mode, $langcode) {
  protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
    // Allow modules to change the view mode.
    $context = array('langcode' => $langcode);
    $context = [];
    $this->moduleHandler()->alter('entity_view_mode', $view_mode, $entity, $context);

    $build = array(
      '#theme' => $this->entityTypeId,
      "#{$this->entityTypeId}" => $entity,
      '#view_mode' => $view_mode,
      '#langcode' => $langcode,
      // Collect cache defaults for this entity.
      '#cache' => array(
        'tags' => Cache::mergeTags($this->getCacheTags(), $entity->getCacheTags()),
@@ -185,7 +178,7 @@ protected function getBuildDefaults(EntityInterface $entity, $view_mode, $langco
      );

      if ($entity instanceof TranslatableInterface && count($entity->getTranslationLanguages()) > 1) {
        $build['#cache']['keys'][] = $langcode;
        $build['#cache']['keys'][] = $entity->language()->getId();
      }
    }

@@ -211,10 +204,7 @@ protected function getBuildDefaults(EntityInterface $entity, $view_mode, $langco
   * @see drupal_render()
   */
  public function build(array $build) {
    $build_list = array(
      '#langcode' => $build['#langcode'],
    );
    $build_list[] = $build;
    $build_list = [$build];
    $build_list = $this->buildMultiple($build_list);
    return $build_list[0];
  }
@@ -240,7 +230,6 @@ public function build(array $build) {
  public function buildMultiple(array $build_list) {
    // Build the view modes and display objects.
    $view_modes = array();
    $langcode = $build_list['#langcode'];
    $entity_type_key = "#{$this->entityTypeId}";
    $view_hook = "{$this->entityTypeId}_view";

@@ -259,16 +248,16 @@ public function buildMultiple(array $build_list) {
    // Build content for the displays represented by the entities.
    foreach ($view_modes as $view_mode => $view_mode_entities) {
      $displays = EntityViewDisplay::collectRenderDisplays($view_mode_entities, $view_mode);
      $this->buildComponents($build_list, $view_mode_entities, $displays, $view_mode, $langcode);
      $this->buildComponents($build_list, $view_mode_entities, $displays, $view_mode);
      foreach (array_keys($view_mode_entities) as $key) {
        // Allow for alterations while building, before rendering.
        $entity = $build_list[$key][$entity_type_key];
        $display = $displays[$entity->bundle()];

        $this->moduleHandler()->invokeAll($view_hook, array(&$build_list[$key], $entity, $display, $view_mode, $langcode));
        $this->moduleHandler()->invokeAll('entity_view', array(&$build_list[$key], $entity, $display, $view_mode, $langcode));
        $this->moduleHandler()->invokeAll($view_hook, [&$build_list[$key], $entity, $display, $view_mode]);
        $this->moduleHandler()->invokeAll('entity_view', [&$build_list[$key], $entity, $display, $view_mode]);

        $this->alterBuild($build_list[$key], $entity, $display, $view_mode, $langcode);
        $this->alterBuild($build_list[$key], $entity, $display, $view_mode);

        // Assign the weights configured in the display.
        // @todo: Once https://www.drupal.org/node/1875974 provides the missing
@@ -291,7 +280,7 @@ public function buildMultiple(array $build_list) {
  /**
   * {@inheritdoc}
   */
  public function buildComponents(array &$build, array $entities, array $displays, $view_mode, $langcode = NULL) {
  public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
    $entities_by_bundle = array();
    foreach ($entities as $id => $entity) {
      // Initialize the field item attributes for the fields being displayed.
@@ -335,11 +324,8 @@ public function buildComponents(array &$build, array $entities, array $displays,
   *   entity components.
   * @param string $view_mode
   *   The view mode that should be used to prepare the entity.
   * @param string $langcode
   *   (optional) For which language the entity should be prepared, defaults to
   *   the current content language.
   */
  protected function alterBuild(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode, $langcode = NULL) { }
  protected function alterBuild(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) { }

  /**
   * {@inheritdoc}
+1 −4
Changes for core/lib/Drupal/Core/Entity/EntityViewBuilderInterface.php: 1 added line, 4 removed lines.
Original line number Diff line number Diff line
@@ -29,11 +29,8 @@ interface EntityViewBuilderInterface {
   *   configured for the entity components, keyed by bundle name.
   * @param string $view_mode
   *   The view mode in which the entity is being viewed.
   * @param string $langcode
   *   (optional) For which language the entity should be build, defaults to
   *   the current content language.
   */
  public function buildComponents(array &$build, array $entities, array $displays, $view_mode, $langcode = NULL);
  public function buildComponents(array &$build, array $entities, array $displays, $view_mode);

  /**
   * Builds the render array for the provided entity.
+8 −13
Changes for core/lib/Drupal/Core/Entity/entity.api.php: 8 added lines, 13 removed lines.
Original line number Diff line number Diff line
@@ -468,7 +468,9 @@
 * @endcode
 * Then, to build and render the entity:
 * @code
 * // You can omit the language ID if the default language is being used.
 * // You can omit the language ID, by default the current content language will
 * // be used. If no translation is available for the current language, fallback
 * // rules will be used.
 * $build = $view_builder->view($entity, 'view_mode_name', $language->getId());
 * // $build is a render array.
 * $rendered = drupal_render($build);
@@ -502,6 +504,7 @@
 *
 * @see i18n
 * @see entity_crud
 * @see \Drupal\Core\Entity\EntityManagerInterface::getTranslationFromContext()
 * @}
 */

@@ -1232,8 +1235,6 @@ function hook_entity_query_alter(\Drupal\Core\Entity\Query\QueryInterface $query
 *   entity components.
 * @param $view_mode
 *   The view mode the entity is rendered in.
 * @param $langcode
 *   The language code used for rendering.
 *
 * The module may add elements to $build prior to rendering. The
 * structure of $build is a renderable array as expected by
@@ -1244,7 +1245,7 @@ function hook_entity_query_alter(\Drupal\Core\Entity\Query\QueryInterface $query
 *
 * @ingroup entity_crud
 */
function hook_entity_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode, $langcode) {
function hook_entity_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
  // Only do the extra work if the component is configured to be displayed.
  // This assumes a 'mymodule_addition' extra field has been defined for the
  // entity bundle in hook_entity_extra_field_info().
@@ -1268,8 +1269,6 @@ function hook_entity_view(array &$build, \Drupal\Core\Entity\EntityInterface $en
 *   entity components.
 * @param $view_mode
 *   The view mode the entity is rendered in.
 * @param $langcode
 *   The language code used for rendering.
 *
 * The module may add elements to $build prior to rendering. The
 * structure of $build is a renderable array as expected by
@@ -1280,7 +1279,7 @@ function hook_entity_view(array &$build, \Drupal\Core\Entity\EntityInterface $en
 *
 * @ingroup entity_crud
 */
function hook_ENTITY_TYPE_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode, $langcode) {
function hook_ENTITY_TYPE_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
  // Only do the extra work if the component is configured to be displayed.
  // This assumes a 'mymodule_addition' extra field has been defined for the
  // entity bundle in hook_entity_extra_field_info().
@@ -1443,8 +1442,6 @@ function hook_entity_view_mode_alter(&$view_mode, Drupal\Core\Entity\EntityInter
 *   The entity that is being viewed.
 * @param string $view_mode
 *   The view_mode that is to be used to display the entity.
 * @param string $langcode
 *   The code of the language $entity is accessed in.
 *
 * @see drupal_render()
 * @see \Drupal\Core\Entity\EntityViewBuilder
@@ -1452,7 +1449,7 @@ function hook_entity_view_mode_alter(&$view_mode, Drupal\Core\Entity\EntityInter
 *
 * @ingroup entity_crud
 */
function hook_ENTITY_TYPE_build_defaults_alter(array &$build, \Drupal\Core\Entity\EntityInterface $entity, $view_mode, $langcode) {
function hook_ENTITY_TYPE_build_defaults_alter(array &$build, \Drupal\Core\Entity\EntityInterface $entity, $view_mode) {

}

@@ -1469,8 +1466,6 @@ function hook_ENTITY_TYPE_build_defaults_alter(array &$build, \Drupal\Core\Entit
 *   The entity that is being viewed.
 * @param string $view_mode
 *   The view_mode that is to be used to display the entity.
 * @param string $langcode
 *   The code of the language $entity is accessed in.
 *
 * @see drupal_render()
 * @see \Drupal\Core\Entity\EntityViewBuilder
@@ -1478,7 +1473,7 @@ function hook_ENTITY_TYPE_build_defaults_alter(array &$build, \Drupal\Core\Entit
 *
 * @ingroup entity_crud
 */
function hook_entity_build_defaults_alter(array &$build, \Drupal\Core\Entity\EntityInterface $entity, $view_mode, $langcode) {
function hook_entity_build_defaults_alter(array &$build, \Drupal\Core\Entity\EntityInterface $entity, $view_mode) {

}

Loading