We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Loop Channel Entries Programmatically

Development and Programming

emorling's avatar
emorling
66 posts
14 years ago
emorling's avatar emorling

How do I loop channel entries programmatically in a add-on? There doesn’t seem to be any “SELECT” functions in the channel API.

       
the3mus1can's avatar
the3mus1can
426 posts
14 years ago
the3mus1can's avatar the3mus1can

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.

       
JohnD's avatar
JohnD
114 posts
13 years ago
JohnD's avatar JohnD

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??

       
the3mus1can's avatar
the3mus1can
426 posts
13 years ago
the3mus1can's avatar the3mus1can

Do you want the group_id to filter the results or do you want it to be returned in the query data?

       
JohnD's avatar
JohnD
114 posts
13 years ago
JohnD's avatar JohnD

@the3mus1can - thank you for responding.

Do you want the group_id to filter the results or do you want it to be returned in the query data?

I want to filter the result.

       
JohnD's avatar
JohnD
114 posts
13 years ago
JohnD's avatar JohnD

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.

       
the3mus1can's avatar
the3mus1can
426 posts
13 years ago
the3mus1can's avatar the3mus1can

I don’t think that it’s going to be possible. There is not an extension hook in the channel module that gets run before it builds the sql query. EL would need to add a channel_module_entries_start hook to get it to work.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.