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 . ‘-’ : ‘
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 ? ‘
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)
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
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>';
}
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.
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>';
}```