You are here

public function SocialPrivateMessageMemberFormatter::viewElements in Open Social 8

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

modules/social_features/social_private_message/src/Plugin/Field/Fieldformatter/SocialPrivateMessageMemberFormatter.php, line 191

Class

SocialPrivateMessageMemberFormatter
Class SocialPrivateMessageMemberFormatter.

Namespace

Drupal\social_private_message\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $users = [];
  $view_builder = $this->entityManager
    ->getViewBuilder('user');
  foreach ($items as $delta => $item) {
    $user = $item->entity;
    if ($user
      ->id() != $this->currentUser
      ->id()) {
      if ($this
        ->getSetting('display_type') == 'label') {
        $users[$user
          ->id()] = $user
          ->getDisplayName();
      }
      elseif ($this
        ->getSetting('display_type') == 'entity') {
        $renderable = $view_builder
          ->view($user, $this
          ->getSetting('entity_display_mode'));
        $users[$user
          ->id()] = render($renderable);
      }
    }
  }
  $separator = $this
    ->getSetting('display_type') == 'label' ? ', ' : '';
  if (count($users) == 1) {
    $recipient = User::load(key($users));

    // Load compact notification view mode of the attached profile.
    if ($recipient instanceof User) {
      $storage = \Drupal::entityTypeManager()
        ->getStorage('profile');
      if (!empty($storage)) {
        $user_profile = $storage
          ->loadByUser($recipient, 'profile');
        if ($user_profile) {
          $content = \Drupal::entityTypeManager()
            ->getViewBuilder('profile')
            ->view($user_profile, 'compact_private_message');

          // Add to a new field, so twig can render it.
          $profile_picture = $content;
        }
      }
    }
    $participants = $profile_picture;
  }
  else {
    $participants['#markup'] = '<div class="media-left avatar"><span class="avatar-icon avatar-group-icon avatar-group-icon--small"></span></div><div class="media-body">' . implode($separator, $users) . '</div>';
  }
  $element = [
    '#prefix' => '<div class="media message__recipients">',
    '#suffix' => '</div>',
  ];
  $element += $participants;
  return $element;
}