You are here

fblikebutton.module in Facebook Like Button 8

File

fblikebutton.module
View source
<?php

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;

/**
 * Implements hook_help()
 */
function fblikebutton_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.fblikebutton':
      $output = '<p>Configure the dynamic Like button. This Like button will like the URL you\'re visiting. ';
      $output .= 'You can set the content types on which the button displays, choose to display it in the ';
      $output .= 'content block or in the links area.</p>';
      $output .= '<p style="font-weight:bold;">You can also configure Static Like Button Blocks in the @link.</p>';
      return t($output, array(
        '@link' => Link::fromTextAndUrl('Block Layout page', Url::fromRoute('block.admin_display'))
          ->toString(),
      ));
  }
}

/**
 * Implements hook_ENTITY_TYPE_view().
 */
function fblikebutton_node_view(array &$build, EntityInterface $node, EntityViewDisplayInterface $display, $view_mode) {
  $user = $account = \Drupal::currentUser();
  $config = \Drupal::config('fblikebutton.settings');
  $types = $config
    ->get('node_types');
  $full_node_display = $config
    ->get('full_node_display');
  $teaser_display = $config
    ->get('teaser_display');
  $full = $view_mode == 'full' ? TRUE : FALSE;
  $show = $types[$node
    ->getType()] && $user
    ->hasPermission('access fblikebutton');
  $likebutton_weight = $config
    ->get('weight');
  if ($show) {

    // Content area
    if ($view_mode == 'teaser' && $teaser_display == 1 || $view_mode == 'full' && $full_node_display == 0) {
      $url = fblikebutton_get_node_url($node
        ->id());
      $build['fblikebutton_field'] = array(
        '#theme' => 'fblikebutton',
        '#weight' => $likebutton_weight,
        '#url' => $url,
      );
    }
  }
}
function hook_node_links_alter(array &$node_links, NodeInterface $node, array &$context) {
  $view_mode = $context['view_mode'];

  // Link area
  if ($view_mode == 'teaser' && $teaser_display == 2 || $view_mode == 'full' && $full_node_display == 1) {
    $url = fblikebutton_get_node_url($node
      ->id());
  }
  $node_links['fblikebutton_field'] = array(
    '#theme' => 'fblikebutton',
    '#url' => $url,
  );
}

/**
 * Get the node url to add in the like button
 *
 * Node id
 * @var $node_id
 */
function fblikebutton_get_node_url($node_id) {
  $url_generator = \Drupal::urlGenerator();
  return $url_generator
    ->generateFromRoute('entity.node.canonical', array(
    'node' => $node_id,
  ), array(
    'absolute' => TRUE,
  ));
}

/**
 * Helper function to generate the configuration array used to generate the like
 * button.
 */
function fblikebutton_conf() {

  // Get current URL.
  // This will be used in the blocks for content types.
  $url_generator = \Drupal::urlGenerator();
  $webpage_to_like = $url_generator
    ->generateFromRoute('<current>', array(), array(
    'absolute' => TRUE,
  ));
  $config = \Drupal::config('fblikebutton.settings');
  $conf = array(
    'layout' => $config
      ->get('layout'),
    'action' => $config
      ->get('action'),
    'color_scheme' => $config
      ->get('color_scheme'),
    'show_faces' => $config
      ->get('show_faces'),
    'font' => $config
      ->get('font'),
    'language' => $config
      ->get('language'),
    'url' => $webpage_to_like,
  );
  return $conf;
}

/**
 * Implements hook_theme().
 */
function fblikebutton_theme() {
  return array(
    'fblikebutton' => array(
      'variables' => fblikebutton_conf(),
      'template' => 'fblikebutton',
      'file' => 'fblikebutton.theme',
    ),
  );
}

Functions

Namesort descending Description
fblikebutton_conf Helper function to generate the configuration array used to generate the like button.
fblikebutton_get_node_url Get the node url to add in the like button
fblikebutton_help Implements hook_help()
fblikebutton_node_view Implements hook_ENTITY_TYPE_view().
fblikebutton_theme Implements hook_theme().
hook_node_links_alter