1

I am working on an addon for EE3 and would like to be able to pull whatever the current {entry_id} is to use inside the mod file without requiring the user to have to add it as a param.

Is this something that can be done? I feel like I have gone in circles through the docs as well as printing objects, etc...

I know I could do something like this:

{exp:my_module:my_method entry_id="{entry_id}"}

I would just like them to be able to use:

{exp:my_module:my_method}

and whatever optional params are wanted if need be.

Thanks

* EDIT *

@derek-jones pointed me in the right direction by sending me to the channel_entries_row in the Channel Module Extension Hooks here.

However, after reading through it, AND being new to hooks and how to use them, I am still unsure as to how to retrieve the data I am after. Any help in this would be greatly appreciated.

I created an extension for my module and in my hooks, I included it in the extension like this:

public $hooks = array('channel_entries_row');

(I placed it into an array so I can loop through all I end up using and adding them in one statement. I know it looks off here.)

So according to the docs...

How it’s called:

$row = $this->extensions->call('channel_entries_row', $this, $row);
if ($this->extensions->end_script === TRUE) return $tagdata;

So do I need to create a method inside my extension named the same as the hook? or create my own method and just call the hook?

So something like this?

public function channel_entries_row($this, $row)
{
    // Check if we're not the only one using this hook
    if (ee()->extensions->last_call !== FALSE) {
        $config = ee()->extensions->last_call;
    }

    // build the rows so we can get the entry_id
    $row = $this->extensions->call('channel_entries_row', $this, $row);

    echo "<pre>";
    print_r($row);
    echo "</pre>";
    exit;

    // The above are just there for trying to test right now...

    if ($this->extensions->end_script === TRUE) return $tagdata;
}

...and then call it from my module when needed?

Just trying to understand a bit here. I don't know if I need more coffee or what. ;)

2 Answers 2

0

The "how it's called" refers to how the application, ExpressionEngine, calls the hook, so you can see if your hook can return anything or if end_script can be set to end processing early. You should really never need to call your own hook unless your code is setup to be reusable in that way. You should just need to activate your hook, and then ExpressionEngine will call it when the event arises. So assuming you've successfully activated your hook (added your hook to the extensions table), the method you specify in your extension class, whether it be called channel_entries_row or whatever you call your method (doesn't have to be the same as the hook), will get called with the documented parameters for the hook. If the hook docs specify a return type, it tells you what your hook should return. For example, channel_entries_row allows you to modify the entry's data before it is printed to the screen, so you'd modify the $row variable and return it from your hook method. Hope this helps.

5
  • Thanks Kevin. (I really appreciate the clarifications) Just to clarify a touch further though... You're saying that with the hook activated, and in this case, specifying a return array of $row, I can simply utilize $row as an array??? In other words, whatever my method is, which doesn't need to be called just specified, I can reference $row and manipulate the data before returning? (That's how I am understanding it anyway) So.... how would you tie that in to being used side-by-side with a module? Or is that pushing this Q&A too far?
    – W3bGuy
    Sep 12, 2016 at 16:57
  • Yes $row is just an array and you can do whatever you want with it. Unfortunately it doesn't really tie in to being used by a module how you're wanting. Extensions and modules are typically used for different reasons and aren't really meant to work together in this way. Yes they can be part of the same add-on but they typically serve different functions of the add-ons overall purpose. Extensions just handle outside events, in the case of channel_entries_row it's the event of a channel entry being parsed, and all of that will happen for each entry before your module tag is even called once.
    – Kevin Cupp
    Sep 12, 2016 at 17:03
  • Thanks... Accepting as answer being you explained the process in a further scope for overall understanding. ;)
    – W3bGuy
    Sep 12, 2016 at 17:04
  • Sure thing. If you'd like to talk more in a discussion format, feel free to post in the dev preview forums or message us in Slack.
    – Kevin Cupp
    Sep 12, 2016 at 17:44
  • Thanks Kevin. I posted a ? in eecms.slack.com/messages/programming
    – W3bGuy
    Sep 12, 2016 at 18:46
0

To fetch the data, you must have to pass the data. You should go with option 1

{exp:my_module:my_method entry_id="{entry_id}"}

You can fetch the entry id from your method by this code.

$entry_id = $this->EE->TMPL->fetch_param('entry_id');
3
  • 1
    I know I can do that... It would actually be like this though: $entry_id = ee()->TMPL->fetch_param('entry_id'); I am looking for another way though...
    – W3bGuy
    Sep 9, 2016 at 6:06
  • Well in that case, If you pass URL title in segment, you can fetch the segment in your module and through URL title, you can find entry id Sep 9, 2016 at 6:32
  • Yea, but that won't work in this case because dynamic="no". This is a loop through a channel for all entries in the channel.
    – W3bGuy
    Sep 9, 2016 at 6:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.