You are here

better_social_sharing_buttons.module in Better Social Sharing Buttons 8.3

Same filename and directory in other branches
  1. 8 better_social_sharing_buttons.module

Add template file for the social buttons.

File

better_social_sharing_buttons.module
View source
<?php

/**
 * @file
 * Add template file for the social buttons.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\Entity\NodeType;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;

/**
 * Implements hook_theme().
 *
 * Defines template files for the social sharing buttons module.
 */
function better_social_sharing_buttons_theme($existing, $type, $theme, $path) {
  return [
    'better_social_sharing_buttons' => [
      'variables' => [
        'items' => '',
      ],
      'template' => 'better-social-sharing-buttons',
    ],
  ];
}

/**
 * Implements hook_entity_extra_field_info().
 */
function better_social_sharing_buttons_entity_extra_field_info() {
  if (\Drupal::config('better_social_sharing_buttons.settings')
    ->get('node_field')) {
    $extra = [];
    foreach (NodeType::loadMultiple() as $bundle) {
      $extra['node'][$bundle
        ->Id()]['display']['sharing_buttons'] = [
        'label' => t('Better Social Sharing Buttons'),
        'description' => '',
        'weight' => 100,
        'visible' => TRUE,
      ];
    }
    return $extra;
  }
}

/**
 * Implements hook_ENTITY_TYPE_view().
 *
 * @throws \Drupal\Core\Entity\EntityMalformedException
 */
function better_social_sharing_buttons_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  if ($display
    ->getComponent('sharing_buttons')) {
    $config = \Drupal::config('better_social_sharing_buttons.settings');
    $items = [];
    global $base_url;
    $items['page_url'] = $entity
      ->toUrl('canonical', [
      'absolute' => TRUE,
    ]);
    $items['description'] = '';
    $items['title'] = $entity
      ->get('title')->value;
    $items['width'] = $config
      ->get('width');
    $items['radius'] = $config
      ->get('radius');
    $items['facebook_app_id'] = $config
      ->get('facebook_app_id');
    $items['print_css'] = $config
      ->get('print_css');
    $items['iconset'] = $config
      ->get('iconset');
    $items['services'] = $config
      ->get('services');
    $items['base_url'] = $base_url;
    $build['sharing_buttons'] = [
      '#theme' => 'better_social_sharing_buttons',
      '#items' => $items,
    ];
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function better_social_sharing_buttons_preprocess_better_social_sharing_buttons(&$variables) {
  if (isset($variables['items']['iconset'])) {
    $variables['social_buttons_sprite'] = '/' . drupal_get_path('module', 'better_social_sharing_buttons') . '/assets/dist/sprites/' . $variables['items']['iconset'] . '.svg';
    $variables['items']['iconset'];
  }
  else {
    $config = \Drupal::config('better_social_sharing_buttons.settings');
    $variables['social_buttons_sprite'] = '/' . drupal_get_path('module', 'better_social_sharing_buttons') . '/assets/dist/sprites/' . $config
      ->get('iconset') . '.svg';
  }
}

/**
 * Implements hook_help().
 *
 * @inheritdoc
 */
function better_social_sharing_buttons_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the better_social_sharing_buttons module.
    case 'help.page.better_social_sharing_buttons':
      $output = '<h2>' . t('Add social sharing buttons via twig (Twig Tweak module v2.0 or higher)') . '</h2>';
      $output .= '<p>' . t('Twig Tweak version 2.0 and above can print blocks that are not instantiated by using the block id:') . '</p>';
      $output .= '<p>' . t('*NOTE: This module was initially meant to be used on node detail pages because it gets the title and url for sharing from the current node.*') . '</p>';
      $output .= '<p>' . t('*It is possible to add sharing buttons on teasers. A separate twig file was created for this so you can include this and pass the necessary parameters to it (title, url, description). On your teaser twig file, you can use this as follows:*') . '</p>';
      $output .= '<code>' . "{# -- Social sharing buttons -- #}<br>\n{% set services = ['facebook', 'twitter', 'email', 'linkedin'] %}<br>\n{% include '/modules/contrib/better_social_sharing_buttons/theme/better-social-sharing-buttons.html.twig' with {<br>\n  'title': item.title,<br>\n  'url': item.url,<br>\n  'description': item.description|raw,<br>\n  'services': services<br>\n} %}" . '</code>';
      $output .= '<p>' . t('*As you can see, this way you can set which fields of your node contain the necessary info and you can set the services you want displayed.*') . '</p>';
      $output .= '<h2>' . t('Add social sharing buttons via a block') . '</h2>';
      $output .= '<p>' . t('You can add a block via block place or layout builder and set independent configuration for those blocks.') . '</p>';
      $output .= '<h2>' . t('Add social sharing buttons via a field') . '</h2>';
      $output .= '<p>' . t('This module also provides a field (Better Social Sharing Buttons field) through a pseudo field. To see this field, you must enable the feature in the configuration and then adjust the display mode of your nodes. When the feature is enabled, the field is enabled for all content types. You will need to adjust it for each content type as desired.') . '</p>';
      return $output;
    default:
  }
}