function contact_ajax_contact_site_form_ajax_callback in Contact ajax 8
Ajax callback for contact form.
Parameters
array $form: The form.
\Drupal\core\Form\FormStateInterface $form_state: The current state of the form.
Return value
\Drupal\core\Ajax\AjaxResponse The ajax response.
1 string reference to 'contact_ajax_contact_site_form_ajax_callback'
- contact_ajax_form_contact_message_form_alter in ./
contact_ajax.module - Implements hook_form_FORM_ID_alter() for contact_form_form().
File
- ./
contact_ajax.module, line 232 - Overrides core contact form functionality
Code
function contact_ajax_contact_site_form_ajax_callback($form, FormStateInterface &$form_state) {
/** @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());
$confirmation_type = $contact_form
->getThirdPartySetting('contact_ajax', 'confirmation_type', FALSE);
$prefix_id = $contact_form
->getThirdPartySetting('contact_ajax', 'prefix_id', FALSE);
$element_id = $prefix_id ? $prefix_id : CONTACT_AJAX_PREFIX . $form_object
->getFormId();
// Used to display results of drupal_set_message() calls.
$messages = StatusMessages::renderMessages(NULL);
// Create AJAX Response object.
$response = new AjaxResponse();
$output = [
'#type' => 'container',
'#attributes' => [
'id' => $element_id,
],
];
// Remove prefix/suffix to avoid duplication.
unset($form['#prefix'], $form['#suffix']);
if (!$form_state
->getErrors()) {
switch ($confirmation_type) {
case CONTACT_AJAX_LOAD_FROM_URI:
// load_from_uri is configured, so load the node content.
$node_id = $contact_form
->getThirdPartySetting('contact_ajax', 'load_from_uri', FALSE);
$node = Node::load($node_id);
if ($node) {
$view_builder = \Drupal::entityTypeManager()
->getViewBuilder('node');
$renderarray = $view_builder
->view($node, 'full');
$output['#markup'] = \Drupal::service('renderer')
->renderRoot($renderarray);
}
break;
case CONTACT_AJAX_LOAD_FROM_MESSAGE:
// Load the custom message from the config field.
$confirmation_message = $contact_form
->getThirdPartySetting('contact_ajax', 'load_from_message', FALSE);
$output['#markup'] = $confirmation_message ? $confirmation_message['value'] : '';
break;
case CONTACT_AJAX_LOAD_CLEAN_FORM:
// Load the default status message and a clean form.
$output[] = $messages;
// Create a new contact message and set it as the form entity.
$bundle_key = $contact_message
->getEntityType()
->getKey('bundle');
$bundle = $contact_message
->bundle();
$new_message = \Drupal::service('entity_type.manager')
->getStorage('contact_message')
->create([
$bundle_key => $bundle,
]);
$form_object
->setEntity($new_message);
// Clear user input, skipping system items.
$input = $form_state
->getUserInput();
$keys = $form_state
->getCleanValueKeys();
$keys[] = 'ajax_page_state';
foreach ($input as $key => $item) {
if (!in_array($key, $keys) && substr($key, 0, 1) !== '_') {
unset($input[$key]);
}
}
$form_state
->setUserInput($input);
// Rebuild the form state values.
$form_state
->setRebuild();
$form_state
->setStorage([]);
$form = \Drupal::service('form_builder')
->rebuildForm($form['#form_id'], $form_state);
$output[] = $form;
break;
default:
// CONTACT_AJAX_LOAD_DEFAULT_MESSAGE
// Load the default status message only.
$output[] = $messages;
}
}
elseif ($form_state
->getErrors()) {
$output[] = $messages;
$output[] = $form;
}
$prefix = '#' . $element_id;
$render_here = $contact_form
->getThirdPartySetting('contact_ajax', 'render_selector', FALSE);
$render_selector = $render_here ? $render_here : $prefix;
if ($render_selector != $prefix) {
// If a custom selector is configured hide the form in its old position.
$response
->addCommand(new ReplaceCommand($prefix, ''));
$response
->addCommand(new HtmlCommand($render_selector, $output));
}
else {
$response
->addCommand(new ReplaceCommand($render_selector, $output));
}
if (\Drupal::moduleHandler()
->moduleExists('views')) {
$response
->addCommand(new ScrollTopCommand($render_selector));
}
return $response;
}