I am developing a feature that allows anonymous users to receive by email the result of a facets + searchAPI.
Facets and Search API work perfectly. The custom module that allows to send an email too. I have also created a custom form (FormAPi) to let user gives its mail and send the datas on submit.
To get the results of the facets search, I use the preprocess_views_view
in .module
file
function my_module_preprocess_views_view__VIEW_ID(array &$variables): void {
foreach ($variables['view']->result as $result) {
$node = $result->_object->getEntity();
$title = $node->get('title')->getValue()[0]['value'];
$custom_field = $node->get('custom_field')->getValue()[0]['value'];
}
}
Every time a facet is modified, I can see the result with a dump()
and everything is ok. Maybe this is not the right hook in my case but it returns what I expect.
However, I don't know how to pass the result of this hook to my custom form which is inserted in a custom block on the page with the facets results view.
When the user submits the form, I need to know what the hook previously returned in SendSearchForm()
public function submitForm(array &$form, FormStateInterface $form_state) {
// email entered by user
$email = $form_state->getValue('email');
// send email but need to send also my_module_preprocess_views_view__VIEW_ID results
$this->sendEmail($email, $facets_result);
}
Do you have a suggestion to do this properly with Drupal?
EDIT : I've seen than I could use function drupal_static
. Is it a good idea ?