Problem with accessing data of Block Object

Hello

I load my snippets with the following Code:

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

When i dump inside of the module snippet the content of the block object (dump($block->content())) i get the following:

Kirby\Cms\Field Object
(
    [content] => Array
        (
            [visible] => true
            [moduleslides] => Array
                (
                    [0] => page://zywNt7WUkJhgpwPT
                    [1] => page://BnoW7ljdYvPvdNws
                    [2] => page://gRcBcQYiEwHTUNid
                    [3] => page://UcTh4BZuW0cd2uUA
                )

        )

)

now i want to access the value of visible like this: $data->visible()->toBool()
Then i get this Error: Call to undefined method Kirby\Editor\Block::visible()

  • I tried renaming visible to something different but its still not working
  • also when i dump it like this: dump($data->content()->visible()); i get the same output as when i dumped just the content.

Am i missing something?

From this error message I’d assume you are still using the old Editor plugin, which was made redundant when we released the blocks field type in Kirby 3.5. But at the same time, you are using a newer version of Kirby (at least 3.8, since you show UUIDs).

Actually i tried to switch from editor to blocks like this: Blocks | Kirby CMS
I changed the yaml to type: Blocks and removed the editor plugin

What is your Kirby version, though? The Blocks migrator was removed at one point (after 3.6 I think and only re-introduced in Kirby 3.9.2

And did you clear the media folder, so that all traces of the Editor plugin are removed?

i just updated from 3.3.6 to 3.9.4 and yes, i just tried to delete media folder it didnt had any impact unfortunately

Could you post the complete code of the snippet, please?

<?php
    dump($data->content()->moduleslides());
     ?>
<?php if ($data->content()->visible()) : ?>
    <div class="container-full richtext noisy module-offset module-header-lead">
        <div class="slider-container">

            <?php foreach ($data->content()->moduleslides()->toPages() as $moduleSlide) : ?>
                <?php if ($moduleSlide->isNotEmpty()) : ?>
                    <div class="item">
                        <?php snippet('layout/organisms/header-lead', [
                            'image' => $moduleSlide->productheaderimage()->isNotEmpty() && $moduleSlide->productheaderimage()->toFile() ? $moduleSlide->productheaderimage()->toFile() : $moduleSlide->image(),
                            'title' => $moduleSlide->producttitle()->html(),
                            'subtitle' => $moduleSlide->productsubtitle()->kirbytext(),
                            'buttonurl' => $moduleSlide->url(),
                            'worldvisual' => $moduleSlide->worldvisual()->value()
                        ]) ?>
                    </div>
                <?php endif ?>
            <?php endforeach ?>

        </div>
        <div class="container jubilee-wrapper">
            <div class="jubilee-inner">
                <div class="jubilee <?php if($page->isHomePage()) {echo 'homepage-jubilee';} ?>" >
                    <?php if($site->jubilee()->isTrue()) {
                        snippet('layout/atoms/jubilee');
                    } ?>
                </div>
            </div>
        </div>
    </div>
<?php endif;  exit; ?>

I managed to get the if to work with <?php if ($data->content()->visible()) : ?>
but now it has an error in the foreach:
Kirby\Cms\Pages::findByKey(): Argument #1 ($key) must be of type ?string, array given, called in …/app/kirby/src/Toolkit/Collection.php on line 393

The Blocks field doesn’t have a visible property, maybe it works automatically if you test if the block is hidden or not

if ($block->isHidden() === false)

(or $data>isHidden(), but don’t know where that variable is coming from)

the visible is my own toggle field. and data comes from this:

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

Ah, ok, because in your first post you had

['block' => $block]

That confused me.

This piece of code is not correct, $moduleSlide is a page object and a page object doesn’t have an isNotEmpty() method.

What you want to know is if the collection is empty, I guess:

$moduleSlides = $data->content()->moduleslides()->toPages();

if ($moduleSlides->isNotEmpty()) {
  foreach ($moduleSlides as $moduleSlide) {
    // do stuff
  }
}

unfortunately i still have this error:
Kirby\Cms\Pages::findByKey(): Argument #1 ($key) must be of type ?string, array given, called in …/app/kirby/src/Toolkit/Collection.php on line 393

When you dump this, what do you get?

i got the same as when i dumped content but i fixed the error with reinstalling kirby 3.9.4 via composer dont ask me why it works but it does now

Great!

Sorry for bothering :smiley: