3

I am looking for away to manipulate and record data about an entry before it is returned to the client. I could do this with AJAX after the page is rendered by the browser but I would prefer to do this on the server.

Is there an event or hook I can use to accomplish this?

1

The full list of events that Craft provides is documented here.

There currently isn't onBeforeRender or onRender events, but it is on our list with no particular ETA. It gets tricky because it's difficult to distinguish between the main template and any other template that might happen to call TemplateService->render() (includes, etc.).

|improve this answer|||||
  • What about an onRequest event. Basically looking for any entry point where I can run some business logic. – David A McInnis Dec 15 '14 at 22:46
  • Could I use the onLoadPluguns event? – David A McInnis Dec 15 '14 at 22:49
  • Yii does have an onBeginRequest (yiiframework.com/doc/api/1.1/CApplication#onBeginRequest-detail), but both of those you listed will be fired on every request regardless of whether a template will render or not. – Brad Bell Dec 15 '14 at 23:21
  • Forgive me if I'm way off, but the template is essentially being rendered on the server before it ever gets delivered to the client. Can't you call a service method (via a template variable wrapper) to do your pre-prossessing? – Douglas McDonald Dec 15 '14 at 23:38
  • Yes... I'm just saying those events will get fired on every request, regardless of if a template would be rendered or not. An onBeforeRender/onRender event would imply a guarantee that a template would be rendered as well as some context as to which template was going to be/was rendered. – Brad Bell Dec 15 '14 at 23:43
0

Based off the recommendations I am going to use the onLoadPlugins event. I can limit its execution of business logic based on the URL construct. At least until I get a better solution. This should work but assumes that the onLoadPlugins event fires with each page request.

Here is my code that appears to be working.

public function init() 
{
    craft()->on('plugins.onLoadPlugins', function(Event $event) {
       $pageUrl = craft()->request->getSegments(); 
       if ( $pageUrl[0] != 'page' ) {
            // if not /page then exit here
            return;
       }

       // place business logic here
       echo "you're in;
       exit;

    });
 }   
|improve this answer|||||
  • Those this work for you? does not seem to work for me .. I would like to be notified when an entry is going to be loaded – shanti Jan 25 '15 at 17:21
  • Yes, it did work for me. However I opted to write a twig extension instead. Cannot remember why I did that. Probably so that I could call it from random pages. Everything I need execute the code, I simply include {% entry.id | myTwigExtension %}. This might work for you too. Just include at top of page, then execute logic based on result. – David A McInnis Jan 30 '15 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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