Service

This module based on own service, that can done everything described below. The service can help you when you work from PHP. To call service just use:

$contact_tools = \Drupal::service('contact_tools');

After that you can call all methods.

getForm() and getFormAjax()

Those two method generate $form render array with specified contact form and return it for your further needs.

Parameters

Example

$contact_tools = \Drupal::service('contact_tools');

// Just loading default form.
$default_form = $contact_tools->getForm();

// Load feedback form with AJAX submit handler.
$feedback_ajax = $contact_tools->getFormAjax('feedback');

// Pass additional information to form. This value can be accesed during alter.
$default_form = $contact_tools->getForm('callback', ['page' => 'My page']);

This methods generate '#link' which will load form in the modal on click. You can change modal settings and pass query parameters with link for future use in form alter.

Parameters

Examples

$contact_tools = \Drupal::service('contact_tools');

// Link which open contact tools in modal without AJAX handler.
$feedback_in_modal = $contact_tools->createModalLinkAjax('Write to use!', 'feedback');

// Link which open contact form in modal with AJAX submit handler.
$callback_link = $contact_tools->createModalLinkAjax('Call me', 'callback');

// This link pass query parameters to controller, that can be used for your needs.
// Also set modal width to 300 and additional class to link 'request-support-button'.
// By pass nid in service, you can access it in hook_form_alter() hooks by
// \Drupal::request()->query->get('service') and do whatever you want with it. F.e.
// set this value and hide form field from user, that service name will be send with
// form, but user don't need to fill and see it.
$link_options = [
  'query' => [
    'service' => $node->id(),
  ],
  'attributes' => [
    // use-ajax class will be added anyway. You don't need to worry about it.
    'class' => ['request-support-button'],
    'data-dialog-options' => [
      'width' => 300,
    ]
  ],
];
$request_support = $contact_tools->createModalLinkAjax('Call me', 'callback', $link_options);