How to get the parent block object of a field?

Hi!

In a plugin I’m extending the fieldMethods of a textarea/writer field. As this field could also appear in a block, I’m trying to get the block object from the $field variable.

Basically something like this:

  'fieldMethods' => [
    'inlinefootnotes' => function ($field) {
      $blockId = $field->isChildOfBlock() ? $field->parentBlock()->id() : false;
      return InlineFootnotes::convert($field, $blockId);
    },

the parent() method just gives me back the $page element.

Is there another way to do this if the $field is not aware of its parent block?

Thanks!

$field->field() should return the parent field

Hi texnixe,

sadly this still gives me back the field object not the block object.

I tried this:

  <div class="blocks">
    <?php foreach($page->blocks()->toBlocks() as $block): ?>
    <div class="block">
      <?php dump($block->text()->field()) ?>
    </div>
    <?php endforeach ?>
  </div>

returns

Kirby\Content\Field Object
(
    [text] => textcontent...
)