You are here

function contact_storage_form_contact_form_form_alter in Contact Storage 8

Implements hook_form_FORM_ID_alter() for contact_form_form().

File

./contact_storage.module, line 44
Contains main module logic.

Code

function contact_storage_form_contact_form_form_alter(&$form, FormStateInterface $form_state) {

  /** @var \Drupal\contact\ContactFormInterface $contact_form */
  $form_object = $form_state
    ->getFormObject();
  if (!in_array($form_object
    ->getOperation(), [
    'edit',
    'add',
  ], TRUE)) {

    // Only alter the edit and add forms.
    return;
  }
  $contact_form = $form_object
    ->getEntity();
  $form['contact_storage_submit_text'] = [
    '#type' => 'textfield',
    '#title' => t('Submit button text'),
    '#description' => t("Override the submit button's default <em>Send message</em> text."),
    '#default_value' => $contact_form
      ->getThirdPartySetting('contact_storage', 'submit_text', t('Send message')),
  ];
  $form['contact_storage_url_alias'] = [
    '#type' => 'textfield',
    '#title' => t('Add URL alias'),
    '#description' => t('Optionally add an URL alias to the contact form.'),
  ];
  if (!$contact_form
    ->isNew()) {
    $aliases = \Drupal::entityTypeManager()
      ->getStorage('path_alias')
      ->loadByProperties([
      'path' => '/' . $contact_form
        ->toUrl()
        ->getInternalPath(),
    ]);
    if ($aliases) {

      /** @var \Drupal\path_alias\PathAliasInterface $alias */
      $alias = reset($aliases);
      $form_state
        ->set('path_alias_id', $alias
        ->id());
      $form['contact_storage_url_alias']['#default_value'] = $alias
        ->getAlias();
    }
  }
  $form['contact_storage_disabled_form_message'] = [
    '#type' => 'textfield',
    '#title' => t('Default disabled contact form message'),
    '#description' => t('Default message to display if the contact form is disabled.'),
    '#default_value' => $contact_form
      ->getThirdPartySetting('contact_storage', 'disabled_form_message', t('This contact form has been disabled.')),
  ];
  $form['contact_storage_preview'] = [
    '#type' => 'checkbox',
    '#title' => t('Allow preview'),
    '#description' => t('Show the preview button?'),
    '#default_value' => $contact_form
      ->getThirdPartySetting('contact_storage', 'show_preview', TRUE),
  ];
  $form['contact_storage_maximum_submissions_user'] = [
    '#type' => 'textfield',
    '#title' => t('Maximum submissions'),
    '#description' => t('The maximum number of times, per user, the form can be submitted (0 for unlimited).'),
    '#default_value' => $contact_form
      ->getThirdPartySetting('contact_storage', 'maximum_submissions_user', 0),
  ];

  // Overrides the 'reply' field provided by Core with a formattable field. If
  // html e-mails are disabled, enforce plain text format.
  $form['reply']['#type'] = 'text_format';
  if (!\Drupal::config('contact_storage.settings')
    ->get('send_html')) {
    $form['reply']['#allowed_formats'] = [
      'plain_text',
    ];
  }
  else {
    $form['reply']['#format'] = $contact_form
      ->getThirdPartySetting('contact_storage', 'page_autoreply_format', 'plain_text');

    // Explicitly set the allowed formats so that the fallback format is not
    // removed. That allows to prevent any formatting and is the default option,
    // without setting it, this option would go away after selecting something
    // else.
    $formats = filter_formats(\Drupal::currentUser());
    $form['reply']['#allowed_formats'] = array_keys($formats);
  }
  $form['#entity_builders'][] = 'contact_storage_contact_form_form_builder';
  $form['#validate'][] = 'contact_storage_contact_form_form_validate';
  $form['actions']['submit']['#submit'][] = 'contact_storage_contact_form_form_submit';
}