Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ExpressionEngine: Extending a core or third party module or plugin
<?php
class ExtendedChannel {
public __construct()
{
$this->EE = &get_instance();
}
public function extended_entries()
{
// Get params
$some_param = $this->EE->TMPL->fetch_param('some_param');
// Do something to get some custom data, in this case $entry_ids was an array of entry IDs
// ...
// Modify the template params
$entry_ids = implode('|', $entry_ids);
$this->EE->TMPL->tagparams['fixed_order'] = $entry_ids;
// Include the class if it isn't included
if(!class_exists('Channel'))
{
// First party modules or plugins
require_once(APPPATH.'modules/channel/mod.channel.php');
// Third party would be
// require_once(PATH_THIRD.'package/mod.package.php');
}
// Create instance of Package class and call the tag you want to extend
// It will use the parameters passed from the template, as well as
// anything you have modified in tagparams
$C = new Channel();
$this->return_data = $C->entries();
return $this->return_data;
}
}
@airways

This comment has been minimized.

Copy link
Owner Author

@airways airways commented Oct 28, 2011

Of course this won't work in every situation as it requires that a parameter or set of parameters to the existing tag can accomplish what you want, but it can help simplify complex bits of template code and make things a lot more efficient if you are doing a lot of work just to pass off the final query to an existing module or plugin.

The fixed_order parameter is particularly useful for extending the {exp:channel:entries} tag since it allows you to specify an exact set of entries to return without any further work. You can do all kinds of crazy stuff using this as a basis.

@amacneil

This comment has been minimized.

Copy link

@amacneil amacneil commented Oct 28, 2011

Hey, that's cool. I have been dynamically outputting a channel entries tag, which was then parsed again by EE. I bet your way is more efficient.

@airways

This comment has been minimized.

Copy link
Owner Author

@airways airways commented Oct 28, 2011

Yes, it would be a tiny bit more efficient to call the class directly the way I am doing. The main difference would be in the regex to find the generated {exp:channel:entries} tag and parse it's parameters.

I feel like calling the class directly is much more testable and easier to work with however, even if it is only a bit faster.

@stevegrossi

This comment has been minimized.

Copy link

@stevegrossi stevegrossi commented Oct 30, 2014

This is really helpful, thanks for posting! However, I notice that when modifying TMPL->tagparams array, EE seems to ignore search params. For example,

$this->EE->TMPL->tagparams['search:title'] = "foo";

Still returns entries without "foo" in the title. I know it's been a while since you posted this, but if in your experience you've ever come across this issue, I'd love to know how you got around it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment