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

[SOLVED] Using the channel module within an addon?

Development and Programming

Rick Jolly's avatar
Rick Jolly
729 posts
15 years ago
Rick Jolly's avatar Rick Jolly

I’d like to programmatically get channel entries from within a plugin, outside of the template in EE 2. The channel api seems to only handle create/update/delete operations, but I’d like to retrieve entries based on parameters. I found some code for EE 1.6 using the Weblog module on slide 70 of a solspace presentation:http://www.slideshare.net/reedmaniac/addon-development-ee-expects-that-every-developer-will-do-his-duty-2494809?src=related_normal&rel=4412482. Is that code applicable to the Channel class in EE 2? Is there another way?

       
Chuck Liddell's avatar
Chuck Liddell
57 posts
15 years ago
Chuck Liddell's avatar Chuck Liddell

Take a look at the model class ‘channel_entries_model’, you can find it in expressionengine/models/channel_entries_model.php.

It has functions like get_entry( entry_id, channel_id, autosave ), which I think will fit the bill for what you need.

You can call it like this:

$query_result = $this->EE->channel_entries_model->get_entry( $entry_id, $channel_id );
if( $query_result->num_rows() > 0 )
{
  foreach( $query_result->result() as $entry )
  {
    // etc, etc
  }
}

To understand query result hydration better and know what your options are, check out the CodeIgniter Documentation.

       
Chuck Liddell's avatar
Chuck Liddell
57 posts
15 years ago
Chuck Liddell's avatar Chuck Liddell

If you’re more interested in retrieving an entry or list of entries based on some parameters (and it looks like this might be your case), you can use a plugin and a custom tag to wrap the channel:entries tag (so you can call exp:my_plugin:entries, which in turn calls exp:channel:entries after doing some custom processing / modifications ).

It’s as simple as this:

/**
 * Wraps the channel:entries tag and provides additional
 * functionality.
 */
function entries()
{
  // use code like this to grab passed tag parameters
  $time_window = $this->EE->TMPL->fetch_param( 'time_window' ) );

  // use code like this to set tag parameters for channel:entries to see
  $this->EE->TMPL->tagparams[ 'status' ] = 'closed';


  // now it's time to call channel->entries() to do the heavy lifting
  if( !class_exists( 'channel' ) )
    require_once PATH_MOD . "channel/mod.channel" . EXT;
    
  $class = new Channel();
  return $class->entries();
}
       
Rick Jolly's avatar
Rick Jolly
729 posts
15 years ago
Rick Jolly's avatar Rick Jolly

Excellent Chuck. Thanks!

       
evilsee's avatar
evilsee
4 posts
14 years ago
evilsee's avatar evilsee

I found the channel_entries_model->get_entry() function, but what I dont understand is why you need to pass it a channel id, if entry id’s are unique.

So to get an entries data you have to call the function twice, first to get the entries channel ID and then to get the entry data.

I guess you could write your own select statement, but I like using existing functions, does anyone know why this function is written like this?

       

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.