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

panelfield
#1

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.

0 Likes

#2

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

#3

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'));
0 Likes

#4

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

0 Likes

#5

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

0 Likes

#6

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

0 Likes

#7

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

Thanks

0 Likes

#8

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

0 Likes

#9

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'
];
0 Likes

#10

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:

0 Likes

#11

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

#12

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

#13

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