$page->contentFileDirectory() returns bad path for Windows?

Hello,

I want to create a directory in each article template directory. Thus I did this:

	'hooks' => [
		'page.create:after' => function ($page) {
			if($page->template()->name() == 'article') {
				Dir::make($page->contentFileDirectory() . '\comments');
			}
        }
	]

I noticed that $page->contentFileDirectory() returns

D:\Software\wamp64\www\kirby\content\1_blog\_drafts/cwaddcwawc

where cwaddcwawc is the article name. Is this a bug? See the mixed usage of / and \ in the path. Firefox doesn’t detect this absolute path due to the mix. If I do replace the / with a \ Firefox will display the directory on my disk.

Remark: My original question was why no directory was created but I did fix this somehow. Still I’m curious about the path.

Best Regards,

Use the directory separator constant instead:

Dir::make($page->contentFileDirectory() . DS . 'comments');

Do you really only want to create a folder or a subpage?

Hi @texnixe!

I don’t see much of a difference in a page and a directory/folder. To be honest I’m working on the comment system after quite some struggling. I wanted to create a folder because I think the article folder becomes quite messy if the article, several comments comments, uploads, etc. are in the same folder without a clear saperation.

During going to bed I thought about creating a pages preset called comments and that page has all the comments as children. This wouldn’t change anything in the current folder structure but I can see more easily which comments are published/drafts and it’s easier to delete some of them.

What do you think? Why did you ask?

PS: Your idea regarding using DS is good. I changed that! On the other hand, this doesn’t have an impact on the echoed path with the /.

Well, you current way of creating the new comments page doesn’t create a page with a text file in it. Kirby has methods to create subpages and I don’t see a reason to fall back to low level folder creation when you can create a page.

You can also auto-create such subpages at page creation, see: https://getkirby.com/docs/cookbook/extensions/subpage-builder

I must admit, I’m not following you… but then I just peaked in and don’t really have the time now to think about it.

config.php with the page.create:after hook:

	'hooks' => [
		'page.create:after' => function ($page) {
			if($page->template()->name() == 'article') {
				$comment = $page->createChild([
                    'slug'     => 'comments',
                    'template' => 'comments',
                    'content'  => null,
					'draft'	   => false
                ]);
			$page->update(['Children' => Yaml::encode($page->childrenAndDrafts()->toArray())]);
			}
        }
	]

where comments blueprint is defined as a section shortcut:

title: Comments
options:
  status: false
sections:
  pages:
    headline: Comments
    status: all
    sortBy: creation_date desc

Now my articles have a children which displays every comment for that article. For now I think this is a good structure and works as intended.

PS: I deleted my two previous comments as they were invalid and just confusing because I was wrong at what I achieved last night.

PS2: Pasting source code seems to become crippled in the forum editor sometimes . . .

That now looks the way I was expecting it to look :slightly_smiling_face:

1 Like

Took me some time for now as I tried it with a pages: preset first but found no way to let it display my comments and found nothing helpful to me in the forum nor in the docs about it. I think I didn’t understand the pages preset.

On the other hand this solution is quickly done too (if I knew it before) and the next steps are informing the user in the front end about his comment being published. Then my comment system works and that was one of the biggest pain points to me.

@warg:
To answer your first question:
look e.g. at https://www.php.net/manual/en/function.dirname:
since a PHP version, I don’t remember the version number, slash and backslash are used without any difference on Windows.
Since Kirby 3 the Kirby core team decided, to set DS always to the slash.