function simplenews_node_view in Simplenews 8
Same name and namespace in other branches
- 8.2 simplenews.module \simplenews_node_view()
- 7.2 simplenews.module \simplenews_node_view()
- 7 simplenews.module \simplenews_node_view()
- 3.x simplenews.module \simplenews_node_view()
Implements hook_node_view().
File
- ./
simplenews.module, line 75 - Simplenews node handling, sent email, newsletter block and general hooks
Code
function simplenews_node_view(array &$build, NodeInterface $node, $display, $view_mode) {
if (!simplenews_check_node_types($node
->getType())) {
return;
}
// Only do token replacements for view modes other than the our own email view
// modes. Token replacements for them will happen later on.
if (strpos($view_mode, 'email_') !== FALSE) {
return;
}
// Build up content, add as much as there is.
$context = array(
'node' => $node,
);
// If the current user is a subscriber, extend context.
$user = \Drupal::currentUser();
if ($user
->id() > 0 && ($subscriber = simplenews_subscriber_load_by_mail($user
->getEmail()))) {
$context['simplenews_subscriber'] = $subscriber;
}
// Loop over all render array elements.
foreach (Element::children($build) as $key) {
$element =& $build[$key];
// Make sure this is a field.
if (!isset($element['#field_type'])) {
continue;
}
// Loop over all field values.
foreach (Element::children($element) as $field_key) {
$item =& $element[$field_key];
// Only fields which result in simple markup elements are supported for
// token replacements for now.
if (isset($item['#markup'])) {
$item['#markup'] = \Drupal::token()
->replace($item['#markup'], $context, array());
}
}
}
}