Single Pages not properly loading block controller's on_page_view method

Permalink
In C5 v5.4.2.2 I'm having a problem with an image gallery block when it is being displayed on the page_not_found page. But ONLY when C5 is re-directing from an invalid URL. Loading the page_not_found page directly works. Look at the middle of the footer on the following pages:

Invalid URL Redirect:http://tinyurl.com/6oez4wq

Direct URL to Page Not Found:http://tinyurl.com/7xg3vmd

When C5 redirects the user to the page_not_found page, it appears it is not triggering the "addHeaderItem" lines in the "on_page_view" method of the gallery block's controller.

I've even tried hard-coding the calls for the FancyBox CSS and JS into the header/footer of the page_not_found.php page... but that didn't help.

You can read more about it in the support thread for the Simple Image Gallery Block here:http://www.concrete5.org/index.php?cID=281733&editmode=...

Has anyone else had a similar problem (and, hopefully, a solution)?

Thanks!

- John

arrestingdevelopment
 
mkly replied on at Permalink Reply
mkly
oooohhh. Ya, this makes sense. The reason, I speculate, is that page_not_found is rendered within the request, not redirected to. You'll notice this when, for example, you try to access a restricted page and get the login but the URL is the page you were trying to get to. Therefore the on_page_view will be called for the page you were trying to get to, which doesn't exist, so you get nothing.

Sooo, I'm sure you would just want to fix it. The good part is that this is in the footer so it's going to be for every page. My favorite hack for this is
$bt = BlockType::getByHandle('blocks_handle');
$btc = $bt->getController();
ob_start(); // isn't that pretty
$btc->on_page_view();
ob_end_clean();

Place that above
Loader::element('header_required');

And you'll get any on_page_view stuff called, reasonably future proofed, and pretty hacky.

The reason for the ob_start() nonsense is that some blocks try to do some wacky writing up here that it's prudent to defend against. It's known as output buffering, and it eats any echo's and prints and then end_clean just throws them in the garbage. I imagine @jordanlev's typically impeccable code it isn't probably needed, but a good habit for general cases.
arrestingdevelopment replied on at Permalink Reply
arrestingdevelopment
Thanks, mkly!

I tried doing what you said in my local MAMP environment, but no luck. I put that code into the "header.php" file that's called by every page... but any "invalid" URLs still showed the non-styled, non-lightboxed version of the gallery in the footer.

The "on_page_view" method for the Simple Image Gallery block looks like this:
public function on_page_view() {
   if ($this->enableLightbox) {
      $html = Loader::helper('html');            
      $bv = new BlockView();
      $bv->setBlockObject($this->getBlockObject());
      $this->addHeaderItem($html->css($bv->getBlockURL() . '/fancybox/jquery.fancybox-1.3.1.css'));
      $this->addHeaderItem($html->javascript($bv->getBlockURL() . '/fancybox/jquery.fancybox-1.3.1.pack.js'));
   }
}


So I'm wondering if the issue is that, because of the way the "page_not_found is rendered within the request", the "$this->enableLightbox" code for the on_page_view method isn't getting the correct reference to the block instance on the page and thus isn't rendering?

I tried modifying the code you suggested, by attempting to set the "enableLightbox" value to true like this:

<?php 
   $bt = BlockType::getByHandle('footer_simple_image_gallery');
   $btc = $bt->getController();
   $btc->set('enableLightbox', true);
   ob_start(); // isn't that pretty
   $btc->on_page_view();
   ob_end_clean();
?>


But that didn't work, either.

I'm beginning to think I may just need to re-design the footer for the page_not_found page so it doesn't have this gallery... but the stubborn part of me says there HAS to be an explanation for why it's not working... AND a solution!

Still stumped...

- John
mkly replied on at Permalink Reply
mkly
Oh the handle is the block type's handle not the block's handle.

Although, because of how the on_page_view() method is written you will need to do a little more code. This is the first on_page_view() I've seen that doesn't assume it may be called via static method. Bummer he didn't want to address that part of it in your support request.

Anyhow...
$bt = BlockType::getByHandle('simple_image_gallery');
$btc = $bt->getController();
$btc->enableLightbox = true;
$btc->on_page_view();


You can also just link to those js files in your header directly.
arrestingdevelopment replied on at Permalink Reply
arrestingdevelopment
DOH! Right... block TYPE handle, not the block's handle. Got it.

Corrected the code... but STILL no go. So I tried hard-coding the references to the Fancybox CSS and JS files into the header for the page_not_found.php page. STILL nothing! They show in the source code, but for some reason the instantiating script that gets put into the page by the block doesn't seem to be triggering.

ARRRRRRRRRRGGGGGGHHHHH! Even my "Plan B" of just hardcoding the references doesn't seem to work!

SHEESH! What gives here, LOL!?!

- John

P.S... Jordan DID apologize for not being able to direct more "bandwidth" to this, but he's swamped with work and just having moved, so he suggested I post here since it appeared to be an issue with the way C5 was rendering/redirecting the page_not_found single page.
mkly replied on at Permalink Reply
mkly
Oh, sorry, I wasn't downing on Jordan. That guy is epic. I was just talking about how he is probably more capable of solving this for you. Sorry it came off like that.

You can seriously just add those js/css files. In the header.php before header_required

$html = Loader::helper('html');
$this->addHeaderItem($html->css('/packages/simple_image_gallery/blocks/simple_image_gallery/fancybox/jquery.fancybox-1.3.1.css');
$this->addHeaderItem($html->javascript('/packages/simple_image_gallery/blocks/simple_image_gallery/fancybox/jquery.fancybox-1.3.1.pack.js');

Although this becomes much less upgrade proof.

I wish I could help more. It's tough on these second hand.
arrestingdevelopment replied on at Permalink Reply
arrestingdevelopment
No problem... didn't think you were downing on him... just didn't want anyone reading this thread (now or in the future) to think that Jordan wasn't being supportive. He was really doing his best but just had to cry "Uncle" when it didn't appear to be something that was directly related to his code and which very well might have had something to do with the C5 core and how it was rendering the single page. Hard to complain, especially since the "Simple Image Gallery" block is FREE! ;D AND since Jordan is, as you say it, EPIC!

I think I've finally got it working. The ONE piece that I was missing was the view.css for the block, which is where the very last piece of styling for the thumbnails was! Adding that to the hard-coded links in the header for the page_not_found.php file seems to have done it.

Not a fan of having those hard-coded, but not the end of the world, either. I'll just comment them so I remember why they're there 6 months from now. LOL!

Thanks, LOADS, for your help!!!!

- John
fastcrash replied on at Permalink Reply
fastcrash
so what is the final solution you did? just hardcoded it with addheaderitem()?

i check with this if its single page
if(!isset($c->ctHandle)){

about 'hard complain' with free addon, i think that is the price, no big deal
but it's still amazing to make this as 'free addon'
i'm too, working on free addon, hope can upload it soon, but ALAS i build it on 5.4 environment

just my feeling,
jordanlev is my 1st favorite member in c5, 2nd is mkly and 3rd is phallanx :)
arrestingdevelopment replied on at Permalink Reply
arrestingdevelopment
No... I ended up creating a custom "header-page-not-found.php" and including that in the "page_not_found.php" page in place of the standard include. In the "header-page-not-found.php" I just hard-coded the links to the files like this:

<link rel="stylesheet" type="text/css" media="screen, print" href="<?php print DIR_REL; ?>/blocks/footer_simple_image_gallery/fancybox/jquery.fancybox-1.3.1.css" />
   <script type="text/javascript" src="<?php print DIR_REL; ?>/blocks/footer_simple_image_gallery/fancybox/jquery.fancybox-1.3.1.pack.js" /></script>


And I moved the CSS for the block from its view.css into my theme's styles.css so that it's available everywhere instead of only where the block is inserted (there wasn't much CSS, so it wasn't a huge deal).

No matter what I tried, I couldn't get the "addHeaderItem()" approach to work in the page_not_found.php file. Adding it before the
<?php Loader::element('header_required'); ?>


line caused the entire page to stop loading and just appear white. And, now that I think of it, doesn't that have to do with when the "addHeaderItem()" method can/can't be invoked? By the time the page is being rendered for the browser, isn't it too late to call "addHeaderItem()"?

Anyway... it's working! And while I can't say I have a "favorite" contributor to these forums, there ARE tons of great people who are willing to take the time to help others. And THAT is priceless! :D

Thanks!

- John
fastcrash replied on at Permalink Reply
fastcrash
Okay, got it. thanks too -
i have a similiar issue like this with some addon, but not investigate further more

note: i click the link 'page not found' in your 1st post. the gallery popup not shown?