You are here

class EntityPrintPdf in Social simple 8

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

The social network Twitter.

Hierarchy

Expanded class hierarchy of EntityPrintPdf

1 string reference to 'EntityPrintPdf'
social_simple.services.yml in ./social_simple.services.yml
social_simple.services.yml
1 service uses EntityPrintPdf
social_simple.entity_print_pdf in ./social_simple.services.yml
Drupal\social_simple\SocialNetwork\EntityPrintPdf

File

src/SocialNetwork/EntityPrintPdf.php, line 15

Namespace

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

  /**
   * The entity print view route.
   */
  const ENTITY_PRINT_ROUTE = 'entity_print.view';

  /**
   * The export type engine.
   */
  const ENTITY_PRINT_EXPORT_TYPE = 'pdf';

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

  /**
   * Drupal\Core\Extension\ModuleHandlerInterface
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Constructs a new Entity Print Pdf object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
    $this->configFactory = $config_factory;
    $this->moduleHandler = $module_handler;
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function getShareLink($share_url, $title = '', EntityInterface $entity = NULL, array $additional_options = []) {
    $link = [
      'url' => Url::fromUserInput('#'),
      'title' => [
        '#markup' => '<i class="fa fa-file-pdf-o"></i><span class="visually-hidden">' . $this
          ->getLabel() . '</span>',
      ],
      'attributes' => [
        'style' => 'display: none;',
      ],
    ];
    if (!$this->moduleHandler
      ->moduleExists('entity_print')) {
      return $link;
    }
    if (!$entity instanceof ContentEntityInterface) {
      return $link;
    }
    $entity_type_id = $entity
      ->getEntityTypeId();
    $entity_id = $entity
      ->id();
    $enabled_entity_type_ids = $this->configFactory
      ->get('entity_print.settings')
      ->get('enabled_entity_type_ids');
    if (!empty($enabled_entity_type_ids) && !in_array($entity_type_id, $enabled_entity_type_ids)) {
      return $link;
    }
    $route_params = [
      'entity_type' => $entity_type_id,
      'entity_id' => $entity_id,
      'export_type' => trim(self::ENTITY_PRINT_EXPORT_TYPE, '_engine'),
    ];
    $options = [];
    if ($additional_options) {
      foreach ($additional_options as $id => $value) {
        $options['query'][$id] = $value;
      }
    }
    $url = Url::fromRoute(self::ENTITY_PRINT_ROUTE, $route_params, $options);
    $link = [
      'url' => $url,
      'title' => [
        '#markup' => '<i class="fa fa-file-pdf-o"></i><span class="visually-hidden">' . $this
          ->getLabel() . '</span>',
      ],
      'attributes' => $this
        ->getLinkAttributes($this
        ->getLabel()),
    ];
    return $link;
  }

  /**
   * {@inheritdoc}
   */
  public function getLinkAttributes($network_name) {
    $attributes = [
      'title' => $network_name,
      'target' => '_blank',
      'rel' => 'noopener noreferrer nofollow',
    ];
    return $attributes;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityPrintPdf::$configFactory protected property Drupal\Core\Config\ConfigFactoryInterface definition.
EntityPrintPdf::$moduleHandler protected property Drupal\Core\Extension\ModuleHandlerInterface
EntityPrintPdf::ENTITY_PRINT_EXPORT_TYPE constant The export type engine.
EntityPrintPdf::ENTITY_PRINT_ROUTE constant The entity print view route.
EntityPrintPdf::getId public function Get the network id. Overrides SocialNetworkInterface::getId
EntityPrintPdf::getLabel public function Get the network name. Overrides SocialNetworkInterface::getLabel
EntityPrintPdf::getLinkAttributes public function Get common attributes for the share link. Overrides SocialNetworkInterface::getLinkAttributes
EntityPrintPdf::getShareLink public function Checks whether the given transition is allowed. Overrides SocialNetworkInterface::getShareLink
EntityPrintPdf::__construct public function Constructs a new Entity Print Pdf 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.