You are here

class SocialSimpleGenerator in Social simple 8

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

Class SocialSimpleGenerator.

@package Drupal\social_simple

Hierarchy

Expanded class hierarchy of SocialSimpleGenerator

1 file declares its use of SocialSimpleGenerator
SocialSimpleBlock.php in src/Plugin/Block/SocialSimpleBlock.php
1 string reference to 'SocialSimpleGenerator'
social_simple.services.yml in ./social_simple.services.yml
social_simple.services.yml
1 service uses SocialSimpleGenerator
social_simple.generator in ./social_simple.services.yml
Drupal\social_simple\SocialSimpleGenerator

File

src/SocialSimpleGenerator.php, line 21

Namespace

Drupal\social_simple
View source
class SocialSimpleGenerator implements SocialSimpleGeneratorInterface {
  use StringTranslationTrait;

  /**
   * Drupal\Core\Controller\TitleResolver definition.
   *
   * @var \Drupal\Core\Controller\TitleResolver
   */
  protected $titleResolver;

  /**
   * Drupal\Core\Routing\CurrentRouteMatch definition.
   *
   * @var \Drupal\Core\Routing\CurrentRouteMatch
   */
  protected $currentRouteMatch;

  /**
   * Symfony\Component\HttpFoundation\RequestStack definition.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * Drupal\Core\Config\ConfigFactory definition.
   *
   * @var \Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * Drupal\Core\Render\Renderer .
   *
   * @var \Drupal\Core\Render\Renderer
   */
  protected $renderer;

  /**
   * Drupal\social_simple\SocialSimpleManagerInterface.
   *
   * @var \Drupal\social_simple\SocialSimpleManagerInterface
   */
  protected $socialSimpleManager;

  /**
   * An array of available social network.
   *
   * @var array
   */
  protected $networks = [];

  /**
   * Constructs a new SocialSimpleGenerator object.
   *
   * @param \Drupal\Core\Controller\TitleResolver $title_resolver
   *   The title resolver service.
   * @param \Drupal\Core\Routing\CurrentRouteMatch $current_route_match
   *   The current route match.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   * @param \Drupal\Core\Config\ConfigFactory $config_factory
   *   The config factory.
   * @param \Drupal\Core\Render\Renderer $renderer_service
   *   The renderer service.
   * @param \Drupal\social_simple\SocialSimpleManagerInterface $social_simple_manager
   *   The social simple manager.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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 '';
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function getNetworks() {
    if (empty($this->networks)) {
      $this->networks = $this->socialSimpleManager
        ->getNetworks();
    }
    return $this->networks;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SocialSimpleGenerator::$configFactory protected property Drupal\Core\Config\ConfigFactory definition.
SocialSimpleGenerator::$currentRouteMatch protected property Drupal\Core\Routing\CurrentRouteMatch definition.
SocialSimpleGenerator::$networks protected property An array of available social network.
SocialSimpleGenerator::$renderer protected property Drupal\Core\Render\Renderer .
SocialSimpleGenerator::$requestStack protected property Symfony\Component\HttpFoundation\RequestStack definition.
SocialSimpleGenerator::$socialSimpleManager protected property Drupal\social_simple\SocialSimpleManagerInterface.
SocialSimpleGenerator::$titleResolver protected property Drupal\Core\Controller\TitleResolver definition.
SocialSimpleGenerator::buildSocialLinks public function Build the render array of social share links. Overrides SocialSimpleGeneratorInterface::buildSocialLinks
SocialSimpleGenerator::generateSocialLinks public function Build the social share links. Overrides SocialSimpleGeneratorInterface::generateSocialLinks
SocialSimpleGenerator::getNetworks public function Return an array of social networks supported. Overrides SocialSimpleGeneratorInterface::getNetworks
SocialSimpleGenerator::getShareUrl public function Get the share url. Overrides SocialSimpleGeneratorInterface::getShareUrl
SocialSimpleGenerator::getTitle public function Get the title to share. Overrides SocialSimpleGeneratorInterface::getTitle
SocialSimpleGenerator::__construct public function Constructs a new SocialSimpleGenerator object.
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.