You are here

function sms_sendtophone_node_links_alter in SMS Framework 8

Same name and namespace in other branches
  1. 2.x modules/sms_sendtophone/sms_sendtophone.module \sms_sendtophone_node_links_alter()
  2. 2.1.x modules/sms_sendtophone/sms_sendtophone.module \sms_sendtophone_node_links_alter()

Implements hook_node_links_alter().

File

modules/sms_sendtophone/sms_sendtophone.module, line 26
Provides hooks for Send To Phone.

Code

function sms_sendtophone_node_links_alter(array &$links, NodeInterface $node, array &$context) {

  /** @var \Drupal\sms\Provider\PhoneNumberProviderInterface $phone_number_provider */
  $phone_number_provider = \Drupal::service('sms.phone_number');

  /** @var \Drupal\user\UserInterface $user */
  $user = User::load(\Drupal::currentUser()
    ->id());
  $phone_numbers = [];
  try {
    $phone_numbers = $phone_number_provider
      ->getPhoneNumbers($user);
  } catch (PhoneNumberSettingsException $e) {
  }
  $types = \Drupal::config('sms_sendtophone.settings')
    ->get('content_types');
  $build = [];
  if (in_array($node
    ->bundle(), $types)) {
    if ($user
      ->hasPermission('send to any number') || count($phone_numbers)) {

      // Only show "send to phone" link if user is permitted to do so.
      $url = Url::fromRoute('sms_sendtophone.page', [
        'type' => 'node',
        'extra' => $node
          ->id(),
      ], [
        'query' => \Drupal::destination()
          ->getAsArray(),
      ]);
      $build['sms_sendtophone'] = [
        'title' => t('Send to phone'),
        'url' => $url,
        'attributes' => [
          'class' => 'sms-sendtophone',
          'title' => 'Send a link via SMS.',
        ],
      ];
    }
    elseif ($user
      ->isAuthenticated()) {

      // Show messages to encourage users to register their mobile number.
      if (!count($phone_numbers)) {
        $build['sms_sendtophone'] = [
          'title' => t('Set up and confirm your mobile number to send to phone.'),
          'url' => Url::fromRoute('entity.user.edit_form', [
            'user' => $user
              ->id(),
          ]),
        ];
      }
    }
    $links['sms_sendtophone'] = [
      '#theme' => 'links__node__sms_sendtophone',
      '#links' => $build,
      '#attributes' => [
        'class' => [
          'links',
          'inline',
        ],
      ],
    ];
  }
}