Score:0

Send hook preprocess_views_view results to custom form or services

pe flag

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 ?

Kevin avatar
in flag
This is not what preprocess hooks are for (calling and commuicating to other functions/methods). All you can do is set a config variable or cache item to store this, but it likely won't be accurate 100% of the time because you don't have many ways to tell one anonymous user from the other. Maybe store it in user temp store.. not sure.
pe flag
@Kevin Thank you for your comment. Indeed, I also realize that hooks not for that. Since the hook returned exactly what I wanted, I tried to use it but I have to figure something else out. Maybe programmatically run the search performed by the user by retrieving the parameters at the time of form submit.
pe flag
@Kevin I managed to do what I wanted by fetching the selection in javascript. I think I will delete the question because the topic is no longer relevant.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.