You are here

class Mail in Social simple 2.0.x

Same name and namespace in other branches
  1. 8 src/SocialNetwork/Mail.php \Drupal\social_simple\SocialNetwork\Mail

The Mail button.

Hierarchy

Expanded class hierarchy of Mail

2 string references to 'Mail'
Mail::getLabel in src/SocialNetwork/Mail.php
Get the network name.
social_simple.services.yml in ./social_simple.services.yml
social_simple.services.yml
1 service uses Mail
social_simple.mail in ./social_simple.services.yml
Drupal\social_simple\SocialNetwork\Mail

File

src/SocialNetwork/Mail.php, line 14

Namespace

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

  /**
   * The social network base share link.
   */
  const MAIL = 'mailto:';

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;
  public function __construct(ModuleHandlerInterface $moduleHandler) {
    $this->moduleHandler = $moduleHandler;
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function getShareLink($share_url, $title = '', EntityInterface $entity = NULL, array $additional_options = []) {
    $options = [
      'query' => [
        'body' => PHP_EOL . $title . PHP_EOL . $share_url,
        'subject' => $title,
      ],
      'absolute' => TRUE,
      'external' => TRUE,
    ];
    if ($additional_options) {
      foreach ($additional_options as $id => $value) {
        $options['query'][$id] = $value;
      }
    }
    if ($entity && $this
      ->checkForwardIntegration($entity)) {
      $url = Url::fromRoute('forward.form', [
        'entity_type' => $entity
          ->getEntityTypeId(),
        'entity' => $entity
          ->id(),
      ]);
    }
    else {
      $url = Url::fromUri(self::MAIL, $options);
    }
    $link = [
      'url' => $url,
      'title' => [
        '#markup' => '<i class="fa fa-envelope"></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,
      'data-popup-open' => 'false',
    ];
    return $attributes;
  }

  /**
   * Check if the mail button should use the forward module.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity which will be shared.
   *
   * @return boolean
   *   True if the Mail button should use the Forward module.
   */
  protected function checkForwardIntegration(EntityInterface $entity) {
    if (!$this->moduleHandler
      ->moduleExists('forward')) {
      return FALSE;
    }

    /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity_type */
    $entity_type = $entity->type->entity;
    if (!$entity_type instanceof ConfigEntityInterface) {
      return FALSE;
    }
    return $entity_type
      ->getThirdPartySetting('social_simple', 'forward_integration', FALSE);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Mail::$moduleHandler protected property The module handler.
Mail::checkForwardIntegration protected function Check if the mail button should use the forward module.
Mail::getId public function Get the network id. Overrides SocialNetworkInterface::getId
Mail::getLabel public function Get the network name. Overrides SocialNetworkInterface::getLabel
Mail::getLinkAttributes public function Get common attributes for the share link. Overrides SocialNetworkInterface::getLinkAttributes
Mail::getShareLink public function Checks whether the given transition is allowed. Overrides SocialNetworkInterface::getShareLink
Mail::MAIL constant The social network base share link.
Mail::__construct public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
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.