You are here

function contact_ajax_form_contact_form_form_alter in Contact ajax 8

Implements hook_form_FORM_ID_alter() for contact_form_form().

File

./contact_ajax.module, line 50
Overrides core contact form functionality

Code

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

  /** @var \Drupal\contact\ContactFormInterface $contact_form */
  $contact_form = $form_state
    ->getFormObject()
    ->getEntity();
  $form['contact_ajax'] = [
    '#type' => 'fieldset',
    '#title' => t('Contact ajax'),
  ];
  $form['contact_ajax']['contact_ajax_enabled'] = [
    '#type' => 'checkbox',
    '#title' => t('Ajax Form'),
    '#description' => t('Send this form using ajax.'),
    '#default_value' => $contact_form
      ->getThirdPartySetting('contact_ajax', 'enabled', FALSE),
  ];
  $form['contact_ajax']['contact_ajax_confirmation_type'] = [
    '#type' => 'select',
    '#title' => t('On submit load'),
    '#description' => t('Select which content have to be loaded after submit.'),
    '#options' => [
      CONTACT_AJAX_LOAD_DEFAULT_MESSAGE => t('Default message'),
      CONTACT_AJAX_LOAD_CLEAN_FORM => t('Default message and empty form'),
      CONTACT_AJAX_LOAD_FROM_URI => t('Node content'),
      CONTACT_AJAX_LOAD_FROM_MESSAGE => t('Custom message'),
    ],
    '#default_value' => $contact_form
      ->getThirdPartySetting('contact_ajax', 'confirmation_type', FALSE),
    '#states' => [
      'visible' => [
        ':input[name="contact_ajax_enabled"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];

  // Default entity_autocomplete get a node object as default value.
  $node_id = $contact_form
    ->getThirdPartySetting('contact_ajax', 'load_from_uri', FALSE);
  $node = $node_id ? Node::load($node_id) : FALSE;
  $form['contact_ajax']['contact_ajax_load_from_uri'] = [
    '#type' => 'entity_autocomplete',
    '#target_type' => 'node',
    '#default_value' => $node,
    '#title' => t('Node to load'),
    '#description' => t('This node will load its content after the form Submit.'),
    '#states' => [
      'visible' => [
        ':input[name="contact_ajax_enabled"]' => [
          'checked' => TRUE,
        ],
        ':input[name="contact_ajax_confirmation_type"]' => [
          'value' => CONTACT_AJAX_LOAD_FROM_URI,
        ],
      ],
    ],
  ];
  $confirmation_message = $contact_form
    ->getThirdPartySetting('contact_ajax', 'load_from_message', FALSE);
  $confirmation_message_value = $confirmation_message ? $confirmation_message['value'] : '';
  $confirmation_message_format = $confirmation_message ? $confirmation_message['format'] : filter_default_format();
  $form['contact_ajax']['contact_ajax_load_from_message'] = [
    '#type' => 'text_format',
    '#title' => t('Message to load'),
    '#description' => t('This message will load after form submit.'),
    '#default_value' => $confirmation_message_value,
    '#format' => $confirmation_message_format,
    '#states' => [
      'visible' => [
        ':input[name="contact_ajax_enabled"]' => [
          'checked' => TRUE,
        ],
        ':input[name="contact_ajax_confirmation_type"]' => [
          'value' => CONTACT_AJAX_LOAD_FROM_MESSAGE,
        ],
      ],
    ],
  ];
  $form['contact_ajax']['advanced'] = [
    '#type' => 'details',
    '#title' => t('ADVANCED SETTINGS'),
    '#open' => FALSE,
    '#states' => [
      'visible' => [
        ':input[name="contact_ajax_enabled"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['contact_ajax']['advanced']['contact_ajax_prefix_id'] = [
    '#type' => 'textfield',
    '#title' => t('Prefix id'),
    '#description' => t('When contact ajax is enabled, a wrapper is created around the form, with this settings you can choose a custom ID from the HTML wrapper element. Leave empty if you don\'t need it'),
    '#default_value' => $contact_form
      ->getThirdPartySetting('contact_ajax', 'prefix_id', FALSE),
    '#states' => [
      'visible' => [
        ':input[name="contact_ajax_enabled"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['contact_ajax']['advanced']['contact_ajax_render_selector'] = [
    '#type' => 'textfield',
    '#title' => t('Render into this HTML element class/id'),
    '#description' => t('If you need to load the response into an element different from Prefix_id you can write here the class id. i.e : .render-here , #render-here.'),
    '#default_value' => $contact_form
      ->getThirdPartySetting('contact_ajax', 'render_selector', FALSE),
    '#states' => [
      'visible' => [
        ':input[name="contact_ajax_enabled"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['#entity_builders'][] = 'contact_ajax_contact_form_form_builder';
}