You are here

sharethis_block.module in Sharethis block 7

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

Hook implementations for the sharethis_block module.

File

sharethis_block.module
View source
<?php

/**
 * @file
 * Hook implementations for the sharethis_block module.
 */

/**
 * Implements hook_theme().
 */
function sharethis_block_theme($existing, $type, $theme, $path) {
  return [
    'block__sharethis_block' => [
      'render element' => 'elements',
      'base hook' => 'block',
      'template' => 'block--sharethis-block',
      'path' => drupal_get_path('module', 'sharethis_block') . '/templates',
    ],
  ];
}

/**
 * Implements hook_block_info().
 */
function sharethis_block_block_info() {
  $blocks = [];
  $blocks['sharethis'] = [
    'info' => t('Sharethis block'),
    'cache' => DRUPAL_CACHE_PER_PAGE,
  ];
  return $blocks;
}

/**
 * Implements hook_block_configure().
 */
function sharethis_block_block_configure($delta = '') {
  $form = [];
  switch ($delta) {
    case 'sharethis':
      $form['sharethis_property_id'] = [
        '#type' => 'textfield',
        '#required' => TRUE,
        '#title' => t('Property ID'),
        '#description' => t('The unique property ID from your sharethis account.'),
        '#default_value' => variable_get('sharethis_property_id', ''),
      ];
      $form['sharethis_inline'] = [
        '#type' => 'radios',
        '#options' => [
          '1' => 'Inline',
          '0' => 'Sticky',
        ],
        '#required' => TRUE,
        '#title' => t('Inline or sticky'),
        '#description' => t('Whether to show inline buttons, placed with the block, or sticky buttons on the side or bottom of the page.'),
        '#default_value' => variable_get('sharethis_inline', 0),
      ];
      break;
  }
  return $form;
}

/**
 * Implements hook_block_save().
 */
function sharethis_block_block_save($delta = '', $edit = []) {
  switch ($delta) {
    case 'sharethis':
      variable_set('sharethis_property_id', $edit['sharethis_property_id']);
      variable_set('sharethis_inline', $edit['sharethis_inline']);
      break;
  }
}

/**
 * Implements hook_block_view().
 */
function sharethis_block_block_view($delta = '') {
  $block = [];
  switch ($delta) {
    case 'sharethis':
      $block['subject'] = '';
      $block['content'] = sharethis_block_output();
      break;
  }
  return $block;
}

/**
 * Returns a render array with the block output and library attached.
 *
 * @return array
 *   The render array for the block.
 */
function sharethis_block_output() {
  $product_type = 'sop';

  // If inline buttons have been chosen then there is some actual markup
  // which is used to place the buttons on the page.
  if (variable_get('sharethis_inline', FALSE)) {
    $build['buttons'] = [
      '#markup' => '<div class="sharethis-inline-share-buttons"></div>',
    ];
    $product_type = 'inline-share-buttons';
  }

  // Get the product ID configured in the block settings.
  $property_id = variable_get('sharethis_property_id', '');

  // Attach the javascript with the correct variables.
  drupal_add_js("//platform-api.sharethis.com/js/sharethis.js#property={$property_id}&product={$product_type}", [
    'type' => 'external',
    'scope' => 'footer',
  ]);

  // The sharethis theme needs to be used for this block to minimise markup.
  $build['#theme'] = 'block__sharethis';
  return $build;
}

Functions

Namesort descending Description
sharethis_block_block_configure Implements hook_block_configure().
sharethis_block_block_info Implements hook_block_info().
sharethis_block_block_save Implements hook_block_save().
sharethis_block_block_view Implements hook_block_view().
sharethis_block_output Returns a render array with the block output and library attached.
sharethis_block_theme Implements hook_theme().