Skip to content

Changing :slug page url parameter name breaks the setUrl methods. #25

@RomainMazB

Description

@RomainMazB

All the four components of this plugin use model helper methods setUrl on Post and Category models which generate the blog or category url from the corresponding page passed in parameter of the method:

public function setUrl($pageName, $controller)
{
$params = [
'id' => $this->id,
'slug' => $this->slug
];
return $this->url = $controller->pageUrl($pageName, $params, false);

public function setUrl($pageName, $controller, $params = [])
{
$params = array_merge([
'id' => $this->id,
'slug' => $this->slug,
], $params);
if (empty($params['category'])) {
$params['category'] = $this->categories->count() ? $this->categories->first()->slug : null;
}
// Expose published year, month and day as URL parameters.
if ($this->published) {
$params['year'] = $this->published_at->format('Y');
$params['month'] = $this->published_at->format('m');
$params['day'] = $this->published_at->format('d');
}
return $this->url = $controller->pageUrl($pageName, $params);
}

The problem is that as you can see, the slug page's param is hard-coded in the url generation, which make this page url generation to fail:

// post.htm
title = "Blog post"
url = "/blog/:notSlug"
layout = "default"
is_hidden = 0

[blogPost]
slug = "{{ :notSlug }}"
categoryPage = 404
==
{% component 'blogPost' %}

Same is true for the page integrating the blogPosts, blogCategories and blogRssFeed components.

As of today, I don't see any easy way to fix this without adding a new property that would refer the target page url parameter name.
Something like this in Categories component:

// components/Categories.php

public function defineProperties()
{
    return [
        'slug' => [
            // ...
        ],
        'displayEmpty' => [
            // ...
        ],
        'categoryPage' => [
            'title'       => 'winter.blog::lang.settings.category_page',
            'description' => 'winter.blog::lang.settings.category_page_description',
            'type'        => 'dropdown',
            'default'     => 'blog/category',
            'group'       => 'winter.blog::lang.settings.group_links',
        ],
        'categoryPageSlugParam' => [
            'title'       => 'winter.blog::lang.settings.category_page_slug_param',
            'description' => 'winter.blog::lang.settings.category_page_slug_param_description',
            'default'     => 'slug',
            'type'        => 'string',
        ],
    ];
}

This property value could be passed to the setUrl method and generate the good url structure like this:

public function setUrl($pageName, $controller, $slugProp)
{
    $params = [
        'id'   => $this->id,
        $slugProp => $this->slug
    ];

    return $this->url = $controller->pageUrl($pageName, $params, false);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions