View source
<?php
include dirname(__FILE__) . '/yashare.inc';
function yashare_help($path, $arg) {
switch ($path) {
case 'admin/help#yashare':
return _filter_autop(file_get_contents(dirname(__FILE__) . '/README.txt'));
break;
}
}
function yashare_permission() {
return array(
'administer yashare' => array(
'title' => t('Administer Yandex.Share'),
'description' => t('Perform administration tasks for Yandex.Share module.'),
),
);
}
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;
}
function yashare_block_info() {
$blocks['yashare'] = array(
'info' => t('Yandex.Share'),
'cache' => DRUPAL_CACHE_GLOBAL,
);
return $blocks;
}
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;
}
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;
$field_bundle_setting = field_bundle_settings($entity_type, $bundle);
if (empty($field_bundle_setting['extra_fields']['display']['yashare'])) {
$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;
}
function yashare_entity_view($entity, $type, $view_mode, $langcode) {
$entity->content['yashare'] = _yashare_widget_view($entity, $type);
list(, , $bundle) = entity_extract_ids($type, $entity);
$field_bundle_setting = field_bundle_settings($type, $bundle);
$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'];
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']);
}
}
if (isset($settings['description'])) {
$entity->content['yashare']['#description'] = token_replace($settings['description'], array(
$type => $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;
}
function yashare_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {
$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',
'#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',
'#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';
}
function _yashare_display_overview_form_submit($form, &$form_state) {
$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'];
field_bundle_settings($form['#entity_type'], $form['#bundle'], $bundle_settings);
}
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;
}
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($image_styles['']);
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);
}
function yashare_element_info() {
$types = array();
$types['yashare_widget'] = array(
'#input' => FALSE,
'#pre_render' => array(
'yashare_widget_pre_render',
),
'#html_tag' => 'div',
'#id' => 'yashare-widget',
'#l10n' => yashare_get_language(TRUE),
'#init_type' => variable_get('yashare_init_type', 'automatic'),
'#yashare_theme' => variable_get('yashare_theme', 'default'),
'#link' => NULL,
'#title' => NULL,
'#description' => NULL,
'#image' => variable_get('yashare_image', FALSE) ? theme_get_setting('logo') : NULL,
'#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_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;
}
function yashare_widget_pre_render($element) {
$path = drupal_get_path('module', 'yashare');
$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,
);
if ($element['#init_type'] == 'automatic') {
$element['#attached']['js'][$path . '/js/yashare.automatic.js'] = array(
'weight' => 10,
);
return $element;
}
$params = array(
'element' => $element['#id'],
'l10n' => $element['#l10n'],
'theme' => $element['#yashare_theme'],
);
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'];
}
$params['elementStyle'] = array(
'text' => $element['#block_title'],
'type' => $element['#yashare_type'],
'border' => $element['#border'],
'linkUnderline' => $element['#linkunderline'],
'linkIcon' => $element['#linkicon'],
'quickServices' => $element['#quickservices'],
);
$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;
}
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',
),
);
}
function yashare_views_api() {
return array(
'api' => '3.0',
'path' => drupal_get_path('module', 'yashare') . '/views',
);
}