You are here

function addtoany_entity_view in AddToAny Share Buttons 8

Implements hook_ENTITY_TYPE_view().

File

./addtoany.module, line 90
Handle AddToAny integration.

Code

function addtoany_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {

  // Prevent error on preview of an unpublished node.
  if ($entity
    ->id() === NULL) {
    return;
  }
  if ($display
    ->getComponent('addtoany')) {
    $config = Drupal::config('addtoany.settings');
    $isAllowed = $config
      ->get("entities.{$entity->getEntityTypeId()}");
    if ($isAllowed) {
      $data = addtoany_create_entity_data($entity);
      $build['addtoany'] = [
        '#addtoany_html' => \Drupal::token()
          ->replace($data['addtoany_html'], [
          'node' => $entity,
        ]),
        '#link_url' => $data['link_url'],
        '#link_title' => $data['link_title'],
        '#button_setting' => $data['button_setting'],
        '#button_image' => $data['button_image'],
        '#universal_button_placement' => $data['universal_button_placement'],
        '#buttons_size' => $data['buttons_size'],
        '#entity_type' => $entity
          ->getEntityType()
          ->id(),
        '#bundle' => $entity
          ->bundle(),
        '#theme' => 'addtoany_standard',
      ];
    }
  }
}