function simplenews_user_view in Simplenews 8
Same name and namespace in other branches
- 8.2 simplenews.module \simplenews_user_view()
- 7.2 simplenews.module \simplenews_user_view()
- 7 simplenews.module \simplenews_user_view()
- 3.x simplenews.module \simplenews_user_view()
Implements hook_user_view().
File
- ./
simplenews.module, line 531 - 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'] = array(
'#type' => 'details',
'#title' => t('Subscribed to'),
'#open' => TRUE,
);
// Collect newsletter to which the current user is subscribed.
// 'hidden' newsletters are not listed.
$newsletters = simplenews_newsletter_get_visible();
$subscriber = simplenews_subscriber_load_by_mail($account
->getEmail());
$links = [];
if ($subscriber) {
foreach ($newsletters 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'] = array(
'#theme' => 'item_list',
'#items' => $links,
);
}
else {
$build['simplenews']['subscriptions'] = array(
'#type' => 'item',
'#markup' => t('None'),
);
}
}
if ($account
->hasPermission('subscribe to newsletters')) {
$build['simplenews']['my_newsletters'] = array(
'#type' => 'link',
'#title' => t('Manage subscriptions'),
'#url' => new Url('simplenews.newsletter_subscriptions_user', array(
'user' => $account
->id(),
)),
);
}
}
}
}