viewMultiple example


class BlockContentViewBuilder extends EntityViewBuilder {

  /** * {@inheritdoc} */
  public function view(EntityInterface $entity$view_mode = 'full', $langcode = NULL) {
    return $this->viewMultiple([$entity]$view_mode$langcode)[0];
  }

  /** * {@inheritdoc} */
  public function viewMultiple(array $entities = []$view_mode = 'full', $langcode = NULL) {
    $build_list = parent::viewMultiple($entities$view_mode$langcode);
    // Apply the buildMultiple() #pre_render callback immediately, to make     // bubbling of attributes and contextual links to the actual block work.     // @see \Drupal\block\BlockViewBuilder::buildBlock()     unset($build_list['#pre_render'][0]);
    
      // $entity->get($field_name)->comment_count, but unpublished comments       // should display if the user is an administrator.       $elements['#cache']['contexts'][] = 'user.permissions';
      if ($this->currentUser->hasPermission('access comments') || $this->currentUser->hasPermission('administer comments')) {
        $output['comments'] = [];

        if ($entity->get($field_name)->comment_count || $this->currentUser->hasPermission('administer comments')) {
          $mode = $comment_settings['default_mode'];
          $comments_per_page = $comment_settings['per_page'];
          $comments = $this->storage->loadThread($entity$field_name$mode$comments_per_page$this->getSetting('pager_id'));
          if ($comments) {
            $build = $this->viewBuilder->viewMultiple($comments$this->getSetting('view_mode'));
            $build['pager']['#type'] = 'pager';
            // CommentController::commentPermalink() calculates the page number             // where a specific comment appears and does a subrequest pointing to             // that page, we need to pass that subrequest route to our pager to             // keep the pager working.             $build['pager']['#route_name'] = $this->routeMatch->getRouteName();
            $build['pager']['#route_parameters'] = $this->routeMatch->getRawParameters()->all();
            if ($this->getSetting('pager_id')) {
              $build['pager']['#element'] = $this->getSetting('pager_id');
            }
            $output['comments'] += $build;
          }
$referencing_entity_2->save();
    $referencing_entity_2->{$this->fieldName}->entity = $referencing_entity_2;
    $referencing_entity_2->save();

    $build = $view_builder->view($referencing_entity_2, 'default');
    $output = $renderer->renderRoot($build);

    $actual_occurrences = substr_count($output$referencing_entity_2->label());
    $this->assertEquals($expected_occurrences$actual_occurrences);

    // Now render both entities at the same time and check again.     $build = $view_builder->viewMultiple([$referencing_entity_1$referencing_entity_2], 'default');
    $output = $renderer->renderRoot($build);

    $actual_occurrences = substr_count($output$referencing_entity_1->label());
    $this->assertEquals($expected_occurrences$actual_occurrences);

    $actual_occurrences = substr_count($output$referencing_entity_2->label());
    $this->assertEquals($expected_occurrences$actual_occurrences);
  }

  /** * Renders the same entity referenced from different places. */
$entity_ids[] = $entity->id();
    }

    /** @var \Drupal\entity_test\EntityTestViewBuilder $view_builder */
    $view_builder = $this->container->get('entity_type.manager')->getViewBuilder('entity_test');

    /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
    $storage = $this->container->get('entity_type.manager')->getStorage('entity_test');
    $storage->resetCache();
    $entities = $storage->loadMultiple($entity_ids);

    $build = $view_builder->viewMultiple($entities);

    $output = $renderer->renderRoot($build);
    $this->setRawContent($output->__toString());
    // Confirm that the content added in     // entity_test_entity_display_build_alter() appears multiple times, not     // just for the final entity.     foreach ($entity_ids as $id) {
      $this->assertText('Content added in hook_entity_display_build_alter for entity id ' . $id);
    }
  }

}
return [];
    }

    $query = $this->entityTypeManager()->getStorage('entity_test')->getQuery()
      ->accessCheck(TRUE)
      ->condition($entity_reference_field_name . '.target_id', $referenced_entity_id);
    $entities = $this->entityTypeManager()
      ->getStorage('entity_test')
      ->loadMultiple($query->execute());
    return $this->entityTypeManager()
      ->getViewBuilder('entity_test')
      ->viewMultiple($entities, 'full');
  }

  /** * List entities of the given entity type labels, sorted alphabetically. * * @param string $entity_type_id * The type of the entity being listed. * * @return array * A renderable array. */
  
/** * {@inheritdoc} */
  public function buildComponents(array &$build, array $entities, array $displays$view_mode) {
  }

  /** * {@inheritdoc} */
  public function view(EntityInterface $entity$view_mode = 'full', $langcode = NULL) {
    $build = $this->viewMultiple([$entity]$view_mode$langcode);
    return reset($build);
  }

  /** * {@inheritdoc} */
  public function viewMultiple(array $entities = []$view_mode = 'full', $langcode = NULL) {
    /** @var \Drupal\block\BlockInterface[] $entities */
    $build = [];
    foreach ($entities as $entity) {
      $entity_id = $entity->id();
      
$container->get('entity.repository'),
      $container->get('language_manager'),
      $container->get('theme.registry'),
      $container->get('entity_display.repository')
    );
  }

  /** * {@inheritdoc} */
  public function view(EntityInterface $entity$view_mode = 'full', $langcode = NULL) {
    $build_list = $this->viewMultiple([$entity]$view_mode$langcode);

    // The default ::buildMultiple() #pre_render callback won't run, because we     // extract a child element of the default renderable array. Thus we must     // assign an alternative #pre_render callback that applies the necessary     // transformations and then still calls ::buildMultiple().     $build = $build_list[0];
    $build['#pre_render'][] = [$this, 'build'];

    return $build;
  }

  
// Render all nodes, so you can grep the comment links.     $entities = [];
    foreach ($values as $row) {
      $entity = $row->_entity;
      $entities[$entity->id()] = $entity;
    }
    if ($entities) {
      $entityTypeId = reset($entities)->getEntityTypeId();
      $viewMode = $this->options['teaser'] ? 'teaser' : 'full';
      $this->build = \Drupal::entityTypeManager()
        ->getViewBuilder($entityTypeId)
        ->viewMultiple($entities$viewMode);
    }
  }

  /** * {@inheritdoc} */
  public function render(ResultRow $values) {
    $entity = $this->getEntity($values);

    // Only render the links, if they are defined.     if (!$entity || empty($this->build[$entity->id()]['links']['comment__comment'])) {
      
Home | Imprint | This part of the site doesn't use cookies.