Skip to content

Instantly share code, notes, and snippets.

@isramv
Created February 5, 2016 18:50
Show Gist options
  • Save isramv/96403646d50d8da9f58e to your computer and use it in GitHub Desktop.
Save isramv/96403646d50d8da9f58e to your computer and use it in GitHub Desktop.
Simple social share icons for D8 twig.

Simple social share icons D8.

hook_preprocess().

// Social share icons.
  if($hook == 'node') {
    if($vars['node']->getType() == 'resource') {
      global $base_root;
      $vars['base_root'] = $base_root;
      //
      $site_name = \Drupal::config('system.site')->get('name');
      $vars['site_name'] = $site_name;
    }
  }

.html.twig

{% set node_title = node.getTitle() | url_encode %}
{% set social_url = base_root ~ url %}
{% set summary = content.body[0]['#text']|striptags %}
{% set summary_url = summary|length > 140 ? summary|url_encode|slice(0, 140) ~ '...' : summary  %}

<div class="container social">
  <div class="service-links">
    <a href="http://www.facebook.com/sharer.php?u={{ social_url }}&amp;t={{ node_title }}"
       title="Share on Facebook"
       class="service-links-facebook"
       rel="nofollow">Facebook</a>
    <a href="http://twitter.com/share?url={{ social_url }}&amp;text={{ node_title }}"
       title="Share this on Twitter"
       class="service-links-twitter"
       rel="nofollow">Twitter</a>
    <a href="http://www.linkedin.com/shareArticle?mini=true&amp;url={{ social_url }}&amp;title={{ node_title }}&amp;summary={{ summary_url }}&amp;source={{ site_name|url_encode }}"
       title="Publish this post to LinkedIn"
       class="service-links-linkedin"
       rel="nofollow">LinkedIn</a>
  </div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment