You are here

public function SendToPhoneForm::buildForm in SMS Framework 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

modules/sms_sendtophone/src/Form/SendToPhoneForm.php, line 57

Class

SendToPhoneForm
Default controller for the sms_sendtophone module.

Namespace

Drupal\sms_sendtophone\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $type = NULL, $extra = NULL) {

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

  /** @var \Drupal\user\UserInterface $user */
  $user = User::load($this
    ->currentUser()
    ->id());

  // @todo This block should be a route access checker.
  try {
    $this->phoneNumbers = $phone_number_provider
      ->getPhoneNumbers($user);
  } catch (PhoneNumberSettingsException $e) {
  }
  if ($user
    ->hasPermission('send to any number') || count($this->phoneNumbers)) {
    $form = $this
      ->getForm($form, $form_state, $type, $extra);
  }
  else {
    if (!count($this->phoneNumbers)) {

      // User has no phone number, or unconfirmed.
      $form['message'] = [
        '#type' => 'markup',
        '#markup' => $this
          ->t('You need to @setup and confirm your mobile phone to send messages.', [
          '@setup' => $user
            ->toLink('set up', 'edit-form')
            ->toString(),
        ]),
      ];
    }
    else {
      $destination = [
        'query' => \Drupal::service('redirect.destination')
          ->getAsArray(),
      ];
      $form['message'] = [
        '#markup' => $this
          ->t('You do not have permission to send messages. You may need to @signin or @register for an account to send messages to a mobile phone.', [
          '@signin' => $this
            ->l('sign in', Url::fromRoute('user.page', [], $destination)),
          '@register' => $this
            ->l('register', Url::fromRoute('user.register', [], $destination)),
        ]),
      ];
    }
  }
  return $form;
}