Wrong slug when creating page in panel

When I create a page in the panel with a name that starts with a number and is followed by a - or a character that is changed to a - (for example ‘100.1 test’):

Then the url appendix dialog says ‘101-1-test’ which is correct.

But when I click on ‘Add’, the created slug is ‘1-test’.

When I click back to the parent, I see that is has been given an index of 101. But I never meant to give it an index and I wouldn’t expect the add page dialog to do this.

Is this a bug in panel or am I doing something wrong?

Well, page slugs may not start with a number, because numbers are reserved for visibility purposes in Kirby. If you have to use a title that starts with a number, you have to make sure that the number is not part of the UID.

Isn’t it strange that the panel’s add page feature is first reporting the url appendix in the correct way and is then changing it afterwards? Feels like a bug to me…

Shouldn’t it then report an error instead, when adding the page? ‘UID’s may not start with a number’

Yes, the Panel should indeed report an error. This is a known issue.

Yes, I agree, that’s a bit weird. Are you using a hook that makes a page visible on page creation?

Workarounds that have been used by other users:

  • use a hook that removes leading numbers from the slug on page creation.
  • use two titles

Is there a related issue on Github for this?

https://github.com/getkirby/panel/issues/326

Thanks!

I am not working with page visibility… In my project, I assume all pages to be ‘visible’. And I really want to avoid having every page directory having a number in front…

I wonder, does putting a ‘-’ as the first character also make the page visible without giving it an index?

Actually, if the first ‘-’ would be swallowed by kirby’s slug system, we could add a - in front when the page name starts with a number and don’t want it to be used as visibility index… and it would still start out invisible

‘-101.1 test’ would generate slug: ‘101-1-test’

I will probably implement this on my side as a hook.

If only it was this simple… It seems the $page->move() function swallows the first ‘-’ instead.

I think you can only use an underscore, not a dash.

Underscores are changed to dashes, which are then stripped from the start of the slug

Hm, too bad :thinking:

I tried creating a hook that adds ‘qqq’ to the start of the dirname when it starts with a number:

  if (preg_match('/^[0-9]+\-/', $page->dirname())) {
    $page->move('qqq'.$page->dirname());
    $page->hide();
  }

Sadly, this results in ‘101.1.test’ -> dirname ‘101-qqq101-1-test’…

Shouldn’t the hide call remove the 101- at the start?

Haven’t tested, but I think this can’t work, you probably have to store the new page after move in a variable first before you can hide it.

as far as I can see, $page->move() does not return a new value to save in a variable…

Yes, it creates a boolean, but fetch the new page:

$newPage = page('qqq'. $page->dirname());
$newPage->hide();
1 Like

That gives me another error actually… and I am confused why I would need to create a new page after calling $page->move() in the first place… doesn’t it update these things internally when calling move?

Anyway, I am calling a day on this… Will try to figure out a cleaner way of dealing with this number issue another time. Thanks for helping!

I figured out a way to add a leading dash by going through the file system instead:

  kirby()->hook('panel.page.create', function($page) {
    if (preg_match('/^[0-9]+\-/', $page->dirname())) {
      $name = '-'.$page->dirname();
      $uri = $page->parent()->uri() . DS . $name;
      $root = dirname($page->root()) . DS . $name;
      dir::move($page->root(), $root);
      $this->panel->redirect('pages' . DS . $uri . DS . 'edit');
    }
  });

Does this stay in place when you update the page via the Panel?