Sorting numbers in the URL

Hello

there is one aspect to Kirby, that I don’t understand.

You link to a file like this:

a) (file: impulspapier.pdf text: Impulspapier)
or
b) (link: /content/1-aktivitaeten/impulspapier.pdf text: Impulspapier)
if you have uploaded it to an other page.

If a user saves or mails the direct link then, she/he get’s

http://domain.de/content/1-aktivitaeten/impulspapier.pdf

If I change the structure of the menu, the link will fail in both cases. In the URL of the html page, Kirby don’t insert the nummer in the URL.

http://domain.de/aktivitaeten

Why does Kirby operate in this different ways relating to the “sorting numbers” ?

Regards

FUSS

First of all, when referencing a file from another page, you don’t have to (nor should you) insert the sorting number:

(file: aktivitaeten/impulspapier.pdf) 

is sufficient (i.e. without the content folder and the sorting numbers). Kirby automatically generates the correct URL with the sorting number.

Secondly, Kirby has an internal router that finds the correct file. If you call a link like this:

http://www.dortmund-zu-fuss.de/aktivitaeten/stellungnahmen/Masterplan-Impulspapier-FUSS.pdf

You will be automatically redirected to

http://www.dortmund-zu-fuss.de/content/1-aktivitaeten/1-stellungnahmen/Masterplan-Impulspapier-FUSS.pdf

If you want Kirby to create links like this through the file or image Kirbytags, you can make copies of those tags and modify them accordingly.

To make Kirby create those permalinks to files, you can use a custom file method, see example here: kirby-custom-methods/file-methods/permanent-url.php at master · texnixe/kirby-custom-methods · GitHub

Note that if you do this, all calls to file/image URLs go through the router.

2 Likes

Hello texnixe,

thank you. I tried it again and now it works.

Means “like this” links without numbers?

Regards

FUSS

Yes, that’s what I meant.

Keep in mind that if you rename a page folder, even file l links without folder numbers will no longer be valid. If this is likely to happen, you might consider using a dedicated file folder.

1 Like

Hello

let’s me repeate it in own words.

If I want links to files without numbers, I can make a copy of the Kirbytags, e. g. file2. Then I write (file2: aktivitaeten/impulspapier.pdf) and the user gets http://domain.de/content/aktivitaeten/impulspapier.pdfinstead of http://domain.de/content/1-aktivitaeten/impulspapier.pdf. Then I can change the order in den menu without destroying the direct link until I rename the page folder. That’s the same basic principle in Wordpress, even the implementation is different.

So I will agree with your last comment.

Furthermore, I have questions, to understand the philosophy of Kirby.

You wrote, that I could modify the Kirbytag file. In the handbook stands, where to save the modifyed Kirbytag.

https://getkirby.com/docs/developer-guide/kirbytext/tags

But the side ist silent about where the default tags are saved.

Then you write.

To make Kirby create those permalinks to files, you can use a custom file method, see example here: https://github.com/texnixe/kirby-custom-methods/blob/master/file-methods/permanent-url.php

Then I add a short link in the impulspapier.pdf.txt in this field and this short link is independent of the pahe folder?

Regards

FUSS

It works like this:

  1. Create a new file methods.php in /site/plugins with the following code:
file::$methods['permanentURL'] = function($file) {
  $site = $file->site();
  $lang = $site->multilang() ? $site->language()->code() . '/' : '';
  return $site->url() . '/' . $lang . $file->page()->id() . '/' . $file->filename();
};

This is a custom file method that generates a link without the prepended numbers.

  1. Copy the following code into file.phpand save in /site/tags:
<?php
kirbytext::$tags['file'] = array(
  'attr' => array(
    'text',
    'class',
    'title',
    'rel',
    'target',
    'popup'
  ),
  'html' => function($tag) {

    // build a proper link to the file
    $file = $tag->file($tag->attr('file'));
    $text = $tag->attr('text');

    if(!$file) return $text;

    // use filename if the text is empty and make sure to
    // ignore markdown italic underscores in filenames
    if(empty($text)) $text = str_replace('_', '\_', $file->name());

    return html::a($file->permanentURL(), html($text), array( // this line uses the new method instead of the url() method
      'class'  => $tag->attr('class'),
      'title'  => html($tag->attr('title')),
      'rel'    => $tag->attr('rel'),
      'target' => $tag->target(),
    ));

  }
);

This is an exact copy of the file tag from/kirby/extensions/tags.php with the only difference that it now uses the custom file method from above to create the url. The new tag thus overwrites the original tag.

In your text file, you call the tag as before:

(file: aktivitaeten/impulspapier.pdf)

If you don’t use the tag, but instead you want to get the URL of a file directly in your template, also use the above method instead of the url() method

if($file = page('aktivitaeten')->file('impulspapier.pdf')) {
 echo $file->permanentURL();
}

Hope this makes it clearer.

Woah! I didn’t know this. Thx for the tip!