Created
May 17, 2023 18:05
-
-
Save Greg-Boggs/33b65a7a5b3773f5e226761c66c00223 to your computer and use it in GitHub Desktop.
run a drupal hook only once
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_form_alter(). | |
*/ | |
function search_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id) { | |
$requestStack = \Drupal::service('request_stack'); | |
// Get the current request object. | |
$request = $requestStack->getCurrentRequest(); | |
// Retrieve the alter count from the request attributes or initialize it to 0. | |
$alterCount = $request->attributes->get('search_alter_count', 0); | |
// Increment the alter count for each load of the alter hook. | |
$alterCount++; | |
$request->attributes->set('search_alter_count', $alterCount); | |
// Execute the code only on the second load of the alter hook. | |
if ($alterCount === 2) { | |
if ($form["#id"] == 'views-exposed-form-search-page-1') { | |
// my alter code here... | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment