You are here

protected function ResponsiveShareButtonsBlock::prepareShareLink in Responsive Share Buttons 8

Prepare a sharing link.

Parameters

string $network: The name of the social network to use.

string $url: The URL to share.

string $title: The title to use.

Return value

string The link for sharing.

1 call to ResponsiveShareButtonsBlock::prepareShareLink()
ResponsiveShareButtonsBlock::build in src/Plugin/Block/ResponsiveShareButtonsBlock.php
Builds and returns the renderable array for this block plugin.

File

src/Plugin/Block/ResponsiveShareButtonsBlock.php, line 89

Class

ResponsiveShareButtonsBlock
Provides a 'Responsive Share buttons' block.

Namespace

Drupal\responsive_share_buttons\Plugin\Block

Code

protected function prepareShareLink($network, $url, $title) {
  $link = '';
  $link_options = [
    'attributes' => [
      'class' => [
        'button-wrap',
        $network,
      ],
    ],
  ];
  switch ($network) {
    case 'delicious':
      $full_url = Url::fromUri('http://del.icio.us/post?url=' . $url . '&title=' . $title);
      $full_url
        ->setOptions($link_options);
      $link = Link::fromTextAndUrl(t('Delicious'), $full_url);
      break;
    case 'digg':
      $full_url = Url::fromUri('http://www.digg.com/submit?phase=2&url=' . $url . '&title=' . $title);
      $full_url
        ->setOptions($link_options);
      $link = Link::fromTextAndUrl(t('Digg it'), $full_url);
      break;
    case 'facebook':
      $full_url = Url::fromUri('https://www.facebook.com/sharer/sharer.php?u=' . $url . '&title=' . $title);
      $full_url
        ->setOptions($link_options);
      $link = Link::fromTextAndUrl(t('Facebook'), $full_url);
      break;
    case 'google':
      $full_url = Url::fromUri('https://plus.google.com/share?url=' . $url . '&title=' . $title);
      $full_url
        ->setOptions($link_options);
      $link = Link::fromTextAndUrl(t('Plus Share'), $full_url);
      break;
    case 'linkedin':
      $full_url = Url::fromUri('http://www.linkedin.com/shareArticle?mini=true&url=' . $url . '&title=' . $title);
      $full_url
        ->setOptions($link_options);
      $link = Link::fromTextAndUrl(t('LinkedIn'), $full_url);
      break;
    case 'pinterest':
      $full_url = Url::fromUri('https://www.pinterest.com/pin/create/button/?url=' . $url . '&description=' . $title);
      $full_url
        ->setOptions($link_options);
      $link = Link::fromTextAndUrl(t('Pinterest'), $full_url);
      break;
    case 'stumbleupon':
      $full_url = Url::fromUri('http://www.stumbleupon.com/submit?url=' . $url . '&title=' . $title);
      $full_url
        ->setOptions($link_options);
      $link = Link::fromTextAndUrl(t('Stumbleupon'), $full_url);
      break;
    case 'twitter':
      $config = \Drupal::config('responsive_share_buttons.settings');
      $twitter_name = $config
        ->get('twitter_name');
      if (!empty($twitter_name)) {
        $title .= ' ' . t('via @@twitter_name', [
          '@twitter_name' => $twitter_name,
        ]);
      }
      $full_url = Url::fromUri('http://twitter.com/home?status=' . $title . ' ' . $url);
      $full_url
        ->setOptions($link_options);
      $link = Link::fromTextAndUrl(t('Tweet'), $full_url, [
        'attributes' => [
          'class' => [
            'button-wrap',
            $network,
          ],
        ],
      ]);
      break;
  }
  return $link;
}