View source
<?php
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Url;
define('SOCBUTT_LAYOUT_VERTICAL', 0);
define('SOCBUTT_LAYOUT_HORIZONTAL', 1);
function socbutt_token_info() {
$type = array(
'name' => t('Socbutt tokens'),
'description' => t("Socbutt tokens."),
);
$info['vertical'] = array(
'name' => t("Social button bar, vertical"),
'description' => t("Social button bar, vertical layout."),
'type' => 'socbutt',
);
$info['horizontal'] = array(
'name' => t("Social button bar, horizontal"),
'description' => t("Social button bar, horizontal layout."),
'type' => 'socbutt',
);
return array(
'types' => array(
'socbutt' => $type,
),
'tokens' => array(
'socbutt' => $info,
),
);
}
function socbutt_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = array();
if ($type == 'socbutt') {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'vertical':
$replacements[$original] = \Drupal::service('renderer')
->render(_socbutt_buttons_render(array(
'layout' => SOCBUTT_LAYOUT_VERTICAL,
)));
break;
case 'horizontal':
$replacements[$original] = \Drupal::service('renderer')
->render(_socbutt_buttons_render(array(
'layout' => SOCBUTT_LAYOUT_HORIZONTAL,
)));
break;
}
}
}
return $replacements;
}
function socbutt_theme() {
return array(
'socbutt-buttons' => array(
'variables' => array(
'layout' => SOCBUTT_LAYOUT_VERTICAL,
'title' => NULL,
'body' => NULL,
'url' => NULL,
),
),
);
}
function _socbutt_buttons_render($variables) {
global $base_url;
$horizontal = $variables['layout'] == SOCBUTT_LAYOUT_HORIZONTAL;
$tag = $horizontal ? 'span' : 'div';
$horizontal_class = $horizontal ? ' inline' : '';
$current_url = $base_url . \Drupal::service('path.current')
->getPath();
$request = \Drupal::request();
$route_match = \Drupal::routeMatch();
$title = \Drupal::service('title_resolver')
->getTitle($request, $route_match
->getRouteObject());
$share_data = array(
'title' => !is_null($variables['title']) ? $variables['title'] : $title,
'body' => !is_null($variables['body']) ? $variables['body'] : '',
'url' => !is_null($variables['url']) ? $variables['url'] : $current_url,
);
foreach ($share_data as $key => $data) {
$share_data[$key] = rawurlencode($data);
}
$title = $share_data['title'];
$body = $share_data['body'];
$url = $share_data['url'];
$btn_classes = array(
'btn',
'btn-info',
'btn-xs',
);
if (!$horizontal) {
$btn_classes[] = 'btn-block';
}
$render_array = array(
'#prefix' => '<div class="social-share-links' . $horizontal_class . '">',
'#suffix' => '</div>',
'#attached' => array(
'library' => array(
'socbutt/socbutt',
),
),
);
$link = "mailto:?subject={$title}&body={$body}";
$text = t('Email');
$url_obj = Url::fromUri($link);
$external_link = \Drupal::getContainer()
->get('link_generator')
->generate($text, $url_obj, array(
'html' => TRUE,
'attributes' => array(
'class' => $btn_classes,
),
));
$render_array['email'] = array(
'#prefix' => '<' . $tag . ' class="share-link share-email">',
'#suffix' => "</{$tag}>",
'#markup' => $external_link,
);
$link = "https://www.facebook.com/sharer/sharer.php?u={$url}";
$text = t('Facebook');
$url_obj = Url::fromUri($link);
$external_link = \Drupal::getContainer()
->get('link_generator')
->generate($text, $url_obj, array(
'html' => TRUE,
'attributes' => array(
'target' => '_blank',
'class' => $btn_classes,
),
));
$render_array['facebook'] = array(
'#prefix' => '<' . $tag . ' class="share-link share-facebook">',
'#suffix' => "</{$tag}>",
'#markup' => $external_link,
);
$twitter_msg = $title . ' -';
$link = "https://twitter.com/intent/tweet?text={$twitter_msg}&url={$url}";
$text = t('Twitter');
$url_obj = Url::fromUri($link);
$external_link = \Drupal::getContainer()
->get('link_generator')
->generate($text, $url_obj, array(
'html' => TRUE,
'attributes' => array(
'target' => '_blank',
'class' => $btn_classes,
),
));
$render_array['twitter'] = array(
'#prefix' => '<' . $tag . ' class="share-link share-twitter">',
'#suffix' => "</{$tag}>",
'#markup' => $external_link,
);
$link = "https://plus.google.com/share?url={$url}";
$text = t('Google Plus');
$url_obj = Url::fromUri($link);
$external_link = \Drupal::getContainer()
->get('link_generator')
->generate($text, $url_obj, array(
'html' => TRUE,
'attributes' => array(
'target' => '_blank',
'class' => $btn_classes,
),
));
$render_array['gplus'] = array(
'#prefix' => '<' . $tag . ' class="share-link share-gplus">',
'#suffix' => "</{$tag}>",
'#markup' => $external_link,
);
$link = "https://www.linkedin.com/shareArticle?mini=true&url={$url}&title={$title}&summary={$body}&source={$url}";
$text = t('LinkedIn');
$url_obj = Url::fromUri($link);
$external_link = \Drupal::getContainer()
->get('link_generator')
->generate($text, $url_obj, array(
'html' => TRUE,
'attributes' => array(
'target' => '_blank',
'class' => $btn_classes,
),
));
$render_array['linkedin'] = array(
'#prefix' => '<' . $tag . ' class="share-link share-linkedin">',
'#suffix' => "</{$tag}>",
'#markup' => $external_link,
);
return $render_array;
}