You are here

function contact_storage_form_contact_message_form_alter in Contact Storage 8

Implements hook_form_FORM_ID_alter() for contact_form_form().

File

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

Code

function contact_storage_form_contact_message_form_alter(&$form, &$form_state, $form_id) {

  /** @var \Drupal\Core\Entity\ContentEntityForm $form_object */
  $form_object = $form_state
    ->getFormObject();

  /* @var \Drupal\contact\MessageInterface $contact_message */
  $contact_message = $form_object
    ->getEntity();
  $contact_form = ContactForm::load($contact_message
    ->bundle());

  /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_mode */
  if ($form_object instanceof MessageForm) {
    if ($submit_text = $contact_form
      ->getThirdPartySetting('contact_storage', 'submit_text', FALSE)) {
      $form['actions']['submit']['#value'] = $submit_text;
    }
    if (!$contact_form
      ->getThirdPartySetting('contact_storage', 'show_preview', TRUE)) {
      $form['actions']['preview']['#access'] = FALSE;
    }
  }

  // Check if the current user has reached the form's maximum submission limit.
  $maximum_submissions_user = $contact_form
    ->getThirdPartySetting('contact_storage', 'maximum_submissions_user', 0);
  if ($maximum_submissions_user !== 0 && contact_storage_maximum_submissions_user($contact_form) >= $maximum_submissions_user) {

    // Sets the error message.
    $form['maximum_submissions_error'] = [
      '#type' => 'container',
      '#markup' => t('You have reached the maximum submission limit of @limit for this form.', [
        '@limit' => $maximum_submissions_user,
      ]),
      '#attributes' => [
        'class' => [
          'messages',
          'messages--error',
        ],
      ],
      '#weight' => -100,
    ];

    // Remove the submit and preview buttons.
    $form['actions']['submit']['#access'] = FALSE;
    $form['actions']['preview']['#access'] = FALSE;
  }
}