Update variable regarding number of same blocks in page

Permalink
Hi,

I'm trying to update a number (variable : XX) on a page.
i need to update this : XX feedback(s)
The number of Feedbacks Should be the amount of the same blocks Type in the page.

I just have no idea how to do this or if it is possible.

If any idea your welcome

Chris

chrismodlao
 
MrKDilkington replied on at Permalink Reply
MrKDilkington
Hi chrismodlao,

You can try this:
- this example gets the page block count for the Feature block
<?php
$c = Page::getCurrentPage();
$pageBlocks = $c->getBlocks();
$featureBlockCount = 0;
if ($pageBlocks) {
    foreach ($pageBlocks as $block) {
        $blockTypeHandle = $block->getBlockTypeHandle();
        if ($blockTypeHandle == 'feature') {
            ++$featureBlockCount;
        }
    }
}
echo "$featureBlockCount Feature blocks on this page";
?>
chrismodlao replied on at Permalink Reply
chrismodlao
Thanks for your solution.

Nice snippet but i need to be able to dynamic change value in Content Block.
I guess its something not possible...

See ya
MrKDilkington replied on at Permalink Reply
MrKDilkington
@chrismodlao

This could be done using a Content block custom template:
<?php defined('C5_EXECUTE') or die('Access Denied.');
$c = Page::getCurrentPage();
if (!$content && is_object($c) && $c->isEditMode()) { ?>
   <div class="ccm-edit-mode-disabled-item"><?=t('Empty Content Block.')?></div>
<?php
} else {
    $pageBlocks = $c->getBlocks();
    $featureBlockCount = 0;
    if ($pageBlocks) {
        foreach ($pageBlocks as $block) {
            $blockTypeHandle = $block->getBlockTypeHandle();
            if ($blockTypeHandle == 'feature') {
                ++$featureBlockCount;
            }
        }

When you type "{{feedback}}" into a Content block with the custom template, it will be replaced with the $featureBlockCount value.
chrismodlao replied on at Permalink Reply
chrismodlao
You save my day with this code !!

But unfortunately it brakes the link to the images in the Content block.
It looks like you forgot the PHP end code like at the end of your exemple :
?>


But with this one or without, It returns this :
<img src="/files/">


And do you think there is any possibilities to check number of blocks in a apge but display the amount in another content block's page ??
MrKDilkington replied on at Permalink Reply
MrKDilkington
@chrismodlao

The closing PHP tag is not required with this code snippet.

I tested this code in 5.7 and in version 8 and images displayed correctly in Content blocks.

"And do you think there is any possibilities to check number of blocks in a apge but display the amount in another content block's page ??"

Instead of using the current page, you can get a specific page by ID.
<?php defined('C5_EXECUTE') or die('Access Denied.');
$c = Page::getCurrentPage();
if (!$content && is_object($c) && $c->isEditMode()) { ?>
   <div class="ccm-edit-mode-disabled-item"><?=t('Empty Content Block.')?></div>
<?php
} else {
    $pageObject = Page::getByID(1);
    $pageBlocks = $pageObject->getBlocks();
    $featureBlockCount = 0;
    if ($pageBlocks) {
        foreach ($pageBlocks as $block) {
            $blockTypeHandle = $block->getBlockTypeHandle();
            if ($blockTypeHandle == 'feature') {
                ++$featureBlockCount;
            }
chrismodlao replied on at Permalink Reply
chrismodlao
Hi Thanks again for this.
Thats exacly this.

But unfortunately i still have the problem with images.
I will dig it out...

Thanks

Chris