You are here

function nopremium_node_view in Node Option Premium 8

Same name and namespace in other branches
  1. 7 nopremium.module \nopremium_node_view()

Implements hook_ENTITY_TYPE_view() for 'node'.

File

./nopremium.module, line 170
Hook implementations.

Code

function nopremium_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  $user = \Drupal::currentUser();
  $token_service = \Drupal::token();
  $config = \Drupal::config('nopremium.settings');
  if ($view_mode == $config
    ->get('teaser_view_mode') && !empty($entity->original_view_mode) && $entity
    ->hasField('premium') && !empty($entity->premium->value)) {

    // Get content type specific premium message.
    $message = $config
      ->get('messages.' . $entity
      ->bundle());
    if (empty($message)) {

      // If the content specific message is empty, pick the default one.
      $message = $config
        ->get('default_message');
    }

    // Replace tokens if they are available in the premium message.
    $message = $token_service
      ->replace($message, [
      'user' => $user,
      'node' => $entity,
    ]);
    $build['premium_message'] = [
      '#markup' => $message,
    ];

    // Hide links by default. This ensures that on full content displays, no
    // read more link gets shown.
    if (isset($build['links'])) {
      $build['links']['#access'] = FALSE;
    }
  }
}