Is there a way to change the homepage in the panel?

I am trying to find a way to change the homepage in the panel without changing it in the config file ?
As I would need the client to choose/change it by himself afterwards.

Can I link a variable from “site.php” to the “config.php” file maybe ?

Thank you for your help.

Something like this could work:

c::set('home', site()->home()->value());
```

In your site.txt

```
home: about
```

But then you need to either set a default or make sure the field has a default value.
1 Like

I don’t think this will work because the Kirby classes are only loaded after the config file to make multilang sites possible. You could however read the site.txt yourself in the config.php file:

$data = data::read(kirby()->roots()->content() . DS . 'site.txt', 'kd');
c::set('home', a::get($data, 'home', 'home'));

I tested this before I posted it and it did work.

Oh, that’s interesting. I have had problems with accessing content from config.php before. But good to know.

Yeah, that’s what I thought as well, but I was curious to see if it works.

Great, I wouldn’t have imagined it would be that easy !

Thanks

i haven’t tested it – would this still work in k3?

I just tested it, unfortunately it no longer works exactly like this.

Try this inside your site/config/config.php:

<?php

$data = Kirby\Data\Data::read(__DIR__ . '/../../content/site.txt');

return [
    'home' => $data['home'] ?? 'home'
];

Thanks for your answer! I updated my config file to

<?php

$data = Kirby\Data\Data::read(__DIR__ . '/../../content/site.txt');

return [
    'debug'  => true,
    'home' => $data['home'] ?? 'home'
];

the site blueprint:

fields:
  home:
    label: Landing Page
    type: pages
    default: work

and in my site.txt it has:

Home: 

- work

I guess your sample should be working but I am getting the error “The home page does not exist”… I can’t really see why :open_mouth:

The problem is that the page is stored not as a string but in yaml format, so $data['home'] returns - work instead of work.

1 Like

Yes, my code assumes that the field simply stores the UID of the page.

If you want to use the pages field, you can do it like this:

<?php

$data = Kirby\Data\Data::read(__DIR__ . '/../../content/site.txt');

return [
    'home' => ltrim($data['home'] ?? 'home', '- ')
];

You also need to limit the field to a single page:

fields:
  home:
    label: Landing Page
    type: pages
    multiple: false
    default: work

Please note that this is a bit of a hack. :slight_smile:

1 Like

It’s working like an absolute charm! I don’t mind the “hack” at all + thank you @texnixe for the reminder how the data is output.

1 Like