You are here

sharerich.module in Sharerich 8

Same filename and directory in other branches
  1. 7.3 sharerich.module
  2. 7 sharerich.module
  3. 7.2 sharerich.module

Contains sharerich.module..

File

sharerich.module
View source
<?php

/**
 * @file
 * Contains sharerich.module..
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Drupal\Core\Url;
use Drupal\Component\Utility\Html;
use Drupal\Core\Block\BlockPluginInterface;

/**
 * Implements hook_help().
 */
function sharerich_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'sharerich.admin_settings_form':
      $output = '';
      $output .= '<p>' . t('You can manage your Sharerich sets at <a href=":menu-settings">Structure > Sharerich</a>.', array(
        ':menu-settings' => Url::fromRoute('entity.sharerich.collection'),
      )) . '</p>';
      return $output;
      break;
    case 'entity.sharerich.collection':
      $output = '';
      $output .= '<p>' . t('Global settings are available at the <a href=":menu-settings">configuration page</a>.', array(
        ':menu-settings' => Url::fromRoute('sharerich.admin_settings_form'),
      )) . '</p>';
      return $output;
      break;
    default:
  }
}

/**
 * Implements hook_theme().
 */
function sharerich_theme() {
  return array(
    'sharerich' => array(
      'variables' => array(
        'buttons' => '',
      ),
      'template' => 'sharerich',
    ),
  );
}

/**
 * Implements hook_preprocess_sharerich().
 *
 * @param $variables
 */
function template_preprocess_sharerich(&$variables) {
}

/**
 * Implements hook_sharerich_buttons_alter().
 *
 * @param $buttons
 */
function sharerich_sharerich_buttons_alter(&$buttons) {
}

/**
 * Helper to scan the services folder.
 * @return A list of file names.
 */
function sharerich_get_default_services() {
  $dir = drupal_get_path('module', 'sharerich') . '/services';
  $list = \Drupal::service('file_system')
    ->scanDirectory($dir, '/.inc/', array(), 0);
  return array_map(function ($service) {
    return $service->name;
  }, $list);
}

/**
 * Helper to load services from the disk.
 */
function sharerich_load_default_service($service_name) {
  $dir = drupal_get_path('module', 'sharerich') . '/services';
  $service_name = $dir . '/' . $service_name . '.inc';
  if (file_exists($service_name)) {
    return file_get_contents($service_name);
  }
  return FALSE;
}

/**
 * Helper to return the list of allowed tags from the configuration.
 */
function sharerich_allowed_tags() {

  // Get list of allowed tags.
  $allowed_tags = \Drupal::config('sharerich.settings')
    ->get('allowed_html');

  // Convert to array.
  $allowed_tags = str_replace([
    '<',
    '>',
  ], '', $allowed_tags);
  $allowed_tags = Html::escape($allowed_tags);
  $allowed_tags = explode(' ', $allowed_tags);
  return $allowed_tags;
}

/**
 * Helper to return the data to be used when rendering tokens.
 *
 * @return array
 */
function _sharerich_get_token_data() {
  $route = \Drupal::request()->attributes
    ->get(RouteObjectInterface::ROUTE_NAME);
  switch ($route) {
    case 'entity.node.canonical':
      $data = [
        'node' => \Drupal::request()->attributes
          ->get('node'),
      ];
      break;
    case 'entity.taxonomy_term.canonical':
      $data = [
        'taxonomy_term' => \Drupal::request()->attributes
          ->get('taxonomy_term'),
      ];
      break;
    case 'entity.user.canonical':
      $data = [
        'user' => \Drupal::request()->attributes
          ->get('user'),
      ];
      break;
    default:
      $data = [];
  }
  return $data;
}

/**
 * Implements hook_block_view_alter().
 *
 * @param array $build
 * @param \Drupal\Core\Block\BlockPluginInterface $block
 */
function sharerich_block_view_alter(array &$build, BlockPluginInterface $block) {
  if ($block
    ->getPluginId() == 'sharerich') {
    $build['#contextual_links']['sharerich'] = array(
      'route_parameters' => array(
        'sharerich' => $block
          ->getConfiguration()['sharerich_set'],
      ),
    );
  }
}

Functions

Namesort descending Description
sharerich_allowed_tags Helper to return the list of allowed tags from the configuration.
sharerich_block_view_alter Implements hook_block_view_alter().
sharerich_get_default_services Helper to scan the services folder.
sharerich_help Implements hook_help().
sharerich_load_default_service Helper to load services from the disk.
sharerich_sharerich_buttons_alter Implements hook_sharerich_buttons_alter().
sharerich_theme Implements hook_theme().
template_preprocess_sharerich Implements hook_preprocess_sharerich().
_sharerich_get_token_data Helper to return the data to be used when rendering tokens.