function simplenews_user_view in Simplenews 3.x
Same name and namespace in other branches
- 8.2 simplenews.module \simplenews_user_view()
- 8 simplenews.module \simplenews_user_view()
- 7.2 simplenews.module \simplenews_user_view()
- 7 simplenews.module \simplenews_user_view()
Implements hook_ENTITY_TYPE_view() for user entity.
File
- ./
simplenews.module, line 503 - Simplenews node handling, sent email, newsletter block and general hooks.
Code
function simplenews_user_view(array &$build, UserInterface $account, EntityViewDisplayInterface $display, $view_mode) {
$user = \Drupal::currentUser();
$build['#cache']['contexts'][] = 'user.permissions';
if ($user
->id() == $account
->id() || $user
->hasPermission('administer users')) {
if ($display
->getComponent('simplenews')) {
$build['simplenews'] = [
'#type' => 'details',
'#title' => t('Subscribed to'),
'#open' => TRUE,
];
// Collect newsletter to which the current user is subscribed.
// 'hidden' newsletters are not listed.
$links = [];
if ($subscriber = Subscriber::loadByUid($account
->id())) {
foreach (simplenews_newsletter_get_visible() as $newsletter) {
if ($subscriber
->isSubscribed($newsletter
->id())) {
// @todo Make links
$links[] = $newsletter
->label();
}
}
}
// When a user has no permission to subscribe and is not subscribed
// we do not display the 'no subscriptions' message.
if ($account
->hasPermission('subscribe to newsletters')) {
if ($links) {
$build['simplenews']['subscriptions'] = [
'#theme' => 'item_list',
'#items' => $links,
];
}
else {
$build['simplenews']['subscriptions'] = [
'#type' => 'item',
'#markup' => t('None'),
];
}
}
if ($account
->hasPermission('subscribe to newsletters')) {
$build['simplenews']['my_newsletters'] = [
'#type' => 'link',
'#title' => t('Manage subscriptions'),
'#url' => new Url('simplenews.newsletter_subscriptions_user', [
'user' => $account
->id(),
]),
];
}
}
}
}