function simplenews_node_view in Simplenews 3.x
Same name and namespace in other branches
- 8.2 simplenews.module \simplenews_node_view()
- 8 simplenews.module \simplenews_node_view()
- 7.2 simplenews.module \simplenews_node_view()
- 7 simplenews.module \simplenews_node_view()
Implements hook_ENTITY_TYPE_view() for node entity.
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 = [
'node' => $node,
];
// If the current user is a subscriber, extend context.
$user = \Drupal::currentUser();
if ($subscriber = Subscriber::loadByUid($user
->id())) {
$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, []);
}
}
}
}