You are here

public function PhoneNumberSettingsForm::save in SMS Framework 8

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

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

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

src/Form/PhoneNumberSettingsForm.php, line 232

Class

PhoneNumberSettingsForm
Form controller for phone number settings.

Namespace

Drupal\sms\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $config =& $this->entity;
  list($entity_type_id, $bundle) = explode('|', $form_state
    ->getValue('entity_bundle'));
  $config
    ->setPhoneNumberEntityTypeId($entity_type_id)
    ->setPhoneNumberBundle($bundle);
  $config
    ->setVerificationMessage($form_state
    ->getValue('verification_message'))
    ->setVerificationCodeLifetime($form_state
    ->getValue('code_lifetime'))
    ->setPurgeVerificationPhoneNumber((bool) $form_state
    ->getValue('phone_number_purge'));
  foreach ($form_state
    ->getValue('field_mapping') as $config_key => $field_name) {
    if ($field_name == self::CREATE_NEW_FIELD) {
      $field_config = $this
        ->createNewField($entity_type_id, $bundle, $config_key);
      $field_name = $field_config
        ->getName();
    }
    else {

      // Use existing field.

      /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $entity_form_display */
      $entity_form_display = $field_storage_config = $this->entityTypeManager
        ->getStorage('entity_form_display')
        ->load($entity_type_id . '.' . $bundle . '.default');
      if ($entity_form_display) {
        $component = $entity_form_display
          ->getComponent($field_name) ?: [];

        // Only change existing form formatter if it is using default
        // widget, or none at all.
        if (!$component || $component && $component['type'] == 'telephone_default') {
          $component['type'] = 'sms_telephone';
          $entity_form_display
            ->setComponent($field_name, $component)
            ->save();
        }
      }
    }
    $config
      ->setFieldName($config_key, $field_name);
  }
  $saved = $config
    ->save();
  $t_args['%id'] = $config
    ->id();
  if ($saved == SAVED_NEW) {
    drupal_set_message($this
      ->t('Phone number settings %id created.', $t_args));
  }
  else {
    drupal_set_message($this
      ->t('Phone number settings %id saved.', $t_args));
  }
  $form_state
    ->setRedirectUrl(Url::fromRoute('sms.phone_number_settings.list'));
}