Twitter.php in Social simple 8
File
src/SocialNetwork/Twitter.php
View source
<?php
namespace Drupal\social_simple\SocialNetwork;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\node\NodeInterface;
class Twitter implements SocialNetworkInterface {
use StringTranslationTrait;
const TWITTER_URL = 'https://twitter.com/intent/tweet/';
public function getId() {
return 'twitter';
}
public function getLabel() {
return $this
->t('Twitter');
}
public function getShareLink($share_url, $title = '', EntityInterface $entity = NULL, array $additional_options = []) {
$options = [
'query' => [
'url' => $share_url,
'text' => $title,
],
'absolute' => TRUE,
'external' => TRUE,
];
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;
}
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;
}
public function getHashtags(ContentEntityInterface $entity) {
$options = [];
if (!$entity instanceof NodeInterface) {
return $options;
}
$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 = [];
foreach ($referenced_entities as $referenced_entity) {
$labels[] = preg_replace('/\\s+/', '', $referenced_entity
->label());
}
if ($labels) {
$options['hashtags'] = implode(',', $labels);
}
}
return $options;
}
}