You are here

share42.module in Share42 - social sharing buttons 7.2

Same filename and directory in other branches
  1. 6 share42.module
  2. 7 share42.module

Main file for the Share42 module.

File

share42.module
View source
<?php

/**
 * @file
 * Main file for the Share42 module.
 */

/**
 * Implements hook_help().
 */
function share42_help($path, $arg) {
  switch ($path) {
    case 'admin/help#share42':
      $output = '';
      $output .= '<h3>' . t('Introduction') . '</h3>';
      $output .= '<p>' . t('Integration of social sharing buttons script from http://share42.com.') . '</p>';
      $output .= '<h3>' . t('Installation') . '</h3>';
      $output .= '<p>' . t('Configure required services at <a href="!site">http://share42.com</a> and then download and unpack into "sites/all/libraries/share42" folder.', array(
        '!site' => 'http://share42.com',
      )) . '</p>';
      return $output;
  }
}

/**
 * Implements hook_permission().
 */
function share42_permission() {
  return array(
    'administer share42' => array(
      'title' => t('Administer Share42'),
      'description' => t('Perform administration tasks for Share42 module.'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function share42_menu() {
  $items['admin/config/services/share42'] = array(
    'title' => 'Share42',
    'description' => 'Configure Share42 widget.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'share42_admin_settings',
    ),
    'access arguments' => array(
      'administer share42',
    ),
    'file' => 'share42.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_block_info().
 */
function share42_block_info() {
  $blocks['share42'] = array(
    'info' => t('Share42 - social sharing buttons block'),
    'cache' => DRUPAL_CACHE_GLOBAL,
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function share42_block_view($delta = '') {
  $block = array();
  if ($delta == 'share42') {
    $block['subject'] = t('Share42');
    $block['content'] = array(
      'share42' => array(
        '#type' => 'share42_widget',
        '#id' => 'share42-block',
      ),
    );
  }
  return $block;
}

/**
 * Implements hook_field_extra_fields().
 */
function share42_field_extra_fields($get_defaults = FALSE) {
  $defaults = array(
    'label' => t('Share42'),
    'description' => t('Share42 widget.'),
    'weight' => 100,
    'settings' => array(
      'image_field' => 'field_image',
      'image_style' => 'thumbnail',
      'description' => '',
    ),
  );
  if ($get_defaults) {
    return array(
      'extra_fields' => array(
        'display' => array(
          'share42' => $defaults,
        ),
      ),
    );
  }
  $extra = array();
  foreach (entity_get_info() as $entity_type => $entity_info) {
    foreach (array_keys($entity_info['bundles']) as $bundle) {
      $extra[$entity_type][$bundle]['display']['share42'] = $defaults;

      // Hide it by default (if there is no info about our extra field in field bundle settings).
      $field_bundle_setting = field_bundle_settings($entity_type, $bundle);
      if (empty($field_bundle_setting['extra_fields']['display']['share42'])) {

        // Prepare all entity type view modes.
        $view_modes = array_keys($entity_info['view modes']);
        if (!in_array('default', $view_modes)) {
          $view_modes[] = 'default';
        }
        foreach ($view_modes as $view_mode) {
          $field_bundle_setting['extra_fields']['display']['share42'][$view_mode] = array(
            'visible' => FALSE,
            'weight' => $defaults['weight'],
          );
        }
        field_bundle_settings($entity_type, $bundle, $field_bundle_setting);
      }
    }
  }
  return $extra;
}

/**
 * Implements hook_entity_view().
 */
function share42_entity_view($entity, $type, $view_mode, $langcode) {
  $entity->content['share42'] = _share42_widget_view($entity, $type);

  // Get entity bundle and load settings.
  list(, , $bundle) = entity_extract_ids($type, $entity);
  $field_bundle_setting = field_bundle_settings($type, $bundle);

  // Determine the view_mode settings to use.
  $effective_view_mode = 'default';
  if (isset($field_bundle_setting['view_modes'][$view_mode]) && $field_bundle_setting['view_modes'][$view_mode]['custom_settings']) {
    $effective_view_mode = $view_mode;
  }
  if (isset($field_bundle_setting['extra_fields']['display']['share42'][$effective_view_mode]['settings'])) {
    $settings = $field_bundle_setting['extra_fields']['display']['share42'][$effective_view_mode]['settings'];

    // Add image to share according to settings.
    if ($items = field_get_items($type, $entity, $settings['image_field'])) {
      if ($settings['image_style']) {
        $entity->content['share42']['#image'] = image_style_url($settings['image_style'], $items[0]['uri']);
      }
      else {
        $entity->content['share42']['#image'] = file_create_url($items[0]['uri']);
      }
    }

    // Add description.
    if (isset($settings['description'])) {
      $entity->content['share42']['#description'] = token_replace($settings['description'], array(
        $type => $entity,
      ));
    }
  }
}

/**
 * Helper function to return Share42 widget for the entity.
 */
function _share42_widget_view($entity, $entity_type) {
  $widget = array(
    '#type' => 'share42_widget',
  );
  if ($label = entity_label($entity_type, $entity)) {
    $widget['#title'] = $label;
  }
  if ($uri = entity_uri($entity_type, $entity)) {
    if (isset($uri['path'])) {
      $uri['options']['absolute'] = TRUE;
      $widget['#url'] = url($uri['path'], $uri['options']);
    }
  }
  return $widget;
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function share42_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {

  // I didn't really check what the code below does - it just works; see smart_paging module.
  // Modified part copy from field_ui_display_overview_form() at /modules/field_ui/field_ui.admin.inc
  // Base button element for the various formatter settings actions.
  $base_button = array(
    '#submit' => array(
      'field_ui_display_overview_multistep_submit',
    ),
    '#ajax' => array(
      'callback' => 'field_ui_display_overview_multistep_js',
      'wrapper' => 'field-display-overview-wrapper',
      'effect' => 'fade',
    ),
    '#field_name' => 'share42',
  );
  $summary = isset($form_state['formatter_settings']['share42']) ? $form_state['formatter_settings']['share42'] : NULL;
  if ($form_state['formatter_settings_edit'] == 'share42') {
    $form['fields']['share42']['#region_callback'] = 'field_ui_display_overview_row_region';
    $form['fields']['share42']['format']['settings_edit_form'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'field-formatter-settings-edit-form',
        ),
      ),
      '#parents' => array(
        'fields',
        'share42',
        'settings_edit_form',
      ),
      'label' => array(
        '#markup' => t('Format settings:') . ' <span class="formatter-name">' . t('Share42') . '</span>',
      ),
      'settings' => _share42_field_formatter_settings_form($form, $summary),
      'actions' => array(
        '#type' => 'actions',
        'save_settings' => $base_button + array(
          '#type' => 'submit',
          '#name' => 'share42_formatter_settings_update',
          '#value' => t('Update'),
          '#op' => 'update',
        ),
        'cancel_settings' => $base_button + array(
          '#type' => 'submit',
          '#name' => 'share42_formatter_settings_cancel',
          '#value' => t('Cancel'),
          '#op' => 'cancel',
          // Do not check errors for the 'Cancel' button, but make sure we
          // get the value of the 'formatter type' select.
          '#limit_validation_errors' => array(
            array(
              'fields',
              'share42',
              'type',
            ),
          ),
        ),
      ),
    );
    $form['fields']['share42']['#attributes']['class'][] = 'field-formatter-settings-editing';
  }
  elseif (isset($form['#entity_type'])) {
    $form['fields']['share42']['#region_callback'] = 'field_ui_display_overview_row_region';
    $form['fields']['share42']['settings_summary'] = array(
      '#markup' => '<div class="field-formatter-summary">' . _share42_field_formatter_settings_summary($form, $summary) . '</div>',
      '#cell_attributes' => array(
        'class' => array(
          'field-formatter-summary-cell',
        ),
      ),
    );
    $form['fields']['share42']['settings_edit'] = $base_button + array(
      '#type' => 'image_button',
      '#name' => 'share42_formatter_settings_edit',
      '#src' => 'misc/configure.png',
      '#attributes' => array(
        'class' => array(
          'field-formatter-settings-edit',
        ),
        'alt' => t('Edit'),
      ),
      '#op' => 'edit',
      // Do not check errors for the 'Edit' button, but make sure we get
      // the value of the 'formatter type' select.
      '#limit_validation_errors' => array(
        array(
          'fields',
          'share42',
          'type',
        ),
      ),
      '#prefix' => '<div class="field-formatter-settings-edit-wrapper">',
      '#suffix' => '</div>',
    );
  }
  if (!isset($form_state['formatter_settings']['share42']) || isset($form_state['formatter_settings']['share42']) && empty($form_state['formatter_settings']['share42'])) {
    $bundle_settings = field_bundle_settings($form['#entity_type'], $form['#bundle']);
    if (isset($bundle_settings['extra_fields']['display']['share42'][$form['#view_mode']]['settings'])) {
      $form_state['formatter_settings']['share42'] = $bundle_settings['extra_fields']['display']['share42'][$form['#view_mode']]['settings'];
    }
    else {
      $display_defaults = share42_field_extra_fields(TRUE);
      $form_state['formatter_settings']['share42'] = $display_defaults['extra_fields']['display']['share42']['settings'];
    }
  }
  $form['#submit'][] = '_share42_display_overview_form_submit';
}

/**
 * Share42 field_ui_display_overview_form submit handler.
 */
function _share42_display_overview_form_submit($form, &$form_state) {

  // Get current bundle settings.
  $bundle_settings = field_bundle_settings($form['#entity_type'], $form['#bundle']);
  $bundle_settings['extra_fields']['display']['share42'][$form['#view_mode']]['settings'] = $form_state['formatter_settings']['share42'];

  // Save updated bundle settings.
  field_bundle_settings($form['#entity_type'], $form['#bundle'], $bundle_settings);
}

/**
 * Pseudo hook_field_formatter_settings_form().
 */
function _share42_field_formatter_settings_form($build, $summary = NULL) {
  $display_stored = field_bundle_settings($build['#entity_type'], $build['#bundle']);
  $display_defaults = share42_field_extra_fields(TRUE);
  if (!empty($summary)) {
    $settings = $summary;
  }
  elseif (isset($display_stored['extra_fields']['display']['share42'][$build['#view_mode']])) {
    $settings = $display_stored['extra_fields']['display']['share42'][$build['#view_mode']]['settings'];
  }
  else {
    $settings = $display_defaults['extra_fields']['display']['share42']['settings'];
  }
  $options = array();
  $field_instances = field_info_instances($build['#entity_type'], $build['#bundle']);
  foreach ($field_instances as $field_name => $field) {
    $field_info = field_info_field($field_name);
    if (in_array($field_info['type'], array(
      'image',
    ))) {
      $options[$field_name] = $field_instances[$field_name]['label'];
    }
  }
  $form['image_field'] = array(
    '#type' => 'select',
    '#title' => t('Image field'),
    '#description' => t('Choose image field to use as image to share.'),
    '#default_value' => $settings['image_field'],
    '#empty_option' => t('None'),
    '#options' => $options,
  );
  $options = image_style_options(FALSE);
  $form['image_style'] = array(
    '#type' => 'select',
    '#title' => t('Image style'),
    '#default_value' => $settings['image_style'],
    '#empty_option' => t('None (original image)'),
    '#options' => $options,
    '#states' => array(
      'invisible' => array(
        ':input[name="fields[share42][settings_edit_form][settings][image_field]"]' => array(
          'value' => '',
        ),
      ),
    ),
  );
  $form['description'] = array(
    '#type' => 'textfield',
    '#title' => t('Description'),
    '#description' => t('Text to share. This field supports tokens.'),
    '#default_value' => $settings['description'],
  );
  if (module_exists('token')) {
    $form['token_tree'] = array(
      '#theme' => 'token_tree',
      '#token_types' => array(
        $build['#entity_type'],
      ),
      '#global_types' => FALSE,
      '#dialog' => TRUE,
    );
  }
  return $form;
}

/**
 * Pseudo hook_field_formatter_settings_summary().
 */
function _share42_field_formatter_settings_summary($build, $summary = NULL) {
  $display_stored = field_bundle_settings($build['#entity_type'], $build['#bundle']);
  $display_defaults = share42_field_extra_fields(TRUE);
  if (!empty($summary)) {
    $settings = $summary;
  }
  elseif (isset($display_stored['extra_fields']['display']['share42'][$build['#view_mode']]['settings'])) {
    $settings = $display_stored['extra_fields']['display']['share42'][$build['#view_mode']]['settings'];
  }
  else {
    $settings = $display_defaults['extra_fields']['display']['share42']['settings'];
  }
  $summary = array();
  $field_instances = field_info_instances($build['#entity_type'], $build['#bundle']);
  if (isset($field_instances[$settings['image_field']])) {
    $summary[] = t('Image field to share: @field', array(
      '@field' => $field_instances[$settings['image_field']]['label'],
    ));
    $image_styles = image_style_options(FALSE);

    // Unset possible 'No defined styles' option.
    unset($image_styles['']);

    // Styles could be lost because of enabled/disabled modules that define
    // their styles in code.
    if (isset($image_styles[$settings['image_style']])) {
      $summary[] = t('Image style: @style', array(
        '@style' => $image_styles[$settings['image_style']],
      ));
    }
    else {
      $summary[] = t('Image style: @style', array(
        '@style' => t('Original image'),
      ));
    }
  }
  else {
    $summary[] = t('Image field to share: @field', array(
      '@field' => t('None'),
    ));
  }
  if (isset($settings['description'])) {
    $summary[] = t('Text to share: @description', array(
      '@description' => $settings['description'],
    ));
  }
  return implode('<br />', $summary);
}

/**
 * Implements hook_element_info().
 */
function share42_element_info() {
  $types = array();
  $types['share42_widget'] = array(
    '#input' => FALSE,
    '#theme' => 'share42_widget',
    '#pre_render' => array(
      'share42_widget_pre_render',
    ),
    '#html_tag' => 'div',
    // General parameters.
    '#id' => 'share42-widget',
    // Link parameters.
    '#url' => NULL,
    '#title' => NULL,
    '#image' => NULL,
    '#description' => NULL,
    // Configuration parameters.
    '#path' => base_path() . _share42_library_path() . '/',
    '#icons_file' => 'icons.png',
    '#zero_counter' => variable_get('share42_zero_counter', 0),
    '#top1' => variable_get('share42_top1', 200),
    '#top2' => variable_get('share42_top2', 50),
    '#margin' => variable_get('share42_margin', -70),
    '#attached' => array(
      'js' => array(
        _share42_library_path() . '/share42.js' => array(
          'weight' => 10,
        ),
      ),
    ),
  );
  return $types;
}

/**
 * Pre-render callback for the 'share42_widget' element.
 */
function share42_widget_pre_render($element) {
  $path = drupal_get_path('module', 'share42');

  // Adding REQUEST_TIME to the id to make it generally unique.
  $element['#id'] = drupal_html_id($element['#id'] . '-' . REQUEST_TIME);
  return $element;
}

/**
 * Implements hook_theme().
 */
function share42_theme($existing, $type, $theme, $path) {
  return array(
    'share42_widget' => array(
      'render element' => 'element',
      'template' => 'templates/share42_widget',
    ),
  );
}

/**
 * Returns the path to the Share42 library.
 */
function _share42_library_path() {
  $library_path =& drupal_static(__FUNCTION__, NULL);
  if (is_null($library_path)) {
    $library_path = variable_get('share42_library_path', module_exists('libraries') ? libraries_get_path('share42') : 'sites/all/libraries/share42');
    if (!file_exists($library_path . '/share42.js')) {
      watchdog('share42', 'Share42 library is missing.', array(), WATCHDOG_ERROR);
      $library_path = FALSE;
    }
  }
  return $library_path;
}

/**
 * Returns version of the Share42 library.
 */
function _share42_library_version() {
  $version =& drupal_static(__FUNCTION__, NULL);
  if (is_null($version) && ($library_path = _share42_library_path())) {
    $pattern = '/share42.com \\| ([0-9\\.]+)/';
    $share42_js = file_get_contents($library_path . '/share42.js', NULL, NULL, 0, 32);
    if (preg_match($pattern, $share42_js, $matches)) {
      $version = $matches[1];
    }
    else {
      $version = 'Unknown';
    }
  }
  return $version;
}

Functions

Namesort descending Description
share42_block_info Implements hook_block_info().
share42_block_view Implements hook_block_view().
share42_element_info Implements hook_element_info().
share42_entity_view Implements hook_entity_view().
share42_field_extra_fields Implements hook_field_extra_fields().
share42_form_field_ui_display_overview_form_alter Implements hook_form_FORM_ID_alter().
share42_help Implements hook_help().
share42_menu Implements hook_menu().
share42_permission Implements hook_permission().
share42_theme Implements hook_theme().
share42_widget_pre_render Pre-render callback for the 'share42_widget' element.
_share42_display_overview_form_submit Share42 field_ui_display_overview_form submit handler.
_share42_field_formatter_settings_form Pseudo hook_field_formatter_settings_form().
_share42_field_formatter_settings_summary Pseudo hook_field_formatter_settings_summary().
_share42_library_path Returns the path to the Share42 library.
_share42_library_version Returns version of the Share42 library.
_share42_widget_view Helper function to return Share42 widget for the entity.