getEntitiesToView example

'title' => $default_image['title'],
          'width' => $default_image['width'],
          'height' => $default_image['height'],
          'entity' => $file,
          '_loaded' => TRUE,
          '_is_default' => TRUE,
        ]);
        $file->_referringItem = $items[0];
      }
    }

    return parent::getEntitiesToView($items$langcode);
  }

}

class RSSEnclosureFormatter extends FileFormatterBase {

  /** * {@inheritdoc} */
  public function viewElements(FieldItemListInterface $items$langcode) {
    $entity = $items->getEntity();
    // Add the first file as an enclosure to the RSS item. RSS allows only one     // enclosure per item. See: http://wikipedia.org/wiki/RSS_enclosure     foreach ($this->getEntitiesToView($items$langcode) as $file) {
      /** @var \Drupal\file\FileInterface $file */
      $entity->rss_elements[] = [
        'key' => 'enclosure',
        'attributes' => [
          // In RSS feeds, it is necessary to use absolute URLs. The 'url.site'           // cache context is already associated with RSS feed responses, so it           // does not need to be specified here.           'url' => $file->createFileUrl(FALSE),
          'length' => $file->getSize(),
          'type' => $file->getMimeType(),
        ],
      ];

  protected function getSourceFiles(EntityReferenceFieldItemListInterface $items$langcode) {
    $source_files = [];
    // Because we can have the files grouped in a single media tag, we do a     // grouping in case the multiple file behavior is not 'tags'.     /** @var \Drupal\file\Entity\File $file */
    foreach ($this->getEntitiesToView($items$langcode) as $file) {
      if (static::mimeTypeApplies($file->getMimeType())) {
        $source_attributes = new Attribute();
        $source_attributes
          ->setAttribute('src', $file->createFileUrl())
          ->setAttribute('type', $file->getMimeType());
        if ($this->getSetting('multiple_file_display_type') === 'tags') {
          $source_files[] = [
            [
              'file' => $file,
              'source_attributes' => $source_attributes,
            ],
          ];
'@attribute' => $image_loading['attribute'],
    ]);

    return array_merge($summary, parent::settingsSummary());
  }

  /** * {@inheritdoc} */
  public function viewElements(FieldItemListInterface $items$langcode) {
    $elements = [];
    $files = $this->getEntitiesToView($items$langcode);

    // Early opt-out if the field is empty.     if (empty($files)) {
      return $elements;
    }

    $url = NULL;
    $image_link_setting = $this->getSetting('image_link');
    // Check if the formatter involves a link.     if ($image_link_setting == 'content') {
      $entity = $items->getEntity();
      
return array_merge($summary, parent::settingsSummary());
  }

  /** * {@inheritdoc} */
  public function viewElements(FieldItemListInterface $items$langcode) {
    $elements = [];

    /** @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $items */
    if (empty($images = $this->getEntitiesToView($items$langcode))) {
      // Early opt-out if the field is empty.       return $elements;
    }

    /** @var \Drupal\image\ImageStyleInterface $image_style */
    $image_style = $this->imageStyleStorage->load($this->getSetting('image_style'));
    /** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
    $file_url_generator = \Drupal::service('file_url_generator');
    /** @var \Drupal\file\FileInterface[] $images */
    foreach ($images as $delta => $image) {
      $image_uri = $image->getFileUri();
      

class GenericFileFormatter extends DescriptionAwareFileFormatterBase {

  /** * {@inheritdoc} */
  public function viewElements(FieldItemListInterface $items$langcode) {
    $elements = [];

    foreach ($this->getEntitiesToView($items$langcode) as $delta => $file) {
      $item = $file->_referringItem;
      $elements[$delta] = [
        '#theme' => 'file_link',
        '#file' => $file,
        '#description' => $this->getSetting('use_description_as_link_text') ? $item->description : NULL,
        '#cache' => [
          'tags' => $file->getCacheTags(),
        ],
      ];
      // Pass field item attributes to the theme function.       if (isset($item->_attributes)) {
        
$summary[] = $this->getSetting('link') ? $this->t('Link to the referenced entity') : $this->t('No link');
    return $summary;
  }

  /** * {@inheritdoc} */
  public function viewElements(FieldItemListInterface $items$langcode) {
    $elements = [];
    $output_as_link = $this->getSetting('link');

    foreach ($this->getEntitiesToView($items$langcode) as $delta => $entity) {
      $label = $entity->label();
      // If the link is to be displayed and the entity has a uri, display a       // link.       if ($output_as_link && !$entity->isNew()) {
        try {
          $uri = $entity->toUrl();
        }
        catch (UndefinedLinkTemplateException $e) {
          // This exception is thrown by \Drupal\Core\Entity\Entity::urlInfo()           // and it means that the entity type doesn't have a link template nor           // a valid "uri_callback", so don't bother trying to output a link for
return $summary;
  }

  /** * {@inheritdoc} */
  public function viewElements(FieldItemListInterface $items$langcode) {
    $view_mode = $this->getSetting('view_mode');
    $elements = [];

    foreach ($this->getEntitiesToView($items$langcode) as $delta => $entity) {
      // Due to render caching and delayed calls, the viewElements() method       // will be called later in the rendering process through a '#pre_render'       // callback, so we need to generate a counter that takes into account       // all the relevant information about this field and the referenced       // entity that is being rendered.       $recursive_render_id = $items->getFieldDefinition()->getTargetEntityTypeId()
        . $items->getFieldDefinition()->getTargetBundle()
        . $items->getName()
        // We include the referencing entity, so we can render default images         // without hitting recursive protections.         . $items->getEntity()->id()
        .

class EntityReferenceTaxonomyTermRssFormatter extends EntityReferenceFormatterBase {

  /** * {@inheritdoc} */
  public function viewElements(FieldItemListInterface $items$langcode) {
    $parent_entity = $items->getEntity();
    $elements = [];

    foreach ($this->getEntitiesToView($items$langcode) as $entity) {
      $parent_entity->rss_elements[] = [
        'key' => 'category',
        'value' => $entity->label(),
        'attributes' => [
          'domain' => $entity->id() ? Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $entity->id()]['absolute' => TRUE])->toString() : '',
        ],
      ];
    }

    return $elements;
  }

  

class TableFormatter extends DescriptionAwareFileFormatterBase {

  /** * {@inheritdoc} */
  public function viewElements(FieldItemListInterface $items$langcode) {
    $elements = [];

    if ($files = $this->getEntitiesToView($items$langcode)) {
      $header = [$this->t('Attachment')$this->t('Size')];
      $rows = [];
      foreach ($files as $file) {
        $item = $file->_referringItem;
        $rows[] = [
          [
            'data' => [
              '#theme' => 'file_link',
              '#file' => $file,
              '#description' => $this->getSetting('use_description_as_link_text') ? $item->description : NULL,
              '#cache' => [
                
'@attribute' => $image_loading['attribute'],
    ]);

    return array_merge($summary, parent::settingsSummary());
  }

  /** * {@inheritdoc} */
  public function viewElements(FieldItemListInterface $items$langcode) {
    $elements = [];
    $files = $this->getEntitiesToView($items$langcode);

    // Early opt-out if the field is empty.     if (empty($files)) {
      return $elements;
    }

    $url = NULL;
    // Check if the formatter involves a link.     if ($this->getSetting('image_link') == 'content') {
      $entity = $items->getEntity();
      if (!$entity->isNew()) {
        

class UrlPlainFormatter extends FileFormatterBase {

  /** * {@inheritdoc} */
  public function viewElements(FieldItemListInterface $items$langcode) {
    $elements = [];

    foreach ($this->getEntitiesToView($items$langcode) as $delta => $file) {
      assert($file instanceof FileInterface);
      $elements[$delta] = [
        '#markup' => $file->createFileUrl(),
        '#cache' => [
          'tags' => $file->getCacheTags(),
        ],
      ];
    }

    return $elements;
  }

}

class EntityReferenceIdFormatter extends EntityReferenceFormatterBase {

  /** * {@inheritdoc} */
  public function viewElements(FieldItemListInterface $items$langcode) {
    $elements = [];

    foreach ($this->getEntitiesToView($items$langcode) as $delta => $entity) {
      if ($entity->id()) {
        $elements[$delta] = [
          '#plain_text' => $entity->id(),
          // Create a cache tag entry for the referenced entity. In the case           // that the referenced entity is deleted, the cache for referring           // entities must be cleared.           '#cache' => [
            'tags' => $entity->getCacheTags(),
          ],
        ];
      }
    }
$summary[] = $this->t('Linked to media item');
    }

    return $summary;
  }

  /** * {@inheritdoc} */
  public function viewElements(FieldItemListInterface $items$langcode) {
    $elements = [];
    $media_items = $this->getEntitiesToView($items$langcode);

    // Early opt-out if the field is empty.     if (empty($media_items)) {
      return $elements;
    }

    $image_style_setting = $this->getSetting('image_style');

    /** @var \Drupal\media\MediaInterface[] $media_items */
    foreach ($media_items as $delta => $media) {
      $elements[$delta] = [
        

class AuthorFormatter extends EntityReferenceFormatterBase {

  /** * {@inheritdoc} */
  public function viewElements(FieldItemListInterface $items$langcode) {
    $elements = [];

    foreach ($this->getEntitiesToView($items$langcode) as $delta => $entity) {
      $elements[$delta] = [
        '#theme' => 'username',
        '#account' => $entity,
        '#link_options' => ['attributes' => ['rel' => 'author']],
        '#cache' => [
          'tags' => $entity->getCacheTags(),
        ],
      ];
    }

    return $elements;
  }
Home | Imprint | This part of the site doesn't use cookies.