selectViewMode example

->save();
    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');
    if (!$this->entity->getEntityType()->hasHandlerClass('view_builder')) {
      $display_repository->getViewDisplay($entity_type$bundle, 'full')
        ->setComponent($field_name[
          'type' => 'entity_reference_label',
        ])
        ->save();
    }
    else {
      $referenced_entity_view_mode = $this->selectViewMode($this->entity->getEntityTypeId());
      $display_repository->getViewDisplay($entity_type$bundle, 'full')
        ->setComponent($field_name[
          'type' => 'entity_reference_entity_view',
          'settings' => [
            'view_mode' => $referenced_entity_view_mode,
          ],
        ])
        ->save();
    }

    // Create an entity that does reference the entity being tested.

  public function testEntityUri() {
    $entity_url = $this->entity->toUrl();
    $entity_type = $this->entity->getEntityTypeId();

    // Selects the view mode that will be used.     $view_mode = $this->selectViewMode($entity_type);

    // The default cache contexts for rendered entities.     $entity_cache_contexts = $this->getDefaultCacheContexts();

    // Generate the standardized entity cache tags.     $cache_tag = $this->entity->getCacheTags();
    $view_cache_tag = \Drupal::entityTypeManager()->getViewBuilder($entity_type)->getCacheTags();
    $render_cache_tag = 'rendered';

    $this->verifyPageCache($entity_url, 'MISS');

    
Home | Imprint | This part of the site doesn't use cookies.