Need a parameter in an integration hook

Started by reslava, September 11, 2022, 12:13:55 PM

Previous topic - Next topic

reslava

Hello,

I am developing a new mod to do some filtering in posts of a topic by using hooks.

I already achived what I want, but for using a hook I need a parameter that is nor passed to the hook function.

I detail it here:

On Display.php line 910 we find:
$posters = array_unique($all_posters);

call_integration_hook('integrate_display_message_list', array(&$messages, &$posters));

I also need $all_posters as a parameter of 'integrate_display_message_list' in order to know who wrote the post and play with filters.

Please, could someone help how to solve this?
Thank you very much in advance for your help!

PS.- I did not know where to post this, if this is not the place then I am sorry and please move it.

Doug Heffernan

What is it that you are trying to accomplish? Can you post the relevant code? I am asking because I am not sure that I understand what you mean.

You do not need the $all_posters as a parameter in the aforementioned hook function imo. You can use the already exisitng $posters parameter in your mod, which contains the output of the $all_posters assigned to it with the array_unique() function, which takes an input array and returns a new array without duplicate values.

http://php.net/manual/en/function.array-unique.php

Quote from: reslava on September 11, 2022, 12:13:55 PMPS.- I did not know where to post this, if this is not the place then I am sorry and please move it.

No worries. I moved it to the right board.

reslava

Hello Doug,

First of all, thank you very much for your quick answer and moving the post to the irght place.

I am trying to filter a topic and show only post written by a poster(s) selected.

With $posters I only have the different and unique Ids of posters of the topic, but in order to know who wrote every msg, this info is in $all_posters variable.

Of course I can paste the code here:
    $posters = array_unique($all_posters);

    call_integration_hook('integrate_display_message_list', array(&$messages, &$posters));

    $filterByPoster = 2; // id of the poster to filter by
    global $all_postersFiltered;
    $posters = array($filterByPoster);
    $all_postersFiltered = array_filter($all_posters, function($value) {               
        return $value == $filterByPoster; 
    });

    $all_posters = $all_postersFiltered;
   
    $messagesFiltered = array_filter($messages, function($value) {               
        global $all_postersFiltered;
        return array_key_exists($value, $all_postersFiltered);
    });

    $messages = array_values($messagesFiltered);

Not good code I know, I am a begginer with PHP and SMF customizing.

Thank you very much in advance!

Diego Andrés

Since you have $messages, you can do another small query on your function/method to get all the member id's from those messages, and then use them for whatever you are trying to accomplish.

It doesn't sound ideal, but it's probably the best option to avoid doing file edits.

SMF Tricks - Free & Premium Responsive Themes for SMF.

reslava

#4
  Thanks so much Diego! I will try that way.

  But maybe passing $all_posters to that hook in the future will be useful for some tasks and preventing to do more queries.

  Of course, I am a complete begginer learning how all pieces of this great forum software are designed.

  I take this opportunity to express my gratitude to all developers of SMF, thank you very much all!

Doug Heffernan

Quote from: reslava on September 11, 2022, 12:42:03 PMHello Doug,

First of all, thank you very much for your quick answer and moving the post to the irght place.

No problem :)

Regarding the code, if you are going to package this as a mod, you should remove the hook function from the code and create a function of your own to call the integrate_display_message_list hook.

Advertisement: