You are here

class Twitter in Social simple 8

Same name and namespace in other branches
  1. 2.0.x src/SocialNetwork/Twitter.php \Drupal\social_simple\SocialNetwork\Twitter

The social network Twitter.

Hierarchy

Expanded class hierarchy of Twitter

2 string references to 'Twitter'
social_simple.services.yml in ./social_simple.services.yml
social_simple.services.yml
Twitter::getLabel in src/SocialNetwork/Twitter.php
Get the network name.
1 service uses Twitter
social_simple.twitter in ./social_simple.services.yml
Drupal\social_simple\SocialNetwork\Twitter

File

src/SocialNetwork/Twitter.php, line 15

Namespace

Drupal\social_simple\SocialNetwork
View source
class Twitter implements SocialNetworkInterface {
  use StringTranslationTrait;

  /**
   * The social network base share link.
   */
  const TWITTER_URL = 'https://twitter.com/intent/tweet/';

  /**
   * {@inheritdoc}
   */
  public function getId() {
    return 'twitter';
  }

  /**
   * {@inheritdoc}
   */
  public function getLabel() {
    return $this
      ->t('Twitter');
  }

  /**
   * {@inheritdoc}
   */
  public function getShareLink($share_url, $title = '', EntityInterface $entity = NULL, array $additional_options = []) {
    $options = [
      'query' => [
        'url' => $share_url,
        'text' => $title,
      ],
      'absolute' => TRUE,
      'external' => TRUE,
    ];

    // Get hashtags if set on the entity.
    if ($entity instanceof ContentEntityInterface) {
      $additional_options += $this
        ->getHashtags($entity);
    }
    if ($additional_options) {
      foreach ($additional_options as $id => $value) {
        $options['query'][$id] = $value;
      }
    }
    $url = Url::fromUri(self::TWITTER_URL, $options);
    $link = [
      'url' => $url,
      'title' => [
        '#markup' => '<i class="fa fa-twitter"></i><span class="visually-hidden">' . $this
          ->getLabel() . '</span>',
      ],
      'attributes' => $this
        ->getLinkAttributes($this
        ->getLabel()),
    ];
    return $link;
  }

  /**
   * {@inheritdoc}
   */
  public function getLinkAttributes($network_name) {
    $attributes = [
      'data-popup-width' => 600,
      'data-popup-height' => 300,
      'data-toggle' => 'tooltip',
      'data-placement' => 'top',
      'title' => $network_name,
    ];
    return $attributes;
  }

  /**
   * {@inheritdoc}
   */
  public function getHashtags(ContentEntityInterface $entity) {
    $options = [];
    if (!$entity instanceof NodeInterface) {
      return $options;
    }

    /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity_type */
    $entity_type = $entity->type->entity;
    if (!$entity_type instanceof ConfigEntityInterface) {
      return $options;
    }
    $key_value = $entity_type
      ->getThirdPartySetting('social_simple', 'hashtags', '');
    if (!empty($key_value) && $entity
      ->hasField($key_value)) {
      $referenced_entities = $entity
        ->get($key_value)
        ->referencedEntities();
      $labels = [];

      /** @var \Drupal\Core\Entity\EntityInterface $referenced_entity */
      foreach ($referenced_entities as $referenced_entity) {
        $labels[] = preg_replace('/\\s+/', '', $referenced_entity
          ->label());
      }
      if ($labels) {
        $options['hashtags'] = implode(',', $labels);
      }
    }
    return $options;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
Twitter::getHashtags public function
Twitter::getId public function Get the network id. Overrides SocialNetworkInterface::getId
Twitter::getLabel public function Get the network name. Overrides SocialNetworkInterface::getLabel
Twitter::getLinkAttributes public function Get common attributes for the share link. Overrides SocialNetworkInterface::getLinkAttributes
Twitter::getShareLink public function Checks whether the given transition is allowed. Overrides SocialNetworkInterface::getShareLink
Twitter::TWITTER_URL constant The social network base share link.