public function PrivateMessageThreadMemberFormatter::viewElements in Private Message 8
Same name and namespace in other branches
- 8.2 src/Plugin/Field/FieldFormatter/PrivateMessageThreadMemberFormatter.php \Drupal\private_message\Plugin\Field\FieldFormatter\PrivateMessageThreadMemberFormatter::viewElements()
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
- src/
Plugin/ Field/ FieldFormatter/ PrivateMessageThreadMemberFormatter.php, line 215
Class
- PrivateMessageThreadMemberFormatter
- Defines the private message member field formatter.
Namespace
Drupal\private_message\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
$access_profiles = $this->currentUser
->hasPermission('access user profiles');
$users = [];
$view_builder = $this->entityManager
->getViewBuilder('user');
foreach ($items as $delta => $item) {
$user = $item->entity;
if ($user) {
if ($user
->id() != $this->currentUser
->id()) {
if ($this
->getSetting('display_type') == 'label') {
if ($access_profiles) {
$url = Url::fromRoute('entity.user.canonical', [
'user' => $user
->id(),
]);
$users[$user
->id()] = new FormattableMarkup('<a href=":link">@username</a>', [
':link' => $url
->toString(),
'@username' => $user
->getDisplayName(),
]);
}
else {
$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);
}
}
}
else {
$users['Missing-' . $delta] = '<em>' . $this
->t('User Deleted') . '</em>';
}
}
$element = [
'#prefix' => '<div class="private-message-recipients">',
'#suffix' => '</div>',
'#markup' => '',
];
$members_prefix = $this
->getSetting('members_prefix');
if (strlen($members_prefix)) {
$element['#markup'] .= '<span>' . $members_prefix . ' </span>';
}
$separator = $this
->getSetting('display_type') == 'label' ? ', ' : '';
$element['#markup'] .= implode($separator, $users);
return $element;
}