You are here

public function PhoneNumberSettingsForm::buildForm in SMS Framework 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

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

Return value

array The form structure.

Overrides EntityForm::buildForm

File

src/Form/PhoneNumberSettingsForm.php, line 82

Class

PhoneNumberSettingsForm
Form controller for phone number settings.

Namespace

Drupal\sms\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config =& $this->entity;
  $form = parent::buildForm($form, $form_state);
  $bundles = [];
  $storage = $this->entityTypeManager
    ->getStorage('phone_number_settings');

  // Generate a list of field-able bundles.
  foreach ($this->entityTypeManager
    ->getDefinitions() as $entity_type) {
    if ($entity_type
      ->isSubclassOf(ContentEntityInterface::class)) {
      foreach ($this->entityTypeBundleInfo
        ->getBundleInfo($entity_type
        ->id()) as $bundle => $bundle_info) {

        // Do not show combinations with pre-existing phone number settings.
        // But, make sure all options are available on existing phone
        // number settings. They are hidden + read only from user anyway.
        if (!$storage
          ->load($entity_type
          ->id() . '.' . $bundle) || !$config
          ->isNew()) {
          $bundles[(string) $entity_type
            ->getLabel()][$entity_type
            ->id() . '|' . $bundle] = $bundle_info['label'];
        }
      }
    }
  }
  $bundle_default_value = !$config
    ->isNew() ? $config
    ->getPhoneNumberEntityTypeId() . '|' . $config
    ->getPhoneNumberBundle() : NULL;

  // Field cannot be called 'bundle' or odd behaviour will happen on re-saves.
  $form['entity_bundle'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Bundle'),
    '#options' => $bundles,
    '#default_value' => $bundle_default_value,
    '#required' => TRUE,
    '#access' => $config
      ->isNew(),
    '#ajax' => [
      'callback' => '::updateFieldMapping',
      'wrapper' => 'edit-field-mapping-wrapper',
    ],
  ];
  if (!$bundles) {
    $form['entity_bundle']['#empty_option'] = $this
      ->t('No Bundles Available');
  }
  $field_options = [];
  $field_options['telephone'][self::CREATE_NEW_FIELD] = $this
    ->t('- Create a new telephone field -');
  $field_options['boolean'][self::CREATE_NEW_FIELD] = $this
    ->t('- Create a new boolean field -');
  if ($entity_bundle = $form_state
    ->getValue('entity_bundle', $bundle_default_value ?: NULL)) {
    list($entity_type_id, $bundle) = explode('|', $entity_bundle);
    if (!empty($entity_type_id) && !empty($bundle)) {
      $field_definitions = $this->entityFieldManager
        ->getFieldDefinitions($entity_type_id, $bundle);
      foreach ($field_definitions as $field_definition) {
        $field_type = $field_definition
          ->getType();
        if (isset($field_options[$field_type])) {
          $field_options[$field_type][$field_definition
            ->getName()] = $field_definition
            ->getLabel();
        }
      }
    }
  }
  $form['field_mapping'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Field mapping'),
    '#prefix' => '<div id="edit-field-mapping-wrapper">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
  ];

  // Form ID must be the same as found in $config->getFieldName().
  // ($config->getFieldName($config_key) ==
  // $form['field_mapping'][$config_key]).
  $form['field_mapping']['phone_number'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Phone number'),
    '#description' => $this
      ->t('Select the field storing phone numbers.'),
    '#options' => $field_options['telephone'],
    '#required' => TRUE,
    '#empty_option' => $this
      ->t('- Select -'),
    '#default_value' => $config
      ->getFieldName('phone_number'),
  ];
  $form['field_mapping']['automated_opt_out'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Automated messages opt out'),
    '#description' => $this
      ->t('Select the field storing preference to opt out of automated messages.'),
    '#options' => $field_options['boolean'],
    '#empty_option' => $this
      ->t('- None -'),
    '#default_value' => $config
      ->getFieldName('automated_opt_out'),
  ];
  $form['message'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Messages'),
  ];
  $form['message']['verification_message'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Verification message'),
    '#description' => $this
      ->t('SMS message sent to verify a phone number. The message should contain the verification code and a link to the verification page.'),
    '#default_value' => $config
      ->isNew() ? "Your verification code is '[sms-message:verification-code]'.\nGo to [sms:verification-url] to verify your phone number.\n- [site:name]" : $config
      ->getVerificationMessage(),
  ];
  $tokens = [
    'sms-message',
  ];
  if ($this->moduleHandler
    ->moduleExists('token')) {
    $form['message']['tokens']['list'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => $tokens,
    ];
  }
  else {
    foreach ($tokens as &$token) {
      $token = "[{$token}:*]";
    }
    $form['message']['tokens']['list'] = [
      '#markup' => $this
        ->t('Available tokens include: @token_types', [
        '@token_types' => implode(' ', $tokens),
      ]),
    ];
  }
  $form['expiration'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Verification expiration'),
  ];
  $form['expiration']['code_lifetime'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Verification code lifetime'),
    '#description' => $this
      ->t('How long a verification code is valid, before it expires. Existing verification codes are retroactively updated.'),
    '#field_suffix' => $this
      ->t('seconds'),
    '#required' => TRUE,
    '#min' => 60,
    '#default_value' => $config
      ->isNew() ? 3600 : $config
      ->getVerificationCodeLifetime(),
  ];
  $form['expiration']['phone_number_purge'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Purge phone numbers'),
    '#description' => $this
      ->t('Remove phone number if verification code expires.'),
    '#default_value' => $config
      ->isNew() ?: $config
      ->getPurgeVerificationPhoneNumber(),
  ];
  return $form;
}