Show different content if page area is empty on Concrete 8.5.6

I’m using Concrete 8.5.6
I am trying to get a page to load different content on if a certain page area is empty or not.
This below, does work, but on;y on the main pages.
<?php $a1 = new Area(‘Extra Page Title’);
$a1->display();
$pageObject = Page::getCurrentPage();
$pageBlocks = $pageObject->getBlocks(‘Extra Page Title’);
$pageTitle = false;
foreach ($pageBlocks as $pb) {
if (!empty($pb->btHandle)) {
$pageTitle = true;
}
break;
}
echo ($pageTitle ? ‘+’ . $pageTitle . ‘-’ : ‘

’ . $c->getCollectionName() . ’ + ’ . $pageTitle . ’ -

’); ?>
This above, does work perfectly.

But now I’m trying to put this code in a page block/view.php
The page area called out “$a56 = new GlobalArea(‘Request Quote’)” in the view.php is on the page, so I was hoping it would work.

I have as follows:
$pageObject = Page::getCurrentPage();
$pageBlocks = $pageObject->getGlobalBlocks(‘Request Quote’);
$pageQuoteTitle = false;
foreach ($pageBlocks as $pb) {
if ($pb->btHandle = NULL) {
$pageQuoteTitle = true;
}
break;
}
echo ($pageQuoteTitle ? ‘


Please use the “Request a Quote” form


’ : ‘

Please use the “Request More Info” form

Try 1 (Not Working)
’);

This is all shown on the this link:
https://www.mytinytown.com/products/mercury-relay-30-amp-single-pole-24-vdc-coil-normally-open

In your second set of code you have

if ($pb->btHandle = NULL) {

which is assigning the value, not checking it. That should be something like this

if (empty($pb->btHandle)) {

Also, if you are just checking to see if the area is empty, why wouldn’t you just say something like this?

if(!is_array($pageBlocks) || count($pageBlocks) === 0){

Ok, I changed it to the following, with out luck:

$pageBlocks = $pageObject->getGlobalBlocks('Request Quote');
$pageQuoteTitle = false;
foreach ($pageBlocks as $pb) {
if (!is_array($pageBlocks) || count($pageBlocks) === 0 || empty($pb->btHandle)) {
$pageQuoteTitle = true;
}
break;
}
echo ($pageQuoteTitle ?  '<hr><h3>Please use the "Request a Quote" form</h3>(Success)<hr>' : '<hr><h3>Please use the "Request More Info" form</h3>(Not Working)<hr>');

Does it work if you do this

$pageBlocks = $pageObject->getGlobalBlocks('Request Quote');
$pageQuoteTitle = true;
if (!is_array($pageBlocks) || count($pageBlocks) === 0) {
$pageQuoteTitle = false;
}

echo ($pageQuoteTitle ?  '<hr><h3>Please use the "Request a Quote" form</h3>(Success)<hr>' : '<hr><h3>Please use the "Request More Info" form</h3>(Not Working)<hr>');

It did not. I have:

$pageBlocks = $pageObject->getGlobalBlocks('Request Quote');
$pageQuoteTitle = true;
if (!is_array($pageBlocks) || count($pageBlocks) === 0) {
	$pageQuoteTitle = true;
}
echo ($pageQuoteTitle ?  '<hr><h3>Please use the "Request a Quote" form</h3>(Success)<hr>' : '<hr><h3>Please use the "Request More Info" form</h3>(Not Working)<hr>');

https://www.mytinytown.com/products/mercury-relay-30-amp-single-pole-24-vdc-coil-normally-open
This page should say:
Please use the “Request a Quote” form

https://www.mytinytown.com/products/mini-mercury-hg-pilot-duty-control-float-switch-10-foot-normally-open-narrow-angle-skived-cord-ends
This should say
Please use the “Request More Info” form

Just to make sure I’m understanding correctly, if the “Request Quote” area is empty you want the page to say “Success” and if it has blocks in it you want it to say “Not Working” is that correct? It’s possible that the code I gave you is backwards because I didn’t completely understand the goal.

If that scenario is correct, can you do a var_dump($pageBlocks) somewhere to see what it contains? It’s possible it’s not getting the values we’re both assuming it has.

If ‘Request Quote’ has anything in it, it should be (Success). But if we can get it to work either way I can reverse that. But no matter it doesn’t change.
I don’t know how or where to add var_dump($pageBlocks) to get it to work. It gives an error. It gives “syntax error, unexpected ‘}’” with it, and this error goes away when I remove it.

$pageBlocks = $pageObject->getGlobalBlocks('Request Quote');
$pageQuoteTitle = true;
if (!is_array($pageBlocks) || count($pageBlocks) === 0) {
	$pageQuoteTitle = true;
}
echo ($pageQuoteTitle ?  '<hr><h3>Please use the "Request a Quote" form</h3>(Success)<hr>' : '<hr><h3>Please use the "Request More Info" form</h3>(Not Working)<hr>');
var_dump($pageBlocks)

https://www.mytinytown.com/products/mercury-relay-30-amp-single-pole-24-vdc-coil-normally-open

You need a semicolon after it.

I thought that, but that caused a 500 error.
I even tried echo var_dump($pageBlocks); which also caused a 500 error

$pageObject = Page::getCurrentPage();
$pageBlocks = $pageObject->getGlobalBlocks('Request Quote');
$pageQuoteTitle = true;
if (!is_array($pageBlocks) || count($pageBlocks) === 0) {
	$pageQuoteTitle = true;
}
echo ($pageQuoteTitle ?  '<hr><h3>Please use the "Request a Quote" form</h3>(Success)<hr>' : '<hr><h3>Please use the "Request More Info" form</h3>(Not Working)<hr>');
var_dump($pageBlocks);

https://www.mytinytown.com/products/mercury-relay-30-amp-single-pole-24-vdc-coil-normally-open

This is probably exceeding the timeout limit for the server, which would mean it’s returning objects, we just don’t know what kind of object it’s returning. According to the code it should be returning an array of block objects. If that’s the case then this code should work, except that you have $pageQuoteTitle = true; in two places, and false nowhere.

I fixed the true / false issue, no change. Which I assume you expected. It was correct at the beginning though.
Then I decided to check the reports / logs.
Exception Occurred: /home/tinytown/public_html/application/blocks/community_product/view.php:209 Allowed memory size of 1073741824 bytes exhausted (tried to allocate 937443328 bytes)

So yes, you were correct about it timing out. No idea why I didn’t think of checking that first.
I will look in to trying to change the timeout limit.

So I just turned my memory_limit to 9100M and still get the error.
Exception Occurred: /home/tinytown/public_html/application/blocks/community_product/view.php:209 Allowed memory size of 9542041600 bytes exhausted (tried to allocate 6440370176 bytes)
I don’t know why or how that worked?
So 6440370176 bytes = about 6441 MB. So I adjusted it to 6512M, I have 8GB RAM in the server. That should work, right? Unless I’m missing something?
But it didn’t work, now I get:
Exception Occurred: /home/tinytown/public_html/application/blocks/community_product/view.php:209 Allowed memory size of 6828326912 bytes exhausted (tried to allocate 4829757440 bytes)
This should work, it leaves 1998569472 bytes
Line 209 is the var_dump($pageBlocks);

From the server:
total used free
Mem: 8008020 919200 6203752
Swap: 4194300 1297280 2897020
Total: 12202320 2216480 9100772

I would remove the var_dump. That Var dump wont give you much info as it is too highlevel

As @TMDesigns notes, var_dump can eat up memory. Concrete classes such as blocks include mutually circular references to other objects, hence var_dump becomes infinitely recursive until it crashes.

You can get better diagnostics in this situation by using
var_dump_safe($data, true, 2);

  • the data to dump
  • true = echo to console
  • 2 = maximum depth 2

var_dump_safe is a global function provided by the doctrine environment

Just to be clear, is this equivalent to the code that you have now?

$pageBlocks = $pageObject->getGlobalBlocks('Request Quote');
$pageQuoteTitle = true;
if (!is_array($pageBlocks) || count($pageBlocks) === 0) {
	$pageQuoteTitle = false;
}
if ($pageQuoteTitle) { 
	// there are blocks in the "Request Quote" Global Area
	echo '<hr><h3>Please use the "Request a Quote" form</h3>(Success)<hr>';
} else { 
	// there are no blocks in the "Request Quote" Global Area
	echo '<hr><h3>Please use the "Request More Info" form</h3>(Not Working)<hr>';
}

$pageObject = Page::getCurrentPage();
Then the code you posted

This page should show a (Not Working) and does not.
https://www.mytinytown.com/products/mini-mercury-hg-pilot-duty-control-float-switch-10-foot-normally-open-narrow-angle-skived-cord-ends

This page shows the (success) as it should
https://www.mytinytown.com/products/mercury-relay-30-amp-single-pole-24-vdc-coil-normally-open

So still not showing the correct info when its empty.

From advice from JohntheFish I added the code:
var_dump_safe($pageBlocks, true, 2);
The 2 pages above show results that are pretty much the same. Could the issue be it is pulling info from a global block? So was I doomed for failure from the beginning?

If you are using a Global Area and you were expecting different results on different pages, yes you were doomed from the start. A Global Area will always have the same blocks, regardless of which page you check it with.

Yup, once I saw the data all pop up the same, I knew it. I knew what the issue was.

I am so sorry for wasting your time.

So, here is a new thought.
Do you think if I put something like:
$pageQuoteTitle = true;
On the pages that have the ‘Request Quote’ block it would work? I know I’d have to change the code. Maybe like:

if ($pageQuoteTitle = true) {
	echo '<hr><h3>Please use the "Request a Quote" form</h3>(Success)<hr>';
} else { 
	echo '<hr><h3>Please use the "Request More Info" form</h3>(Not Working)<hr>';
}```

You will not get any page variables in the block. You might be better off to create a checkbox page attribute.