Hello,
Is there any way to “embed” an exp:channel:entries tag into a module tag ?
Here is a quick example of what I want to do :
{exp:mymodule:search_results}
{exp:channel:entries}
{title} : {custom_field}
{/exp:channel:entries}
{/exp:mymodule:search_results}The aim is to keep all advantages of the channel module, while being able to fetch only the needed entries.
My first idea was to pass a list of entries ID to the module, but there may be a cleverest way to do that ?
I’ve looked at search module source code (which does something similar), but it create a new Channel class, and I’m not sure doing like this will keep all the channel module features (ie: pagination, switch variable, etc…).
Any help highly appreciated, thanks !
Finally I’ve found a solution to my problem, I post it here so maybe it could help somebody.
I’m curious, does someone at EllisLab can tell me if it’s a good way to work with channel entries please ?
Step 1 : Create a new class which extends Channel
<?php
if ( ! defined('EXT')) { exit('Invalid file request'); }
if( ! class_exists( 'Channel' )) { require_once PATH_MOD.'channel/mod.channel.php'; }
class Mymodule_entries extends Channel {
public $return_data;
private $channel = false;
private $url_title = false;
private $status = false;
private $category_group = false;
private $category = false;
private $group_id = false;
private $author_id = false;
public function __construct()
{
$this->EE =& get_instance();
parent::Channel();
$this->_params();
}
/** ----------------------------------------
/** Setup default parameters
/** ----------------------------------------*/
private function _params()
{
$this->channel = $this->EE->TMPL->fetch_param('channel');
$this->url_title = $this->EE->TMPL->fetch_param('url_title');
$this->status = $this->EE->TMPL->fetch_param('status', 'open');
$this->category_group = $this->EE->TMPL->fetch_param('category_group');
$this->category = $this->EE->TMPL->fetch_param('category');
$this->group_id = $this->EE->TMPL->fetch_param('group_id');
$this->author_id = $this->EE->TMPL->fetch_param('author_id');
}
/** ----------------------------------------
/** Set up the tagparams with the IDs of search results entries before running the channel:entries method
/** ----------------------------------------*/
public function entries($entry_array)
{
if(!is_array($entry_array))
return FALSE;
$entries = implode('|', $entry_array);
$this->EE->TMPL->tagparams['entry_id'] = $entries;
return parent::entries();
}
}
?>Step 2 : in your exp:mymodule:function simply load the class :
$channel = new Mymodule_entries;
return $channel->entries(array(1));And that’s all !
Just now found this after searching for extending the channel class. Thanks for posting your solution.
DMlogic made a blogpost explaining this a bit and options for altering tagdata before or after the channel module http://dmlogic.com/blog/extending-the-expression-engine-channel-module/
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.