You are here

function share_everywhere_node_view in Share Everywhere 2.x

Same name and namespace in other branches
  1. 8 share_everywhere.module \share_everywhere_node_view()

Implements hook_ENTITY_TYPE_view().

File

./share_everywhere.module, line 136
A module that adds social buttons to your website.

Code

function share_everywhere_node_view(array &$build, EntityInterface $node, EntityViewDisplayInterface $display, $view_mode) {
  $config = \Drupal::config('share_everywhere.settings');
  $share_service = \Drupal::service('share_everywhere.service');
  $route_params = \Drupal::routeMatch()
    ->getParameters();
  if ($route_params
    ->has('node_preview') && $route_params
    ->get('node_preview')->in_preview || $share_service
    ->isRestricted($view_mode)) {
    return;
  }
  $url = $node
    ->toUrl()
    ->setAbsolute()
    ->toString();
  $id = $node
    ->getEntityTypeId() . $node
    ->id();
  $enabled_types = $config
    ->get('content_types');
  $enabled_view_modes = $config
    ->get('view_modes.' . $node
    ->bundle());
  $per_entity = $config
    ->get('per_entity');
  $enabled_nodes = $config
    ->get('enabled_entities.' . $node
    ->getEntityTypeId()) ?? [];
  switch ($config
    ->get('location')) {
    case 'content':
      if (isset($enabled_types[$node
        ->bundle()]) && $enabled_types[$node
        ->bundle()] === $node
        ->bundle() && $display
        ->getComponent('share_everywhere')) {
        if (isset($enabled_view_modes[$view_mode]) && $enabled_view_modes[$view_mode]) {
          if (!$per_entity || in_array($node
            ->id(), $enabled_nodes)) {
            $build['share_everywhere'] = $share_service
              ->build($url, $id);
          }
        }
      }
      break;
    case 'links':
      if (isset($enabled_types[$node
        ->bundle()]) && $enabled_types[$node
        ->bundle()] === $node
        ->bundle() && $display
        ->getComponent('links')) {
        if (isset($enabled_view_modes[$view_mode]) && $enabled_view_modes[$view_mode]) {
          if (!$per_entity || in_array($node
            ->id(), $enabled_nodes)) {
            $content = $share_service
              ->build($url, $id);
            $links['share_everywhere'] = [
              'title' => $content,
            ];
            $build['links'] = [
              '#theme' => 'links',
              '#links' => $links,
              '#tag' => 'div',
              '#type' => 'html_tag',
            ];
          }
        }
      }
      break;
  }
}