You are here

share_everywhere.module in Share Everywhere 8

Same filename and directory in other branches
  1. 2.x share_everywhere.module

A module that adds social buttons to your website.

File

share_everywhere.module
View source
<?php

/**
 * @file
 * A module that adds social buttons to your website.
 */
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function share_everywhere_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.share_everywhere':
      $text = file_get_contents(dirname(__FILE__) . "/README.md");
      if (!\Drupal::moduleHandler()
        ->moduleExists('markdown')) {
        return '<pre>' . $text . '</pre>';
      }
      else {

        // Use the Markdown filter to render the README.
        $filter_manager = \Drupal::service('plugin.manager.filter');
        $settings = \Drupal::configFactory()
          ->get('markdown.settings')
          ->getRawData();
        $config = [
          'settings' => $settings,
        ];
        $filter = $filter_manager
          ->createInstance('markdown', $config);
        return $filter
          ->process($text, 'en');
      }
  }
  return NULL;
}

/**
 * Implements hook_theme().
 */
function share_everywhere_theme($existing, $type, $theme, $path) {
  $template_vars = [
    'url' => NULL,
    'content' => NULL,
  ];
  $variables = [
    'share_everywhere' => [
      'variables' => [
        'attributes' => [
          'class' => NULL,
        ],
        'title' => NULL,
        'share_icon' => NULL,
        'se_links_id' => NULL,
        'is_active' => NULL,
        'facebook_like' => NULL,
        'buttons' => NULL,
      ],
      'template' => 'share-everywhere',
    ],
    'se_facebook_like' => [
      'variables' => [
        'url' => NULL,
      ],
      'template' => 'se-facebook-like',
    ],
    'se_facebook_share' => [
      'variables' => $template_vars,
      'template' => 'se-facebook-share',
    ],
    'se_twitter' => [
      'variables' => $template_vars,
      'template' => 'se-twitter',
    ],
    'se_linkedin' => [
      'variables' => $template_vars,
      'template' => 'se-linkedin',
    ],
    'se_messenger' => [
      'variables' => $template_vars,
      'template' => 'se-messenger',
    ],
    'se_viber' => [
      'variables' => $template_vars,
      'template' => 'se-viber',
    ],
    'se_whatsapp' => [
      'variables' => $template_vars,
      'template' => 'se-whatsapp',
    ],
    'se_copy' => [
      'variables' => $template_vars,
      'template' => 'se-copy-url',
    ],
  ];
  return $variables;
}

/**
 * Implements hook_entity_extra_field_info().
 */
function share_everywhere_entity_extra_field_info() {
  $config = \Drupal::config('share_everywhere.settings');
  $extra = [];
  if ($config
    ->get('location') == 'content') {
    $entity_info = \Drupal::service('entity_type.bundle.info')
      ->getAllBundleInfo();
    if (isset($entity_info['node'])) {
      foreach ($entity_info['node'] as $bundle => $bundle_info) {
        $extra['node'][$bundle]['display'] = [
          'share_everywhere' => [
            'label' => t('Share Everywhere'),
            'description' => t('Share Everywhere buttons'),
            'weight' => $config
              ->get('weight'),
          ],
        ];
      }
    }
    if (isset($entity_info['commerce_product'])) {
      foreach ($entity_info['commerce_product'] as $bundle => $bundle_info) {
        $extra['commerce_product'][$bundle]['display'] = [
          'share_everywhere' => [
            'label' => t('Share Everywhere'),
            'description' => t('Share Everywhere buttons'),
            'weight' => $config
              ->get('weight'),
          ],
        ];
      }
    }
  }
  return $extra;
}

/**
 * Implements hook_ENTITY_TYPE_view().
 */
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;
  }
}

/**
 * Implements hook_ENTITY_TYPE_view().
 */
function share_everywhere_commerce_product_view(array &$build, EntityInterface $product, EntityViewDisplayInterface $display, $view_mode) {
  $config = \Drupal::config('share_everywhere.settings');
  $share_service = \Drupal::service('share_everywhere.service');
  $module_handler = \Drupal::service('module_handler');
  if (!$module_handler
    ->moduleExists('commerce_product') || $share_service
    ->isRestricted($view_mode)) {
    return;
  }
  $url = $product
    ->toUrl()
    ->setAbsolute()
    ->toString();
  $id = $product
    ->getEntityTypeId() . $product
    ->id();
  $enabled_types = $config
    ->get('product_types');
  $enabled_view_modes = $config
    ->get('product_view_modes.' . $product
    ->bundle());
  $per_entity = $config
    ->get('per_entity');
  $enabled_products = $config
    ->get('enabled_entities.' . $product
    ->getEntityTypeId()) ?? [];
  if ($config
    ->get('location') == 'content') {
    if (isset($enabled_types[$product
      ->bundle()]) && $enabled_types[$product
      ->bundle()] === $product
      ->bundle() && $display
      ->getComponent('share_everywhere')) {
      if (isset($enabled_view_modes[$view_mode]) && $enabled_view_modes[$view_mode]) {
        if (!$per_entity || in_array($product
          ->id(), $enabled_products)) {
          $build['share_everywhere'] = $share_service
            ->build($url, $id);
        }
      }
    }
  }
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function share_everywhere_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  share_everywhere_inject_into_entity_forms($form, $form_state);
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function share_everywhere_form_commerce_product_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  share_everywhere_inject_into_entity_forms($form, $form_state);
}

/**
 * Injects Share Everywhere settings into node or product forms.
 */
function share_everywhere_inject_into_entity_forms(&$form, FormStateInterface $form_state) {
  $config = \Drupal::config('share_everywhere.settings');
  $entity = $form_state
    ->getFormObject()
    ->getEntity();
  if ($entity
    ->getEntityTypeId() == 'commerce_product') {
    $enabled_types = $config
      ->get('product_types');
  }
  else {
    $enabled_types = $config
      ->get('content_types');
  }
  if ($config
    ->get('per_entity') && in_array($entity
    ->bundle(), $enabled_types)) {
    $enabled_products = $config
      ->get('enabled_entities.' . $entity
      ->getEntityTypeId()) ?? [];
    $entity_is_enabled = in_array($entity
      ->id(), $enabled_products);
    $form['share_everywhere'] = [
      '#type' => 'details',
      '#title' => t('Share Everywhere Settings'),
      '#group' => 'advanced',
      '#weight' => 100,
      '#tree' => TRUE,
      '#open' => $entity_is_enabled,
    ];
    $form['share_everywhere']['enabled'] = [
      '#type' => 'checkbox',
      '#title' => t('Show social share buttons'),
      '#default_value' => $entity_is_enabled,
    ];
    $form['actions']['submit']['#submit'][] = 'share_everywhere_entity_form_submit';
  }
}

/**
 * Custom form submit to save Share Everywhere settings on entities.
 */
function share_everywhere_entity_form_submit(&$form, FormStateInterface $form_state) {
  $config = \Drupal::configFactory()
    ->getEditable('share_everywhere.settings');
  $entity = $form_state
    ->getFormObject()
    ->getEntity();
  $config_key = 'enabled_entities.' . $entity
    ->getEntityTypeId();
  $enabled_entities = $config
    ->get($config_key) ?? [];
  $se_enabled = $form_state
    ->getValue([
    'share_everywhere',
    'enabled',
  ], 0);
  $key = array_search($entity
    ->id(), $enabled_entities);
  if ($key !== FALSE && !$se_enabled) {
    unset($enabled_entities[$key]);
    $config
      ->set($config_key, array_values($enabled_entities))
      ->save();
  }
  elseif ($key === FALSE && $se_enabled) {
    $enabled_entities[] = $entity
      ->id();
    $config
      ->set($config_key, $enabled_entities)
      ->save();
  }
}