class SendToPhoneForm in SMS Framework 8
Default controller for the sms_sendtophone module.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\sms_sendtophone\Form\SendToPhoneForm
Expanded class hierarchy of SendToPhoneForm
1 string reference to 'SendToPhoneForm'
- sms_sendtophone.routing.yml in modules/
sms_sendtophone/ sms_sendtophone.routing.yml - modules/sms_sendtophone/sms_sendtophone.routing.yml
File
- modules/
sms_sendtophone/ src/ Form/ SendToPhoneForm.php, line 19
Namespace
Drupal\sms_sendtophone\FormView source
class SendToPhoneForm extends FormBase {
/**
* Phone numbers for the authenticated user.
*
* @var array
*/
protected $phoneNumbers = [];
/**
* The SMS Provider.
*
* @var \Drupal\sms\Provider\SmsProviderInterface
*/
protected $smsProvider;
/**
* Creates an new SendForm object.
*
* @param \Drupal\sms\Provider\SmsProviderInterface $sms_provider
* The SMS service provider.
*/
public function __construct(SmsProviderInterface $sms_provider) {
$this->smsProvider = $sms_provider;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('sms.provider'));
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'sms_sendtophone_form';
}
/**
* Builds the form array.
*/
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,
];
// Add library for CSS styling.
$form['#attached']['library'] = 'sms_sendtophone/default';
return $form;
}
/**
* {@inheritdoc}
*/
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');
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
SendToPhoneForm:: |
protected | property | Phone numbers for the authenticated user. | |
SendToPhoneForm:: |
protected | property | The SMS Provider. | |
SendToPhoneForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
SendToPhoneForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
SendToPhoneForm:: |
protected | function | Builds the form array. | |
SendToPhoneForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
SendToPhoneForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
SendToPhoneForm:: |
public | function | Creates an new SendForm object. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |