Executing on_page_view() for every block in a Page List
Permalink
I have a Page List with "Blog index" as custom template.
It appears that Page List does not call BlockController::on_page_view() for
every block that is rendered.
I have solved it by overwriting PageListBlockController::on_page_view() by this way:
Is there a better approach to solve this?
It appears that Page List does not call BlockController::on_page_view() for
every block that is rendered.
I have solved it by overwriting PageListBlockController::on_page_view() by this way:
public function on_page_view() { $cArray = $this->getPages(); foreach($cArray as $page) { $blocks = $page->getBlocks('Main'); foreach ($blocks as $b){ $b->display(); $controller = $b->getController(); if(method_exists($controller, "on_page_view")) { $controller->on_page_view(); } } } }
Is there a better approach to solve this?
Sorry, I mean of course override, just my english isn't very well.
I have blocks that shows some pictures, this blocks to be rendered properly must add some js files and they are added by calling addHeaderItem in BlockController::on_page_view (as C5 documentation recommends).
Example:
Page list:http://wirdil.c5host.com/index.php/blog/...
Blog entry:http://wirdil.c5host.com/index.php/blog/cos-jest-na-rzeczy/...
"Blog entry" have sortable_fancybox_gallery javascript files but "Page list" doesn't have these files.
I have blocks that shows some pictures, this blocks to be rendered properly must add some js files and they are added by calling addHeaderItem in BlockController::on_page_view (as C5 documentation recommends).
Example:
Page list:http://wirdil.c5host.com/index.php/blog/...
Blog entry:http://wirdil.c5host.com/index.php/blog/cos-jest-na-rzeczy/...
"Blog entry" have sortable_fancybox_gallery javascript files but "Page list" doesn't have these files.
I think your solution is the only way to do it. Note that some blocks also have header items that are outputted automatically (not from the on_page_view() function) -- the view.js and view.css files, as well as js files in the block /js directory and css files in the block /css directory.
Look at the code in the "outputAutoHeaderItems" function of the block controller in the free addon "Global Areas" to see some example code that outputs these (http://www.concrete5.org/marketplace/addons/global-areas... ).
I don't know if it's efficient or not... it is probably okay if you are limiting the page list blocks to a reasonable number (3, 5, maybe 10)... but probably not okay if you're showing 50 or 100 pages. You also will want to be careful that you don't have a page list block on one of those blog pages, because then you might get caught in an infinite loop (one page list calls the other page list's on_page_view function, and then that page list calls another's on_page_view, etc.).
Look at the code in the "outputAutoHeaderItems" function of the block controller in the free addon "Global Areas" to see some example code that outputs these (http://www.concrete5.org/marketplace/addons/global-areas... ).
I don't know if it's efficient or not... it is probably okay if you are limiting the page list blocks to a reasonable number (3, 5, maybe 10)... but probably not okay if you're showing 50 or 100 pages. You also will want to be careful that you don't have a page list block on one of those blog pages, because then you might get caught in an infinite loop (one page list calls the other page list's on_page_view function, and then that page list calls another's on_page_view, etc.).
Thank you, this example helped me a lot!
- You should 'override' rather than over-write. That way it won't all fall apart next time you upgrade c5. There are several howtos on this including some recent docs specific to the special features c5.6 now has for overrides.
- This is a potentially very expensive piece of code and could slow a site down massively. Do you really need all blocks on a page to run their on_page_view handlers just for a page list? All the referenced filed to be loaded?