Show page urls (multilanguage) in custom field

Hi, I would like to show the page urls with a custom field in the panel on every page.
The following works fine in the frontend.


$kirby = kirby();
$urls = [];
foreach ($kirby->languages() as $language) {
      $url = $kirby->site()->page()->url($language->code());
      $urls[] = $url;
 }
return $urls;

Which returns:
http://project:8888/de/haeufig-gestellte-fragen
http://project:8888/en/faq

But in the panel i get only the domain + langcode without the page url (slug?) like this:
http:://project:8888/de
http:://project:8888/en

Here´s my custom field code:
my-plugin/index.php

Kirby::plugin('my/plugin',  [
    'fields' => [
        'pageurls' => [
            'props' => [
                'label' => function (string $label) {
                    return $label;
                },
            ],
            'computed' => [
                'urls' => function () {
                    $kirby = kirby();
                    $urls = [];
                    foreach ($kirby->languages() as $language) {
                        $url = $kirby->site()->page()->url($language->code());
                        $urls[] = $url;
                    }
                    return $urls;
                }
            ],
        ]
    ]
]);

What am I missing?

Are you intending to literally show the list of the pages for information purposes, or is there more to it you want to do? I don’t think its working in the panel because you are in the context of the panel, not the context of the page.

If it’s just a list you want, you can dynamically create structure field content with the info you want, via a hook (page create & page update). I think there, the code you have will work (and i think you can probably do what you have quite a bit shorter). As long as its in the blueprint, you can set its contents from a hook, and set the structure field to read only. I don’t think you need to go the trouble of the plugin.

To save space, you could just populate a readonly select field with the query language.

p.s welcome to the forum :slight_smile:

I think the problem is the way you try to call the current page, this should work:

 foreach ($kirby->languages() as $language) {
    $url = $this->model->url($language->code());
    $urls[] = $url;
}
return $urls;

You should probably check if you have a multi language site and display something else if not, otherwise, users will end up with an empty field.

@jimbobrjames thanks for your idea. the field will be just for display purposes. The query language isn´t my »friend« at the moment, so I will go with texnixes solution.

@texnixe Thanks, this works!

Where can I find more about the »panel(?)«-model?
$this->model->... or just the $this in this context.

var_dump($this) doesn´t seem to work well within the panel output.

$this in this context refers to the field, I think, which has certain properties (https://getkirby.com/docs/reference/@/cms/field). $this->parent->url($kirby->language()->code()) would also be possible (Edit: doesn’t seem to work, though, :thinking:)

Nowhere, I’m afraid. Bastian is currently doing a series of live streams on field development which we will integrate into the website when ready.

But we nevertheless have to update the documentation.

@texnixe thanks a lot for the info!