You are here

public function MobileNumberItem::fieldSettingsForm in Mobile Number 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Field/FieldType/MobileNumberItem.php \Drupal\mobile_number\Plugin\Field\FieldType\MobileNumberItem::fieldSettingsForm()

Returns a form for the field-level settings.

Invoked from \Drupal\field_ui\Form\FieldConfigEditForm to allow administrators to configure field-level settings.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The form state of the (entire) configuration form.

Return value

array The form definition for the field settings.

Overrides FieldItemBase::fieldSettingsForm

File

src/Plugin/Field/FieldType/MobileNumberItem.php, line 194

Class

MobileNumberItem
Plugin implementation of the 'mobile_number' field type.

Namespace

Drupal\mobile_number\Plugin\Field\FieldType

Code

public function fieldSettingsForm(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\mobile_number\MobileNumberUtilInterface $util */
  $util = \Drupal::service('mobile_number.util');
  $field = $this
    ->getFieldDefinition();
  $settings = $this
    ->getSettings() + $this
    ->defaultFieldSettings();

  // @todo Remove FALSE after port of TFA for drupal 8 is available
  if ($form['#entity'] instanceof User && FALSE) {
    $element['tfa'] = [
      '#type' => 'checkbox',
      '#title' => t('Use this field for two-factor authentication'),
      '#description' => t("If enabled, users will be able to choose if to use the number for two factor authentication. Only one field can be set true for this value, verification must be enabled, and the field must be of cardinality 1. Users are required to verify their number when enabling their two-factor authenticaion. <a href='https://www.drupal.org/project/tfa' target='_blank'>Two Factor Authentication</a> must be installed, as well as a supported sms provider such as <a href='https://www.drupal.org/project/smsframework' target='_blank'>SMS Framework</a>."),
      '#default_value' => $this
        ->tfaAllowed() && $util
        ->getTfaField() === $this
        ->getFieldDefinition()
        ->getName(),
      '#disabled' => !$this
        ->tfaAllowed(),
    ];
    if ($this
      ->tfaAllowed()) {
      $element['tfa']['#states'] = [
        'disabled' => [
          'input[name="settings[verify]"]' => [
            'value' => $util::MOBILE_NUMBER_VERIFY_NONE,
          ],
        ],
      ];
    }
  }
  $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['verify'] = [
    '#type' => 'radios',
    '#title' => t('Verification'),
    '#options' => [
      MobileNumberUtilInterface::MOBILE_NUMBER_VERIFY_NONE => t('None'),
      MobileNumberUtilInterface::MOBILE_NUMBER_VERIFY_OPTIONAL => t('Optional'),
      MobileNumberUtilInterface::MOBILE_NUMBER_VERIFY_REQUIRED => t('Required'),
    ],
    '#default_value' => $settings['verify'],
    '#description' => (string) t('Verification requirement. Will send sms to mobile number when user requests to verify the number as their own. Requires <a href="https://www.drupal.org/project/smsframework" target="_blank">SMS Framework</a> or any other sms sending module that integrates with with the Mobile Number module.'),
    '#required' => TRUE,
    '#disabled' => !$util
      ->isSmsEnabled(),
  ];
  $element['message'] = [
    '#type' => 'textarea',
    '#title' => t('Verification Message'),
    '#default_value' => $settings['message'],
    '#description' => t('The SMS message to send during verification. Replacement parameters are available for verification code (!code) and site name (!site_name). Additionally, tokens are available if the token module is enabled, but be aware that entity values will not be available on entity creation forms as the entity was not created yet.'),
    '#required' => TRUE,
    '#token_types' => [
      $field
        ->getTargetEntityTypeId(),
    ],
    '#disabled' => !$util
      ->isSmsEnabled(),
    '#element_validate' => [
      [
        $this,
        'fieldSettingsFormValidate',
      ],
    ],
  ];
  if (\Drupal::moduleHandler()
    ->moduleExists('token')) {
    $element['message']['#element_validate'] = [
      'token_element_validate',
    ];
    $element['message_token_tree']['token_tree'] = [
      '#theme' => 'token_tree',
      '#token_types' => [
        $field
          ->getTargetEntityTypeId(),
      ],
      '#dialog' => TRUE,
    ];
  }
  return $element;
}