You are here

yashare.module in Yandex.Share 7.2

Same filename and directory in other branches
  1. 7 yashare.module

File

yashare.module
View source
<?php

/**
 * @file
 * Main file for the Yandex.Share module.
 */
include dirname(__FILE__) . '/yashare.inc';

/**
 * Implements hook_help().
 */
function yashare_help($path, $arg) {
  switch ($path) {
    case 'admin/help#yashare':

      // Return a line-break version of the module README.txt
      return _filter_autop(file_get_contents(dirname(__FILE__) . '/README.txt'));
      break;
  }
}

/**
 * Implements hook_permission().
 */
function yashare_permission() {
  return array(
    'administer yashare' => array(
      'title' => t('Administer Yandex.Share'),
      'description' => t('Perform administration tasks for Yandex.Share module.'),
    ),
  );
}

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

/**
 * Implements hook_block_info().
 */
function yashare_block_info() {
  $blocks['yashare'] = array(
    'info' => t('Yandex.Share'),
    'cache' => DRUPAL_CACHE_GLOBAL,
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function yashare_block_view($delta = '') {
  $block = array();
  if ($delta == 'yashare') {
    $block['subject'] = t('Yandex.Share');
    $block['content'] = array(
      'yashare' => array(
        '#type' => 'yashare_widget',
        '#id' => 'yashare-block',
      ),
    );
  }
  return $block;
}

/**
 * Implements hook_field_extra_fields().
 */
function yashare_field_extra_fields($get_defaults = FALSE) {
  $defaults = array(
    'label' => t('Yandex.Share'),
    'description' => t('Yandex.Share widget.'),
    'weight' => 100,
    'settings' => array(
      'image_field' => 'field_image',
      'image_style' => 'thumbnail',
      'description' => '',
    ),
  );
  if ($get_defaults) {
    return array(
      'extra_fields' => array(
        'display' => array(
          'yashare' => $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']['yashare'] = $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']['yashare'])) {

        // 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']['yashare'][$view_mode] = array(
            'visible' => FALSE,
            'weight' => $defaults['weight'],
          );
        }
        field_bundle_settings($entity_type, $bundle, $field_bundle_setting);
      }
    }
  }
  return $extra;
}

/**
 * Implements hook_entity_view().
 */
function yashare_entity_view($entity, $type, $view_mode, $langcode) {
  $entity->content['yashare'] = _yashare_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']['yashare'][$effective_view_mode]['settings'])) {
    $settings = $field_bundle_setting['extra_fields']['display']['yashare'][$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['yashare']['#image'] = image_style_url($settings['image_style'], $items[0]['uri']);
      }
      else {
        $entity->content['yashare']['#image'] = file_create_url($items[0]['uri']);
      }
    }

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

/**
 * Helper function to return Yandex.Share widget for the entity.
 */
function _yashare_widget_view($entity, $entity_type) {
  $widget = array(
    '#type' => 'yashare_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['#link'] = url($uri['path'], $uri['options']);
    }
  }
  return $widget;
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function yashare_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' => 'yashare',
  );
  $summary = isset($form_state['formatter_settings']['yashare']) ? $form_state['formatter_settings']['yashare'] : NULL;
  if ($form_state['formatter_settings_edit'] == 'yashare') {
    $form['fields']['yashare']['#region_callback'] = 'field_ui_display_overview_row_region';
    $form['fields']['yashare']['format']['settings_edit_form'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'field-formatter-settings-edit-form',
        ),
      ),
      '#parents' => array(
        'fields',
        'yashare',
        'settings_edit_form',
      ),
      'label' => array(
        '#markup' => t('Format settings:') . ' <span class="formatter-name">' . t('Yandex.Share') . '</span>',
      ),
      'settings' => _yashare_field_formatter_settings_form($form, $summary),
      'actions' => array(
        '#type' => 'actions',
        'save_settings' => $base_button + array(
          '#type' => 'submit',
          '#name' => 'yashare_formatter_settings_update',
          '#value' => t('Update'),
          '#op' => 'update',
        ),
        'cancel_settings' => $base_button + array(
          '#type' => 'submit',
          '#name' => 'yashare_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',
              'yashare',
              'type',
            ),
          ),
        ),
      ),
    );
    $form['fields']['yashare']['#attributes']['class'][] = 'field-formatter-settings-editing';
  }
  elseif (isset($form['#entity_type'])) {
    $form['fields']['yashare']['#region_callback'] = 'field_ui_display_overview_row_region';
    $form['fields']['yashare']['settings_summary'] = array(
      '#markup' => '<div class="field-formatter-summary">' . _yashare_field_formatter_settings_summary($form, $summary) . '</div>',
      '#cell_attributes' => array(
        'class' => array(
          'field-formatter-summary-cell',
        ),
      ),
    );
    $form['fields']['yashare']['settings_edit'] = $base_button + array(
      '#type' => 'image_button',
      '#name' => 'yashare_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',
          'yashare',
          'type',
        ),
      ),
      '#prefix' => '<div class="field-formatter-settings-edit-wrapper">',
      '#suffix' => '</div>',
    );
  }
  if (!isset($form_state['formatter_settings']['yashare']) || isset($form_state['formatter_settings']['yashare']) && empty($form_state['formatter_settings']['yashare'])) {
    $bundle_settings = field_bundle_settings($form['#entity_type'], $form['#bundle']);
    if (isset($bundle_settings['extra_fields']['display']['yashare'][$form['#view_mode']]['settings'])) {
      $form_state['formatter_settings']['yashare'] = $bundle_settings['extra_fields']['display']['yashare'][$form['#view_mode']]['settings'];
    }
    else {
      $display_defaults = yashare_field_extra_fields(TRUE);
      $form_state['formatter_settings']['yashare'] = $display_defaults['extra_fields']['display']['yashare']['settings'];
    }
  }
  $form['#submit'][] = '_yashare_display_overview_form_submit';
}

/**
 * Yandex.Share field_ui_display_overview_form submit handler.
 */
function _yashare_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']['yashare'][$form['#view_mode']]['settings'] = $form_state['formatter_settings']['yashare'];

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

/**
 * Pseudo hook_field_formatter_settings_form().
 */
function _yashare_field_formatter_settings_form($build, $summary = NULL) {
  $display_stored = field_bundle_settings($build['#entity_type'], $build['#bundle']);
  $display_defaults = yashare_field_extra_fields(TRUE);
  if (!empty($summary)) {
    $settings = $summary;
  }
  elseif (isset($display_stored['extra_fields']['display']['yashare'][$build['#view_mode']])) {
    $settings = $display_stored['extra_fields']['display']['yashare'][$build['#view_mode']]['settings'];
  }
  else {
    $settings = $display_defaults['extra_fields']['display']['yashare']['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[yashare][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 _yashare_field_formatter_settings_summary($build, $summary = NULL) {
  $display_stored = field_bundle_settings($build['#entity_type'], $build['#bundle']);
  $display_defaults = yashare_field_extra_fields(TRUE);
  if (!empty($summary)) {
    $settings = $summary;
  }
  elseif (isset($display_stored['extra_fields']['display']['yashare'][$build['#view_mode']]['settings'])) {
    $settings = $display_stored['extra_fields']['display']['yashare'][$build['#view_mode']]['settings'];
  }
  else {
    $settings = $display_defaults['extra_fields']['display']['yashare']['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 yashare_element_info() {
  $types = array();
  $types['yashare_widget'] = array(
    '#input' => FALSE,
    '#pre_render' => array(
      'yashare_widget_pre_render',
    ),
    '#html_tag' => 'div',
    // General parameters.
    '#id' => 'yashare-widget',
    '#l10n' => yashare_get_language(TRUE),
    '#init_type' => variable_get('yashare_init_type', 'automatic'),
    '#yashare_theme' => variable_get('yashare_theme', 'default'),
    // Link parameters.
    '#link' => NULL,
    '#title' => NULL,
    '#description' => NULL,
    '#image' => variable_get('yashare_image', FALSE) ? theme_get_setting('logo') : NULL,
    // Block parameters.
    '#block_title' => check_plain(variable_get('yashare_block_title', '')),
    '#yashare_type' => variable_get('yashare_type', 'button'),
    '#border' => variable_get('yashare_border', FALSE),
    '#linkunderline' => variable_get('yashare_linkunderline', FALSE),
    '#linkicon' => variable_get('yashare_linkicon', FALSE),
    '#quickservices' => NULL,
    // Popup parameters.
    '#popup_title' => check_plain(variable_get('yashare_popup_title', '')),
    '#popup_services' => NULL,
    '#copypastefield' => variable_get('yashare_popup_link', FALSE),
    '#vdirection' => variable_get('yashare_popup_vdirection', 0),
    '#codeforblog' => variable_get('yashare_popup_codeforblog', FALSE),
  );
  return $types;
}

/**
 * Pre-render callback for the 'yashare_widget' element.
 * See http://api.yandex.ru/share/doc/dg/concepts/share-button-ov.xml
 */
function yashare_widget_pre_render($element) {
  $path = drupal_get_path('module', 'yashare');

  // Adding REQUEST_TIME to the id to make it generally unique.
  // @see https://www.drupal.org/node/2472511
  $element['#id'] = drupal_html_id($element['#id'] . '-' . REQUEST_TIME);
  $element['#theme'] = 'yashare_widget_' . $element['#init_type'];
  if (!isset($element['#quickservices'])) {
    $element['#quickservices'] = array_values(yashare_get_enabled_services('block', $element['#yashare_theme']));
  }
  if (!isset($element['#popup_services'])) {
    $element['#popup_services'] = array_values(yashare_get_enabled_services('popup', $element['#yashare_theme']));
  }
  $element['#attached']['js'][$path . '/js/yashare.async.js'] = array(
    'weight' => 0,
  );

  // For the automatic initialization type we need just one JS file.
  if ($element['#init_type'] == 'automatic') {
    $element['#attached']['js'][$path . '/js/yashare.automatic.js'] = array(
      'weight' => 10,
    );
    return $element;
  }

  // General parameters.
  $params = array(
    'element' => $element['#id'],
    'l10n' => $element['#l10n'],
    'theme' => $element['#yashare_theme'],
  );

  // Link parameters.
  if ($element['#link']) {
    $params['link'] = $element['#link'];
  }
  if ($element['#title']) {
    $params['title'] = check_plain($element['#title'] . ' | ' . variable_get('site_name', 'Drupal'));
  }
  if ($element['#description']) {
    $params['description'] = check_plain($element['#description']);
  }
  if ($element['#image']) {
    $params['image'] = $element['#image'];
  }

  // Block parameters.
  $params['elementStyle'] = array(
    'text' => $element['#block_title'],
    'type' => $element['#yashare_type'],
    'border' => $element['#border'],
    'linkUnderline' => $element['#linkunderline'],
    'linkIcon' => $element['#linkicon'],
    'quickServices' => $element['#quickservices'],
  );

  // Popup parameters.
  $params['popupStyle'] = array(
    'blocks' => $element['#popup_title'] ? array(
      $element['#popup_title'] => $element['#popup_services'],
    ) : $element['#popup_services'],
    'copyPasteField' => $element['#copypastefield'],
  );
  if ($element['#vdirection']) {
    $params['popupStyle']['vDirection'] = $element['#vdirection'];
  }
  if ($element['#codeforblog'] && isset($params['title']) && isset($params['link'])) {
    $params['popupStyle']['codeForBlog'] = l($params['title'], $params['link'], array(
      'attributes' => array(
        'title' => $params['title'],
        'target' => '_blank',
      ),
    ));
  }
  $element['#attached']['js'][$path . '/js/yashare.standard.js'] = array(
    'weight' => 10,
  );
  $element['#attached']['js'][] = array(
    'data' => array(
      'yashare' => array(
        $element['#id'] => $params,
      ),
    ),
    'type' => 'setting',
  );
  return $element;
}

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

/**
 * Implements hook_views_api().
 */
function yashare_views_api() {
  return array(
    'api' => '3.0',
    'path' => drupal_get_path('module', 'yashare') . '/views',
  );
}