You are here

facebookshare.module in Facebook Share 8

Same filename and directory in other branches
  1. 6.3 facebookshare.module
  2. 6 facebookshare.module
  3. 7 facebookshare.module

Adds a button to a node to share on a user's Facebook stream.

File

facebookshare.module
View source
<?php

/**
 * @file
 * Adds a button to a node to share on a user's Facebook stream.
 */
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function facebookshare_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.facebookshare':
      return '<p>' . t('Provides a way for viewers to share a link to a node on their Facebook stream.') . '</p>';
  }
}

/**
 * Implements hook_ENTITY_TYPE_view().
 */
function facebookshare_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  $config = \Drupal::config('facebookshare.settings');
  $settings = $config
    ->get();

  // Make sure we're on the right content type.
  if (in_array($entity
    ->getType(), $settings['facebookshare_types'])) {
    if ($view_mode == 'teaser' || $view_mode == 'full') {
      if (\Drupal::currentUser()
        ->hasPermission('access facebookshare')) {

        // Retrieve the location where we should show it.
        $location = $settings['facebookshare_location'];

        // Check if the current $view_mode has content to show.
        if ($view_mode == 'teaser' && !empty($location['teasers']) || $view_mode == 'full' && !empty($location['content'])) {

          // Retrieve the weight and url of the button.
          $url = Url::fromRoute('entity.node.canonical', array(
            'node' => $entity
              ->id(),
          ), array(
            'absolute' => TRUE,
          ));
          $weight = Html::escape($settings['facebookshare_weight']);

          // Add and theme the button.
          $build['facebookshare'] = array(
            '#theme' => 'facebookshare',
            '#url' => $url,
            '#weight' => is_numeric($weight) ? (int) $weight : -10,
            '#attached' => array(
              'library' => array(
                'facebookshare/facebookshare',
              ),
            ),
            '#cache' => array(
              'tags' => array(
                'config:facebookshare.settings',
              ),
            ),
          );
        }
      }
    }
  }
}

/**
 * Implements hook_theme().
 */
function facebookshare_theme($existing, $type, $theme, $path) {
  return array(
    'facebookshare' => array(
      'variables' => array(
        'url' => NULL,
        'size' => NULL,
        'text' => NULL,
      ),
    ),
  );
}

/**
 * Prepares variables for color scheme form templates.
 *
 * Default template: color-scheme-form.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - form: A render element representing the form.
 */
function template_preprocess_facebookshare(&$variables) {
  $config = \Drupal::config('facebookshare.settings');
  $settings = $config
    ->get();
  $variables['app_id'] = Html::escape($settings['facebookshare_app_id']);
  $variables['layout'] = Html::escape($settings['facebookshare_layout']);
  $variables['size'] = Html::escape($settings['facebookshare_size']);
  $variables['width'] = Html::escape($settings['facebookshare_width']);
  $variables['height'] = Html::escape($settings['facebookshare_height']);
  $variables['mobile_iframe'] = !empty($settings['facebookshare_mobile_iframe']) ? 'true' : 'false';
}

Functions

Namesort descending Description
facebookshare_help Implements hook_help().
facebookshare_node_view Implements hook_ENTITY_TYPE_view().
facebookshare_theme Implements hook_theme().
template_preprocess_facebookshare Prepares variables for color scheme form templates.