public function SmsTelephoneWidget::formElement in SMS Framework 8
Returns the form for a single field widget.
Field widget form elements should be based on the passed-in $element, which contains the base form element properties derived from the field configuration.
The BaseWidget methods will set the weight, field name and delta values for each form element. If there are multiple values for this field, the formElement() method will be called as many times as needed.
Other modules may alter the form element provided by this function using hook_field_widget_form_alter() or hook_field_widget_WIDGET_TYPE_form_alter().
The FAPI element callbacks (such as #process, #element_validate, #value_callback, etc.) used by the widget do not have access to the original $field_definition passed to the widget's constructor. Therefore, if any information is needed from that definition by those callbacks, the widget implementing this method, or a hook_field_widget[_WIDGET_TYPE]_form_alter() implementation, must extract the needed properties from the field definition and set them as ad-hoc $element['#custom'] properties, for later use by its element callbacks.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: Array of default values for this field.
int $delta: The order of this item in the array of sub-elements (0, 1, 2, etc.).
array $element: A form element array containing basic properties for the widget:
- #field_parents: The 'parents' space for the field in the form. Most widgets can simply overlook this property. This identifies the location where the field values are placed within $form_state->getValues(), and is used to access processing information for the field through the getWidgetState() and setWidgetState() methods.
- #title: The sanitized element label for the field, ready for output.
- #description: The sanitized element description for the field, ready for output.
- #required: A Boolean indicating whether the element value is required; for required multiple value fields, only the first widget's values are required.
- #delta: The order of this item in the array of sub-elements; see $delta above.
array $form: The form structure where widgets are being attached to. This might be a full form structure, or a sub-element of a larger form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form elements for a single widget for this field.
Overrides TelephoneDefaultWidget::formElement
See also
hook_field_widget_form_alter()
hook_field_widget_WIDGET_TYPE_form_alter()
File
- src/
Plugin/ Field/ FieldWidget/ SmsTelephoneWidget.php, line 29
Class
- SmsTelephoneWidget
- Plugin implementation of the 'sms_telephone' widget.
Namespace
Drupal\sms\Plugin\Field\FieldWidgetCode
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
/** @var \Drupal\sms\Provider\PhoneNumberVerificationInterface $phone_number_verification_provider */
$phone_number_verification_provider = \Drupal::service('sms.phone_number.verification');
try {
$config = $phone_number_verification_provider
->getPhoneNumberSettingsForEntity($items
->getEntity());
} catch (PhoneNumberSettingsException $e) {
return $element;
}
/** @var \Drupal\Core\Datetime\DateFormatter $date_formatter */
$date_formatter = \Drupal::service('date.formatter');
$current_time = \Drupal::request()->server
->get('REQUEST_TIME');
$t_args['@url'] = $this
->url('sms.phone.verify');
$lifetime = $config
->getVerificationCodeLifetime() ?: 0;
if (isset($items[$delta]->value)) {
$phone_verification = $phone_number_verification_provider
->getPhoneVerificationByEntity($items
->getEntity(), $items[$delta]->value);
if ($phone_verification) {
if ($phone_verification
->getStatus()) {
$element['value']['#description'] = $this
->t('This phone number is verified. <strong>Warning:</strong> Modifying this phone number will remove verification.');
}
else {
$element['value']['#disabled'] = TRUE;
$expiration_date = $phone_verification
->getCreatedTime() + $lifetime;
if ($current_time < $expiration_date) {
$t_args['@time'] = $date_formatter
->formatTimeDiffUntil($expiration_date, [
'granularity' => 2,
]);
$element['value']['#description'] = $this
->t('A verification code has been sent to this phone number. Go to the <a href="@url">verification form</a> and enter the code. The code will expire if it is not verified in @time.', $t_args);
}
else {
// This message displays if we are waiting for cron to delete
// expired verification codes.
$element['value']['#description'] = $this
->t('Verification code expired. Try again later.');
}
}
}
else {
// This message will display if there is a field value, but the
// verification expired.
$t_args['@time'] = $date_formatter
->formatInterval($lifetime, 2);
$element['value']['#description'] = $this
->t('Save this form to send a new verification code as an SMS message, you must enter the code into the <a href="@url">verification form</a> within @time.', $t_args);
}
}
else {
$t_args['@time'] = $date_formatter
->formatInterval($lifetime, 2);
$element['value']['#description'] = $this
->t('Enter a phone number. A verification code will be sent as an SMS message, you must enter the code into the <a href="@url">verification form</a> within @time.', $t_args);
}
return $element;
}