Access current block in query

I have a usecase where I would like to access the current block in a field. E.g. the custom my-field acts on the value of another field. I specify this relation by providing a query that should point to another field within the same block:

# blueprints/blocks/my-block.yml

fields:
  test:
    type: writer
  custom:
    type: my-field
    query: block.test 
// my-field.php
return [
  // ...
  'computed' => [
    'text' => function() {
      // Should return the value of the writer field
      return $this->model()->query($this->attrs['query'])->value();
    }
  ]
];

But it seems that only site, page and file are available to query, not block. Is there maybe any workaround?

Shouldn’t it be block not blocks if it works st all? Or try model instead of block

Ah yes, block is what I tried, this was just a typo for the example here

I’m facing same probleme.

block.someMethode does not work at all. And model.someMethode will be resolved to page.someMethode.

Same behaviour here for legacy and the new query runner query | Kirby CMS

A solution would be great.

I think the big problem is that a field doesn’t know anything about it’s whereabouts. No matter if it lives within a page blueprint, a block blueprint, a structure, the model is always the page.

I hope there will be a solution.

Currently it’s not possible process data based on referenced field within same block. Just an example with or without practical relevance:

blueprints/blocks/specialPages.yml

type: specialPages
fields:
  pages:
    type: pages
    min: 1
    query: page.index.filterBy('template', 'specials')

  specialAttributes:
    type: select
    options:
      type: query
      query: this.pages.toPages().plug('attributes', ',', true)

This query is just to demonstrate why ‘this’ could be a useful key word.

1 Like

Agree that this would be useful, but the problem is that this would have to happen on the frontend, when the value of the pages field changes. The backend blueprint doesn’t know anything about your frontend.