i need to modify the submit function of the module custom_search (8.x-1.0-beta3).

I need to set a Cookie every time the guest user search something, how can i override or extend the submit function?

It looks like not represent a hook. The function is: custom_search_search_page_submit located in the file custom_search.module and called by the custom_search_form_alter function that implements hook_form_alter()

 * Search pages settings form callback.
function custom_search_search_page_submit($form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $pageId = $form_state->getValue('id');
  // Save settings, and path as well so we can use it to retrieve the correct settings later.
  \Drupal::configFactory()->getEditable('custom_search.settings.results')->set($pageId . '.path', $form_state->getValue('path')) 
    ->set($pageId . '.search', $form_state->getValue('custom_search_search'))
    ->set($pageId . '.advanced.visibility', $form_state->getValue('custom_search_advanced_search'))
    ->set($pageId . '.advanced.collapsible', $form_state->getValue('custom_search_collapsible'))
    ->set($pageId . '.advanced.collapsed', $form_state->getValue('custom_search_collapsed'))
    ->set($pageId . '.advanced.criteria', $form_state->getValue('custom_search_criteria'))
    ->set($pageId . '.advanced.types', $form_state->getValue('custom_search_types'))
    ->set($pageId . '.advanced.languages', $form_state->getValue('custom_search_languages'))
    ->set($pageId . '.info', $form_state->getValue('custom_search_info'))
    ->set($pageId . '.filter.position', $form_state->getValue('custom_search_filter_position'))
    ->set($pageId . '.filter.label', $form_state->getValue('custom_search_filter_label'))
    ->set($pageId . '.filter.any', $form_state->getValue('custom_search_filter_any'))

2 Answers 2


You don't need to override the submit function. Each module or theme can do exactly the same as the custom search module does. Use a form alter hook and add their own submit function to the form array. On submit they are each executed one after another.


 * Implements hook_form_alter().
function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id == 'form_id') {
    $form['actions']['submit']['#submit'][] = 'mymodule_search_page_submit';

function mymodule_search_page_submit($form, \Drupal\Core\Form\FormStateInterface $form_state) {
  // set cookie
  • I need to create a new module? Or i can place the above code somewhere in my theme?
    – Lenin Meza
    Dec 1, 2016 at 19:26
  • 2
    This belongs in a custom module. It's best to maintain the separation between code that controls how your site works (modules) and code that controls how your site looks (theme). Dec 1, 2016 at 20:00
  • @LeninMeza: This works both in modules and themes. In a theme you put the code in mytheme.theme. I agree with marcvangend, a module is the better place.
    – 4uk4
    Dec 1, 2016 at 20:07

I ended up with the next code on my own module, thanks to @4k4 and @marcvangend It works like a charm!!


 * @file
 * Bring customizations to the default core functions
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Datetime\DrupalDateTime;

 * Implements hook_form_alter().
function my_module_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'search_form') {
    $form['#submit'][] = 'my_module_search_page_submit';

function my_module_search_page_submit($form, FormStateInterface $form_state) {
  $form_values = $form_state->getValues();
  $date = new DrupalDateTime();
  user_cookie_save(['search_form-'.$date->format('Y-m-d_H-i-s') => $form_values['keys'] ]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.