You are here

class ActivityEntityReferenceFormatter in Open Social 10.2.x

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
  2. 8 modules/custom/activity_viewer/src/Plugin/Field/FieldFormatter/ActivityEntityReferenceFormatter.php \Drupal\activity_viewer\Plugin\Field\FieldFormatter\ActivityEntityReferenceFormatter
  3. 8.2 modules/custom/activity_viewer/src/Plugin/Field/FieldFormatter/ActivityEntityReferenceFormatter.php \Drupal\activity_viewer\Plugin\Field\FieldFormatter\ActivityEntityReferenceFormatter
  4. 8.3 modules/custom/activity_viewer/src/Plugin/Field/FieldFormatter/ActivityEntityReferenceFormatter.php \Drupal\activity_viewer\Plugin\Field\FieldFormatter\ActivityEntityReferenceFormatter
  5. 8.4 modules/custom/activity_viewer/src/Plugin/Field/FieldFormatter/ActivityEntityReferenceFormatter.php \Drupal\activity_viewer\Plugin\Field\FieldFormatter\ActivityEntityReferenceFormatter
  6. 8.5 modules/custom/activity_viewer/src/Plugin/Field/FieldFormatter/ActivityEntityReferenceFormatter.php \Drupal\activity_viewer\Plugin\Field\FieldFormatter\ActivityEntityReferenceFormatter
  7. 8.6 modules/custom/activity_viewer/src/Plugin/Field/FieldFormatter/ActivityEntityReferenceFormatter.php \Drupal\activity_viewer\Plugin\Field\FieldFormatter\ActivityEntityReferenceFormatter
  8. 8.7 modules/custom/activity_viewer/src/Plugin/Field/FieldFormatter/ActivityEntityReferenceFormatter.php \Drupal\activity_viewer\Plugin\Field\FieldFormatter\ActivityEntityReferenceFormatter
  9. 8.8 modules/custom/activity_viewer/src/Plugin/Field/FieldFormatter/ActivityEntityReferenceFormatter.php \Drupal\activity_viewer\Plugin\Field\FieldFormatter\ActivityEntityReferenceFormatter
  10. 10.3.x modules/custom/activity_viewer/src/Plugin/Field/FieldFormatter/ActivityEntityReferenceFormatter.php \Drupal\activity_viewer\Plugin\Field\FieldFormatter\ActivityEntityReferenceFormatter
  11. 10.0.x modules/custom/activity_viewer/src/Plugin/Field/FieldFormatter/ActivityEntityReferenceFormatter.php \Drupal\activity_viewer\Plugin\Field\FieldFormatter\ActivityEntityReferenceFormatter
  12. 10.1.x modules/custom/activity_viewer/src/Plugin/Field/FieldFormatter/ActivityEntityReferenceFormatter.php \Drupal\activity_viewer\Plugin\Field\FieldFormatter\ActivityEntityReferenceFormatter

Provides a custom dynamic entity reference formatter.

Plugin annotation


@FieldFormatter(
  id = "activity_viewer_entity_reference_formatter",
  module = "activity_viewer",
  label = @Translation("Custom dynamic entity reference formatter for activities"),
  field_types = {
    "dynamic_entity_reference"
  }
)

Hierarchy

  • class \Drupal\activity_viewer\Plugin\Field\FieldFormatter\ActivityEntityReferenceFormatter extends \Drupal\dynamic_entity_reference\Plugin\Field\FieldFormatter\DynamicEntityReferenceEntityFormatter

Expanded class hierarchy of ActivityEntityReferenceFormatter

File

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

Namespace

Drupal\activity_viewer\Plugin\Field\FieldFormatter
View source
class ActivityEntityReferenceFormatter extends DynamicEntityReferenceEntityFormatter {

  /**
   * {@inheritdoc}
   */
  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
              ->toUrl(),
          ];
        }
      }
      else {

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

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $options = $this->entityDisplayRepository
      ->getAllViewModes();
    $only_view_modes = [];
    foreach ($options as $entity) {
      foreach ($entity as $key => $view_mode) {
        $only_view_modes[$key] = $view_mode['label'];
      }
    }
    $elements['view_mode'] = [
      '#type' => 'select',
      '#options' => $only_view_modes,
      '#title' => t('View mode'),
      '#default_value' => $this
        ->getSetting('view_mode'),
      '#required' => TRUE,
    ];
    return $elements;
  }

}

Members