View source
<?php
namespace Drupal\sms_sendtophone\Form;
use Drupal\Core\Form\FormBase;
use Drupal\sms\Provider\SmsProviderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\node\Entity\Node;
use Drupal\sms\Entity\SmsMessage;
use Drupal\sms\Direction;
use Drupal\user\Entity\User;
use Drupal\sms\Exception\PhoneNumberSettingsException;
class SendToPhoneForm extends FormBase {
protected $phoneNumbers = [];
protected $smsProvider;
public function __construct(SmsProviderInterface $sms_provider) {
$this->smsProvider = $sms_provider;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('sms.provider'));
}
public function buildForm(array $form, FormStateInterface $form_state, $type = NULL, $extra = NULL) {
$phone_number_provider = \Drupal::service('sms.phone_number');
$user = User::load($this
->currentUser()
->id());
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)) {
$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;
}
public function getFormId() {
return 'sms_sendtophone_form';
}
protected function getForm(array $form, FormStateInterface $form_state, $type = NULL, $extra = NULL) {
switch ($type) {
case 'cck':
case 'field':
case 'inline':
$form['message'] = [
'#type' => 'value',
'#value' => $this
->getRequest()
->get('text'),
];
$form['message_preview'] = [
'#type' => 'item',
'#markup' => '<p class="sms-sendtophone--message-preview">' . $this
->getRequest()
->get('text') . '</p>',
'#title' => t('Message preview'),
];
break;
case 'node':
if (is_numeric($extra)) {
$node = Node::load($extra);
$form['message_display'] = [
'#type' => 'textarea',
'#title' => t('Message preview'),
'#description' => t('This URL will be sent to the phone.'),
'#cols' => 35,
'#rows' => 2,
'#attributes' => [
'disabled' => TRUE,
],
'#default_value' => $node
->toUrl()
->setAbsolute()
->toString(),
];
$form['message'] = [
'#type' => 'value',
'#value' => $node
->toUrl()
->setAbsolute()
->toString(),
];
}
break;
}
$form['number'] = [
'#type' => 'tel',
'#title' => $this
->t('Phone number'),
];
if (count($this->phoneNumbers)) {
$form['number']['#default_value'] = reset($this->phoneNumbers);
}
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Send'),
'#weight' => 20,
];
$form['#attached']['library'] = 'sms_sendtophone/default';
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$user = User::load($this
->currentUser()
->id());
$number = $form_state
->getValue('number');
$message = $form_state
->getValue('message');
$sms_message = SmsMessage::create()
->setDirection(Direction::OUTGOING)
->setMessage($message)
->setSenderEntity($user)
->addRecipient($number);
try {
$this->smsProvider
->queue($sms_message);
drupal_set_message($this
->t('Message has been sent.'));
} catch (\Exception $e) {
drupal_set_message($this
->t('Message could not be sent: @error', [
'@error' => $e
->getMessage(),
]), 'error');
}
}
}