class Twitter in Social simple 8
Same name and namespace in other branches
- 2.0.x src/SocialNetwork/Twitter.php \Drupal\social_simple\SocialNetwork\Twitter
The social network Twitter.
Hierarchy
- class \Drupal\social_simple\SocialNetwork\Twitter implements SocialNetworkInterface uses StringTranslationTrait
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
File
- src/
SocialNetwork/ Twitter.php, line 15
Namespace
Drupal\social_simple\SocialNetworkView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
Twitter:: |
public | function | ||
Twitter:: |
public | function |
Get the network id. Overrides SocialNetworkInterface:: |
|
Twitter:: |
public | function |
Get the network name. Overrides SocialNetworkInterface:: |
|
Twitter:: |
public | function |
Get common attributes for the share link. Overrides SocialNetworkInterface:: |
|
Twitter:: |
public | function |
Checks whether the given transition is allowed. Overrides SocialNetworkInterface:: |
|
Twitter:: |
constant | The social network base share link. |