You are here

function _socbutt_buttons_render in Bootstrap Social Sharing Buttons 8

Theme function to render social buttons.

3 calls to _socbutt_buttons_render()
SocbuttHorizont::build in src/Plugin/Block/SocbuttHorizont.php
Builds and returns the renderable array for this block plugin.
SocbuttVertical::build in src/Plugin/Block/SocbuttVertical.php
Builds and returns the renderable array for this block plugin.
socbutt_tokens in ./socbutt.module
Implements hook_tokens().

File

./socbutt.module, line 90
Allows integration with the Twitter microblogging service.

Code

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();

  // Preparing default sharing data.
  $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,
  );

  // URL encode so the strings can be used inside URLs.
  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';
  }

  // Prepare render array.
  $render_array = array(
    '#prefix' => '<div class="social-share-links' . $horizontal_class . '">',
    '#suffix' => '</div>',
    '#attached' => array(
      'library' => array(
        'socbutt/socbutt',
      ),
    ),
  );

  // Link for email.
  $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,
  );

  // Facebook share.
  $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 share.
  // Twitter adds the $url after this automatically.
  $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,
  );

  // Google Plus share:
  $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,
  );

  // LinkedIn share.
  $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,
  );

  // Render.
  return $render_array;
}