Observers

Feed 13 posts, 6 voices

Aug 8, 2008 09:25
Avatar
38 posts

Hi all,

The Observer pattern is a good way to extend frog functionality with plugins. Thank you Philippe to have implemented it!

I just don't found a good list of all events that's can be observeable (this page is not totally up-to-date).

Here the actual (r162) list:

  • view_page_edit_plugins
    Can be used to add custom field at the bottom of the page edit view
    Parameters:

    • $page: the Page model object
  • view_page_edit_tabs
    Can be used to add a tab in the page edit view
    Parameters:

    • $page: the Page model object
  • view_page_edit_popup
    ?
    No parameter.

  • page_edit_before_save
    Can be used to perform special stuff before saving the page. No parameter.

  • page_not_found
    When a page is not found
    No parameter.

  • page_found
    just before displayed the page
    Parameters:

    • $page: the Page model object

I have some idea to improve this list:

First, it would be nice to have a reference to the page object in parameter when it is possible and when there are a sense (for example the page_edit_before_save event have no parameter).

It would be nice to have more observers too. Here some ideas:

  • page_add_before_save (with a &$page params)
  • page_delete (with an $id or $page params)
  • part_edit_before_save (with a &$part param)
  • page_requested (with a $url param) : I need an event like this in the frontend/main.php before trying to found a page. It would be useful to implement a cache plugin.

What do you think about that ?

 
Aug 8, 2008 09:36
Avatar
277 posts

These observers have been updated in the v0.9.3! The view_page_edit_popup has been edited. The view_page_edit_popup was replaced by view_page_edit_plugins, but the code was not updated. Your ideas are pretty good.

 
Aug 8, 2008 11:19
Avatar
390 posts

ho yea I like those kind of idea :)

and I will really impress to have a cache plugin too.

I add those observer right now !!

 
Aug 8, 2008 11:21
Avatar
390 posts

with the page_requested one, i think I will trow the unparsed $url ( with full query ) !! and the parsed one will stil be available with the CURRENT_URI constant.

 
Aug 8, 2008 11:26
Avatar
390 posts

and for the page_add_before_save I can't pass the page if it is not yet saved !!

 
Aug 8, 2008 11:28
Avatar
38 posts

Thanks! :)

I will release a first version of the file cache plugin soon. I just need to do more tests and small doc.

 
Aug 8, 2008 11:43
Avatar
390 posts

good :)

I have add those observer: page_add_after_save [$page], page_edit_after_save [$page], page_delete [$page], part_edit_after_save [$part], page_requested [$full_query_url]

 
Sep 25, 2008 18:04
Avatar
351 posts

Can anyone give me a brief code example for using page_add_after_save?

For example, emailing the page contents somewhere…

I’m interested in doing an Upcoming.org plugin so that events created in Frog can automatically be published to Upcoming….

 
Sep 26, 2008 02:58
Avatar
38 posts

Hi ricks,

Sure, it’s really easy :-) See this exemple:

// in your plugin index.php file
Observer::observe('page_edit_after_save', 'my_simple_observer');

// in the same file
function my_simple_observer($page)
{
    /* do want do you want */
   var_dump($page);
}

But perhaps, for your need, it’s better to use the ‘part_edit_after_save’ event:

Observer::observe('part_edit_after_save', 'my_simple_part_observer');

function my_simple_part_observer($part)
{
  if('body' == $part->name)
  {
     /* do something with the body content ($part->content or $part->content_html) */
     var_dump($part);
  }
}
 
Sep 26, 2008 04:03
Avatar
351 posts

awesome. thank you, gido!

 
Sep 26, 2008 11:33
Avatar
458 posts

@gido – I shamelessy stole your code example and placed it in the site’s Docs section in preperation for a more complete document about Observers… when I find the time.

 
Sep 26, 2008 12:14
Avatar
38 posts

@mvdkleijn – no problems ;)

I have some idea to extends the layout system and I want to know if it would be possible to add following observers in frog core:

  • view_page_edit_layout
  • layout_edit_after_save or layout_edit_before_save

It’s really not urgent, but it would be nice to have an implementation of these events in all main controllers in frog admin.

 
Sep 27, 2008 14:51
Avatar
15 posts

Maybe my problem fits in here.

I have form, I use Observer – page_found and problem is when form has errors, how can I serve those error variables in frontend?

Lets say:
bc.. $name = ‘’;
if(empty($name)) { $error = ‘Your name is null’; }

So how can I server this $error variable into frontend?

thanks..

edit// why codebox not working? damn..

 
Sep 27, 2008 15:16
Avatar
458 posts

Marko, you could use the following code to produce a red “flash” error message with the message you want:

<?php Flash::set('error','Your name is null'); ?>

and then just redirect back to the original form/page. It depends a lot on your specific situation.

p.s. the codebox wasn’t working because you didn’t prefix it with an empty line.. I think.