You are here

public function MobileNumberWidget::settingsForm in Mobile Number 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Field/FieldWidget/MobileNumberWidget.php \Drupal\mobile_number\Plugin\Field\FieldWidget\MobileNumberWidget::settingsForm()

Returns a form to configure settings for the widget.

Invoked from \Drupal\field_ui\Form\EntityDisplayFormBase to allow administrators to configure the widget. The field_ui module takes care of handling submitted form values.

Parameters

array $form: The form where the settings form is being included in.

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

Return value

array The form definition for the widget settings.

Overrides WidgetBase::settingsForm

File

src/Plugin/Field/FieldWidget/MobileNumberWidget.php, line 45

Class

MobileNumberWidget
Plugin implementation of the 'mobile_number' widget.

Namespace

Drupal\mobile_number\Plugin\Field\FieldWidget

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $element = parent::settingsForm($form, $form_state);
  $field_settings = $this
    ->getFieldSettings();
  $field_country_validation = isset($field_settings['countries']);

  /** @var \Drupal\mobile_number\Element\MobileNumberUtilInterface $util */
  $util = \Drupal::service('mobile_number.util');

  /** @var ContentEntityInterface $entity */
  $entity = $form_state
    ->getFormObject()
    ->getEntity();
  $form_state
    ->set('field_item', $this);
  $verification_enabled = !empty($this->fieldDefinition) && $this->fieldDefinition
    ->getType() == 'mobile_number';
  $element['default_country'] = [
    '#type' => 'select',
    '#title' => t('Default Country'),
    '#options' => $util
      ->getCountryOptions([], TRUE),
    '#default_value' => $this
      ->getSetting('default_country'),
    '#description' => t('Default country for mobile number input.'),
    '#required' => TRUE,
    '#element_validate' => [
      [
        $this,
        'settingsFormValidate',
      ],
    ],
  ];
  if (!$field_country_validation) {
    $element['countries'] = [
      '#type' => 'select',
      '#title' => t('Allowed Countries'),
      '#options' => $util
        ->getCountryOptions([], TRUE),
      '#default_value' => $this
        ->getSetting('countries'),
      '#description' => t('Allowed countries for the mobile number. If none selected, then all are allowed.'),
      '#multiple' => TRUE,
      '#attached' => [
        'library' => [
          'mobile_number/element',
        ],
      ],
    ];
  }
  $element['placeholder'] = [
    '#type' => 'textfield',
    '#title' => t('Number Placeholder'),
    '#default_value' => $this
      ->getSetting('placeholder') !== NULL ? $this
      ->getSetting('placeholder') : 'Phone number',
    '#description' => t('Number field placeholder.'),
    '#required' => FALSE,
  ];
  if ($verification_enabled) {
  }
  return $element;
}