If you are needing to do this in a module or plugin I often find extending the Channel modules class and overriding the entries method to be a good option. You can do some pre and post process of params etc. before calling the parent classes entries method. Ex:
<?php
require_once APPPATH."modules/channel/mod.channel.php";
class My_module extends Channel {
/**
* entries
*
* @access public
* @param void
* @return void
*
**/
public function entries()
{
/* I usually wait to call the parent classes constructor till here as
putting it in the subclasses constructor adds extra processing that other methods/tags in the module
might not need.
*/
parent::Channel();
//you can do some pre processing here... you can pass along other params using the $this->EE->TMPL->tagparams array
//lets say we only ever want to return entries from a particular category
$this->EE->TMPL->tagparams['category'] = 82; //or whatever
//maybe you want to search a particular field
$this->EE->TMPL->search_fields['my_custom_field'] = '=y';
//then let the channel entries do all the heavy lifting...
$this->return_data = parent::entries();
//you can do post processing on the parsed tagdata data here.
//let say your client does not like the number 3 appearing on the site.
$this->return_data = str_replace('3', '#@$@^', $this->return_data);
return $this->return_data;
}
}
?>Depending on your use case this might be helpful or not.
Thank you for this very useful post, but I could do with more help.
I am writing a module in which I need to intercept all channel entries in order to modify the parameters and the tag data before rendering.
Since I have no control over what might actually be in each template/entry before I get to it, I assume that I have to make use of an extension - so far so good.
I have found it relatively easy to modify the tag data exactly as I need to using the channel_entries_query_result hook.
But I am having a devil of a time affecting the parameters. The channel_entries_query_result hook calls my code, which simplified, looks like this:
<?php
function entry()
{
$this->EE->TMPL->tagparams['group_id'] = 99; // This does not work
$this->EE->TMPL->tagdata = str_replace('9', '31', $this->EE->TMPL->tagdata); // This works
}I can echo out the $this->EE->TMPL->tagparams[‘group_id’] both before and after modification - just can’t get it into the final result.
What the heck??
To restate - I am trying to use what was written by the3mus1can earlier,
extending the Channel modules class and overriding the entries method
but with a twist.
Rather than triggering a module method via the module’s tags in a template, I was hoping to be able to trigger the method via a hook, essentially using the hook without necessarily using the hook’s intended functionality so much as the fact that it launches my method without me having to have my module’s tags in the template in question to do that.
The reason for all this, is as stated in my earlier post, because I have no control over the template contents, but I need to be able to modify the template parameters to affect the filtering of the results.
EDIT: I forgot to add that this means I should not be using the channel_entries_query_result hook because of infinite recursion problems. Now looking for another way.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.