Commit fccc1149 authored by Philippe Mouchel's avatar Philippe Mouchel
Browse files

Start sharer toolabr generation to render it

[refs #70413]
parent 6fe65511
......@@ -34,9 +34,11 @@ function sharer_toolbar_config_form($form, &$form_state) {
);
// Textfield to hold sharer icon class.
$icon_value = (isset($sharer_conf['icon_class'])) ? $sharer_conf['icon_class'] : $sharer['icon_class'];
$form['sharers'][$sid]['icon_class'] = array(
'#type' => 'textfield',
'#default_value' => (isset($sharer_conf['icon_class'])) ? $sharer_conf['icon_class'] : $sharer['icon_class'],
'#default_value' => $icon_value,
'#field_prefix' => '<i class="' . $icon_value . '"></i>',
);
// Invisible field, but contains sort info (weights).
......
......@@ -6,23 +6,48 @@
*/
/**
* List all available environments,
* List all available sharers by invoking:
* - hook_sharer_toolbar_available_sharer()
* - hook_sharer_toolbar_available_sharer_alter()
*/
function _sharer_toolbar_get_available_sharers() {
$available_sharers = module_invoke_all('sharer_toolbar_available_sharer');
drupal_alter('sharer_toolbar_available_sharer', $available_sharers);
// And return them
return $available_sharers;
}
/**
* List all available sharers,
* ordered by config form weight if available
*/
function _sharer_toolbar_get_ordered_available_sharers() {
$ordered_sharers = array();
// List all available sharers by invoking:
// - hook_sharer_toolbar_available_sharer()
// - hook_sharer_toolbar_available_sharer_alter()
$available_sharers = module_invoke_all('sharer_toolbar_available_sharer');
drupal_alter('sharer_toolbar_available_sharer', $available_sharers);
// Get all available sharers
$available_sharers = _sharer_toolbar_get_available_sharers();
// Generate ordered list if sharers order is already defined
$sharers_order = variable_get('sharer_toolbar_sharers_order', array());
$existing_sharers = array();
if (!empty($sharers_order)) {
// List already ordered sharers
foreach ($sharers_order as $weight => $sid) {
$ordered_sharers[$weight] = $available_sharers[$sid];
if (isset($available_sharers[$sid])) {
$ordered_sharers[$weight] = $available_sharers[$sid];
$existing_sharers[] = $sid;
}
}
// Add new sharers
$weight++;
foreach ($available_sharers as $sid => $sharer) {
if (!in_array($sid, $existing_sharers)) {
$ordered_sharers[$weight] = $sharer;
$weight++;
}
}
}
......@@ -36,3 +61,26 @@ function _sharer_toolbar_get_ordered_available_sharers() {
ksort($ordered_sharers);
return $ordered_sharers;
}
/**
* Replace tokens with correct value in pattern URL
*/
function _sharer_toolbar_generate_sharer_url(&$sharer_url, $variables) {
global $base_url;
// URL replacement
$node_url = url('node/' . $variables['nid'], array(
'language' => $variables['language'],
));
$sharer_url = str_replace('[:shared_url:]', $base_url . $node_url, $sharer_url);
// Title replacement
$sharer_url = str_replace('[:shared_title:]', $variables['title'], $sharer_url);
// Source replacement
$sharer_url = str_replace('[:shared_source:]', variable_get('site_name', ''), $sharer_url);
// Summary replacement
$sharer_url = str_replace('[:shared_summary:]', '', $sharer_url);
}
......@@ -62,6 +62,7 @@ function sharer_toolbar_sharer_toolbar_available_sharer() {
'sid' => 'facebook',
'name' => 'Facebook',
'icon_class' => 'icon-facebook',
'url' => 'https://www.facebook.com/sharer/sharer.php?u=[:shared_url:]',
),
// Twitter sharer
......@@ -69,6 +70,15 @@ function sharer_toolbar_sharer_toolbar_available_sharer() {
'sid' => 'twitter',
'name' => 'Twitter',
'icon_class' => 'icon-twitter',
'url' => 'https://twitter.com/home?status=[:shared_message:]',
),
// Google Plus sharer
'googleplus' => array(
'sid' => 'googleplus',
'name' => 'Google +',
'icon_class' => 'icon-googleplus',
'url' => 'https://plus.google.com/share?url=[:shared_url:]',
),
// Twitter sharer
......@@ -76,13 +86,48 @@ function sharer_toolbar_sharer_toolbar_available_sharer() {
'sid' => 'linkedin',
'name' => 'LinkedIn',
'icon_class' => 'icon-linkedin',
'url' => 'https://www.linkedin.com/shareArticle?mini=true&url=[:shared_url:]&title=[:shared_title:]&summary=[:shared_summary:]&source=[:shared_source:]',
),
// Email sharer
'email' => array(
'sid' => 'email',
'name' => 'Email',
'icon_class' => 'icon-email',
),
// // Email sharer
// 'email' => array(
// 'sid' => 'email',
// 'name' => 'Email',
// 'icon_class' => 'icon-email',
// ),
);
}
/**
* Implements hook_preprocess_node().
*/
function sharer_toolbar_preprocess_node(&$variables) {
module_load_include('inc', 'sharer_toolbar');
$sharing_buttons = array();
// dd($variables);
// Get all active and available sharers
$active_sharers = variable_get('sharer_toolbar_sharers_configuration', array());
$available_sharers = _sharer_toolbar_get_available_sharers();
// Generate button if sharer is active
foreach ($active_sharers as $sid => &$sharer) {
if ($sharer['status']) {
// Merge database configuration in file configuration
$sharer = array_merge($available_sharers[$sid], $sharer);
// Transform URL
_sharer_toolbar_generate_sharer_url($sharer['url'], $variables);
// Generate button
$sharing_buttons[] = l('Partager sur ' . $sharer['name'], $sharer['url']);
}
}
// Render HTML for sharer toolbar
$variables['sharer_toolbar'] = theme('sharer_toolbar', array(
'sharing_buttons' => $sharing_buttons,
));
}
......@@ -20,3 +20,9 @@ msgstr "Configurer la barre de partage"
msgid "Sharer toolbar configuration has been saved."
msgstr "La configuration de la barre de partage a été sauvegardée."
msgid "Sharer toolbar configuration has been saved."
msgstr "La configuration de la barre de partage a été sauvegardée."
msgid "Icon class"
msgstr "Classe de l'icône"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment