View source
<?php
namespace Drupal\social_simple;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Core\Controller\TitleResolver;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\Core\Url;
use Drupal\social_simple\SocialNetwork\SocialNetworkInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Render\Renderer;
class SocialSimpleGenerator implements SocialSimpleGeneratorInterface {
use StringTranslationTrait;
protected $titleResolver;
protected $currentRouteMatch;
protected $requestStack;
protected $configFactory;
protected $renderer;
protected $socialSimpleManager;
protected $networks = [];
public function __construct(TitleResolver $title_resolver, CurrentRouteMatch $current_route_match, RequestStack $request_stack, ConfigFactory $config_factory, Renderer $renderer_service, SocialSimpleManagerInterface $social_simple_manager) {
$this->titleResolver = $title_resolver;
$this->currentRouteMatch = $current_route_match;
$this->requestStack = $request_stack;
$this->configFactory = $config_factory;
$this->renderer = $renderer_service;
$this->socialSimpleManager = $social_simple_manager;
}
public function buildSocialLinks(array $networks, $title, EntityInterface $entity = NULL, array $options = []) {
$links = $this
->generateSocialLinks($networks, $entity, $options);
$build = [
'#theme' => 'social_simple_buttons',
'#links' => $links,
'#attributes' => [
'class' => [
'links',
'inline',
'social-buttons-links',
],
],
'#heading' => [
'text' => $title,
'level' => 'div',
'attributes' => [
'class' => [
'social-buttons-title',
],
],
],
];
$build['#attached']['library'][] = 'social_simple/buttons';
return $build;
}
public function generateSocialLinks(array $networks, EntityInterface $entity = NULL, array $options = []) {
$links = [];
$title = $this
->getTitle($entity);
$share_url = $this
->getShareUrl($entity);
$networks_supported = $this
->getNetworks();
$networks = array_intersect_key($networks_supported, $networks);
foreach ($networks as $network_id => $network_name) {
$additional_options = isset($options[$network_id]) ? $options[$network_id] : [];
if ($this->socialSimpleManager
->get($network_id) instanceof SocialNetworkInterface) {
$links[$network_id] = $this->socialSimpleManager
->get($network_id)
->getShareLink($share_url, $title, $entity, $additional_options);
}
}
return $links;
}
public function getTitle(EntityInterface $entity = NULL) {
$title = NULL;
if ($entity) {
$title = $entity
->label();
}
elseif ($route_object = $this->currentRouteMatch
->getRouteObject()) {
$title = $this->titleResolver
->getTitle($this->requestStack
->getCurrentRequest(), $route_object);
}
if (is_string($title)) {
return $title;
}
elseif (is_array($title)) {
return $this->renderer
->render($title);
}
elseif ($title instanceof MarkupInterface) {
return (string) $title;
}
return '';
}
public function getShareUrl(EntityInterface $entity = NULL) {
if ($entity) {
$share_url = $entity
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString();
}
else {
$share_url = Url::fromRoute('<current>', [], [
'absolute' => 'true',
])
->toString();
}
return $share_url;
}
public function getNetworks() {
if (empty($this->networks)) {
$this->networks = $this->socialSimpleManager
->getNetworks();
}
return $this->networks;
}
}