You are here

function theme_socbutt_buttons in Bootstrap Social Sharing Buttons 7

Theme function to render social buttons.

2 theme calls to theme_socbutt_buttons()
socbutt_block_view in ./socbutt.module
Implements hook_block_view().
socbutt_tokens in ./socbutt.module
Implements hook_tokens().

File

./socbutt.module, line 121

Code

function theme_socbutt_buttons($variables) {
  global $base_url;

  // Let other modules alter the buttons if needed.
  drupal_alter('socbutt_buttons_data', $variables);
  $path = drupal_get_path('module', 'socbutt');
  $horizontal = $variables['layout'] == SOCBUTT_LAYOUT_HORIZONTAL;
  $tag = $horizontal ? 'span' : 'div';
  $horizontal_class = $horizontal ? ' inline' : '';
  $current_url = $base_url . '/' . request_path();

  // Preparing default sharing data.
  $share_data = array(
    'title' => !is_null($variables['title']) ? $variables['title'] : drupal_get_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(
      'css' => array(
        $path . '/css/socbutt.min.css',
      ),
    ),
  );

  // Link for email.
  $link = "mailto:?subject={$title}&body={$body}";
  $text = '<span class="fa fa-fw fa-2x fa-envelope"></span> ' . t('Email');
  $render_array['email'] = array(
    '#prefix' => '<' . $tag . ' class="share-link share-email">',
    '#suffix' => "</{$tag}>",
    '#markup' => l($text, $link, array(
      'html' => TRUE,
      'attributes' => array(
        'class' => $btn_classes,
      ),
    )),
  );

  // Facebook share.
  $link = "https://www.facebook.com/sharer/sharer.php?u={$url}";
  $text = '<span class="fa fa-fw fa-2x fa-facebook"></span> ' . t('Facebook');
  $render_array['facebook'] = array(
    '#prefix' => '<' . $tag . ' class="share-link share-facebook">',
    '#suffix' => "</{$tag}>",
    '#markup' => l($text, $link, array(
      'html' => TRUE,
      'attributes' => array(
        'target' => '_blank',
        'class' => $btn_classes,
      ),
    )),
  );

  // Twitter share.
  $twitter_msg = $title . ' -';

  // Twitter adds the $url after this automatically.
  $link = "https://twitter.com/intent/tweet?text={$twitter_msg}&url={$url}";
  $text = '<span class="fa fa-fw fa-2x fa-twitter"></span> ' . t('Twitter');
  $render_array['twitter'] = array(
    '#prefix' => '<' . $tag . ' class="share-link share-twitter">',
    '#suffix' => "</{$tag}>",
    '#markup' => l($text, $link, array(
      'html' => TRUE,
      'attributes' => array(
        'target' => '_blank',
        'class' => $btn_classes,
      ),
    )),
  );

  // LinkedIn share.
  $link = "https://www.linkedin.com/shareArticle?mini=true&url={$url}&title={$title}&summary={$body}&source={$url}";
  $text = '<span class="fa fa-fw fa-2x fa-linkedin"></span> ' . t('LinkedIn');
  $render_array['linkedin'] = array(
    '#prefix' => '<' . $tag . ' class="share-link share-linkedin">',
    '#suffix' => "</{$tag}>",
    '#markup' => l($text, $link, array(
      'html' => TRUE,
      'attributes' => array(
        'target' => '_blank',
        'class' => $btn_classes,
      ),
    )),
  );

  // Let other modules alter the buttons if needed.
  drupal_alter('socbutt_buttons', $render_array, $variables);

  // Render.
  return drupal_render($render_array);
}