You are here

public function ActivityEntityReferenceFormatter::viewElements in Open Social 8.5

Same name and namespace in other branches
  1. 8.9 modules/custom/activity_viewer/src/Plugin/Field/FieldFormatter/ActivityEntityReferenceFormatter.php \Drupal\activity_viewer\Plugin\Field\FieldFormatter\ActivityEntityReferenceFormatter::viewElements()
  2. 8 modules/custom/activity_viewer/src/Plugin/Field/FieldFormatter/ActivityEntityReferenceFormatter.php \Drupal\activity_viewer\Plugin\Field\FieldFormatter\ActivityEntityReferenceFormatter::viewElements()
  3. 8.2 modules/custom/activity_viewer/src/Plugin/Field/FieldFormatter/ActivityEntityReferenceFormatter.php \Drupal\activity_viewer\Plugin\Field\FieldFormatter\ActivityEntityReferenceFormatter::viewElements()
  4. 8.3 modules/custom/activity_viewer/src/Plugin/Field/FieldFormatter/ActivityEntityReferenceFormatter.php \Drupal\activity_viewer\Plugin\Field\FieldFormatter\ActivityEntityReferenceFormatter::viewElements()
  5. 8.4 modules/custom/activity_viewer/src/Plugin/Field/FieldFormatter/ActivityEntityReferenceFormatter.php \Drupal\activity_viewer\Plugin\Field\FieldFormatter\ActivityEntityReferenceFormatter::viewElements()
  6. 8.6 modules/custom/activity_viewer/src/Plugin/Field/FieldFormatter/ActivityEntityReferenceFormatter.php \Drupal\activity_viewer\Plugin\Field\FieldFormatter\ActivityEntityReferenceFormatter::viewElements()
  7. 8.7 modules/custom/activity_viewer/src/Plugin/Field/FieldFormatter/ActivityEntityReferenceFormatter.php \Drupal\activity_viewer\Plugin\Field\FieldFormatter\ActivityEntityReferenceFormatter::viewElements()
  8. 8.8 modules/custom/activity_viewer/src/Plugin/Field/FieldFormatter/ActivityEntityReferenceFormatter.php \Drupal\activity_viewer\Plugin\Field\FieldFormatter\ActivityEntityReferenceFormatter::viewElements()
  9. 10.3.x modules/custom/activity_viewer/src/Plugin/Field/FieldFormatter/ActivityEntityReferenceFormatter.php \Drupal\activity_viewer\Plugin\Field\FieldFormatter\ActivityEntityReferenceFormatter::viewElements()
  10. 10.0.x modules/custom/activity_viewer/src/Plugin/Field/FieldFormatter/ActivityEntityReferenceFormatter.php \Drupal\activity_viewer\Plugin\Field\FieldFormatter\ActivityEntityReferenceFormatter::viewElements()
  11. 10.1.x modules/custom/activity_viewer/src/Plugin/Field/FieldFormatter/ActivityEntityReferenceFormatter.php \Drupal\activity_viewer\Plugin\Field\FieldFormatter\ActivityEntityReferenceFormatter::viewElements()
  12. 10.2.x modules/custom/activity_viewer/src/Plugin/Field/FieldFormatter/ActivityEntityReferenceFormatter.php \Drupal\activity_viewer\Plugin\Field\FieldFormatter\ActivityEntityReferenceFormatter::viewElements()

File

modules/custom/activity_viewer/src/Plugin/Field/FieldFormatter/ActivityEntityReferenceFormatter.php, line 26

Class

ActivityEntityReferenceFormatter
Provides a custom dynamic entity reference formatter.

Namespace

Drupal\activity_viewer\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $view_mode = $this
    ->getSetting('view_mode');
  $elements = [];
  foreach ($this
    ->getEntitiesToView($items, $langcode) as $delta => $entity) {

    // Protect ourselves from recursive rendering.
    static $depth = 0;
    $depth++;
    if ($depth > 20) {
      $this->loggerFactory
        ->get('entity')
        ->error('Recursive rendering detected when rendering entity @entity_type @entity_id. Aborting rendering.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '@entity_id' => $entity
          ->id(),
      ]);
      return $elements;
    }
    if ($entity
      ->id()) {
      $view_builder = $this->entityTypeManager
        ->getViewBuilder($entity
        ->getEntityTypeId());
      $elements[$delta] = $view_builder
        ->view($entity, $view_mode, $entity
        ->language()
        ->getId());

      // Add a resource attribute to set the mapping property's value to the
      // entity's url. Since we don't know what the markup of the entity will
      // be, we shouldn't rely on it for structured data such as RDFa.
      if (!empty($items[$delta]->_attributes)) {
        $items[$delta]->_attributes += [
          'resource' => $entity
            ->url(),
        ];
      }
    }
    else {

      // This is an "auto_create" item.
      $elements[$delta] = [
        '#markup' => $entity
          ->label(),
      ];
    }
    $depth = 0;
  }
  return $elements;
}