How to access the current page in a field plugin

Is the any way to get the current page in a field plugin? I must use it in vue and have tried really a lot. This gives me always the “homepage” in the panel.

<?php

Kirby::plugin('werbschaft/test', [
  'fields' => [
    'test' => [
      'props' => [
        'panelurl' => function () {
          $page = page();
          return $page->panelUrl();
        },
      ]
    ]
  ]
]);

I think then I could use panelurl in vue but it doesn’t seem to work.

What does your plugin need to do exactly? You can get hold of the current pages field data in a panel Vue compent by going through this.$store for example i read the product ID from the field value in my snipcart plugin like this:

data() {
  return {
    totalSales: null,
    productID: this.$store.getters["content/values"]()["productid"]
  }
},
1 Like

Unfortunately this does not really help. For most of the standard attributes like label or help you can use the query language. I am looking for a way to use them either directly in vue.js or in the yaml markup.

But what is the aim of your plugin? it would help us to give you the best advice if we knew what your end result should be. The line i gave you above is how to get the fields in the Vue component.

this.$store.getters["content/values"]()["productid"] will get the value of the productid field from the content text file.

In the PHP file, you have access to $this, and you should be able to address the current page via $this->model().

My usecase is actually relatively simple. I don’t know if this is the best way to do it. I need a field where the user enters the ID of a YouTube video. Then he can press the download button. Since I do not know how to trigger functions directly in the backend, I do this via a small detour. The user presses the button, he is led to the frontend where a function is executed (in this case the YouTube thumbnail is loaded on the page) and he is led directly back to the backend.

That’s why I have to include the page url and the page panel url in the GET parameters that I can redirect back. Or is there another way to do this?

I don’t have success with $this->model() or the getters as I don’t need a field, I need access to the $page object. Cause of this I was also asking how to include the Query language in my own fields.

And yes, I know that I could also use the Janitor plugin for this, but at some point I have to learn to make my own field and this seemed to me a good usecase.

template: `
    <k-field
      class="k-getter-field"
      :label="label"
    >
      <k-input
        theme="field"
        type="text"
        name="getter"
        :value="value"
        @input="onInput"
      >
        <k-button
          v-if="link"
          :icon="icon"
          // here the link is generated
          :link="siteurl + '?youtube_id=' + value + '&redirect=' + panelurl"
          slot="icon"
          class="k-input-icon-button"
          tabindex="-1"
          target="_self"
        />
      </k-input>
    </k-field>
  `

I think you should do this directly when the button is pressed. Listed to this button in your Vue component, then call a custom endpoint that does the downloading in the background.

What does this return? Just the field object?

@texnixe I am also in need of getting the current user’s ID for a plugin in the panel. not the logged in user, but the user ID of the user page I am viewing. calling $this->model()->id() inside the PHP works well

'props' => [
                'param' => function ($param = null) {
                    return $this->model()->id();
                },
],

but right now it is only hardcoded. i want to be able to pass param: this.model.id in the blueprint, but i don’t know how to translate the query language to php. or does this happen in the vue component? i cant seem to find some helpful info.

and also, it says in the docs that this model class is deprecated already and will be removed in version 5. how would you implement this otherwise?

EDIT:

ah, i got it working by passing param: "{{user.id}}" and:

'param' => function ($param = null) {
                    if ($param)
                        $param = $this->model->toSafeString($param);

                    return $param;
},

but still not sure if this is correct, i am just trying things i find in the forum blindly :slight_smile:

Not sure what your context is, but yes, see Dynamic default number field - #2 by texnixe