Display all blocks used on page?

Hey :slight_smile:

I want to load script on the page only of a block is used.
To do that i have to check if the block is used.

Is there any way to see all blocks used on the page?

In a simple blocks field or inside a layout field? Or do you even use multiple blocks/layouts fields in a single page?

I use multiple blocks on my page

<?php foreach ($page->modules()->toBlocks() as $block) : ?>
                <?php snippet('blocks/' . $block->_key(), ['data' => $block]) ?>
            <?php endforeach ?>

<?= $page->listofusedblocks... ?>

In the end i need to check if a specific block is used on that page or not.

That was not my question. Is there only one blocks field in the page? Or multiple? Or even layout fields?

Ah sorry. In the end i am not adding the content. So there could be one, could be 10…

but in case i am getting the question wrong again. Here the blueprint

        width: 1/1
          lego: fields/modules

  settings: tabs/settings

   type: fields
        label: Modules
        type: blocks
            label: content
            type: group
              cta__header: fieldsets/cta__header
              cta__footer: fieldsets/cta__footer
              card__text_4: fieldsets/card__text_4
              card__light_4: fieldsets/card__light_4
              content__text_image: fieldsets/content__text_image
              content__section_title: fieldsets/content__section_title
              content__image_big: fieldsets/content__image_big
              content__image_custom_width: fieldsets/content__image_custom_width
              content__embed_video: fieldsets/content__embed_video
              content__3_text_blocks: fieldsets/content__3_text_blocks
              content__3_columns: fieldsets/content__3_columns
              content__explainblock: fieldsets/content__explainblock
              content__other-pages: fieldsets/content__other-pages
              content__slider: fieldsets/content__slider
              buttons__two: fieldsets/buttons__two
              landingpage__content_section: fieldsets/landingpage__content_section
              landingpage__anim_content_section: fieldsets/landingpage__anim_content_section
              landingpage__cta_footer: fieldsets/landingpage__cta_footer
            label: rare use
            type: group
            open: false
              rare_use__pricing: fieldsets/rare_use__pricing
              rare_use__career: fieldsets/rare_use__career
              rare_use__modal: fieldsets/rare_use__modal
              rare_use__text_formal: fieldsets/rare_use__text_formal
              rare_use__margin: fieldsets/rare_use__margin
              rare_use__quote: fieldsets/rare_use__quote
              rare_use__custom_code: fieldsets/rare_use__custom_code
              rare_use__testimonial: fieldsets/rare_use__testimonial
              rare_use__form_waitlist: fieldsets/rare_use__form_waitlist
              landingpage__not_used_by: fieldsets/landingpage__not_used_by
              rare_use__top-background: fieldsets/rare_use__top-background

Getting the fieldset names in the end would be completely fine.

Ok, I see only one blocks field here called modules.

$blockType        = 'cta__header',
$doesBlockXExist  = $page->modules()->toBlocks()->filterBy('type', $blockType)->isNotEmpty();

Thank you for your help, but it seems that there still is a problem with that one. It always says that it is empty/β€œis false” .

<?php $blockType = 'buttons__two' ?>
<?php $doesBlockXExist = $page->blocks()->filterBy('type', $blockType); ?>

<?php if ($doesBlockXExist->isNotEmpty()) : ?>
is true
<?php else : ?>
is false
<?php endif ?>

Could the reason be that i have fieldsets in fieldsets?


You have to call the toBlocks() method on the modules field, not on $page.

1 Like

Awesome! That works!
Thank you :relaxed: