Accessing Specific Block Field from $kirby

Hi folks

I use the layout functionality and inside, I use blocks. Let’s say I am on page A and would like to access a block field value of page B? Is this somehow possible?

I started by trying $kirby->page('B')->layout()->toLayouts() but then I somehow got lost and couldn’t figure out a way to retrieve a single block field value.

The only idea I have right now is a foreach loop over all returned layouts and blocks. However, this is a bit a heavy load for only retrieving a single value. Somehow, I think it should be possible to directly access a block field value, maybe by the block’s ID?

Any ideas appreciated :slight_smile:

Cheers

Stefan

That is definitely possible, but to do that, you would have to know the id of the particular block. In any case, you would have to fetch all blocks of all layout columns into a single collection and then find the one that interests you.

Here is an example field methods to find a block by ID:

	'fieldMethods' => [
		'findBlockById' => function($field, $id) {
			$blocks = new Kirby\Cms\Blocks();
			return $field->toLayouts()->map( static function($layout) use($blocks){
				return $layout->columns()->map( static function($column) use($blocks){
					$blocks->add($column->blocks());
				});
			})->findBy('id', $id);
		}
	]

@texnixe thanks a lot! that’s exactly the approach I couln’t figure out myself!