-
-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changing :slug page url parameter name breaks the setUrl methods. #25
Comments
Ok, I've found how to solve this issue properly without any modification to the /*
* Add a "url" helper attribute for linking to each post and category
*/
// Load post and category pages
$activeTheme = Theme::getActiveTheme();
$postPage = Page::loadCached($activeTheme, $this->property('postPage'));
$categoryPage = Page::loadCached($activeTheme, $this->property('categoryPage'));
// Retrieve the post slug property name
if ($postPage && $postPage->hasComponent('blogPost')) {
$postPageSlugParam = $postPage->getComponent('blogPost')->property('slug');
if (preg_match('/^\{\{([^\}]+)\}\}$/', $postPageSlugParam, $postMatches)) {
$postPageSlugParam = substr(trim($postMatches[1]), 1);
}
}
// Retrieve the category slug property name
if ($categoryPage && $categoryPage->hasComponent('blogPosts')) {
$categoryPageSlugParam = $categoryPage->getComponent('blogPosts')->property('slug');
if (preg_match('/^\{\{([^\}]+)\}\}$/', $categoryPageSlugParam, $categoryMatches)) {
$categoryPageSlugParam = substr(trim($categoryMatches[1]), 1);
}
}
// Default to slug if not found
$postPageSlugParam ??= 'slug';
$categoryPageSlugParam ??= 'slug';
$posts->each(function($post) use ($postPageSlugParam, $categoryPageSlugParam, $categorySlug) {
// Pass them as $params parameter of setUrl method
$post->setUrl($this->postPage, $this->controller, ['category' => $categorySlug, $postPageSlugParam => $post->slug]);
$post->categories->each(function($category) use ($categoryPageSlugParam) {
$category->setUrl($this->categoryPage, $this->controller, [$categoryPageSlugParam => $category->slug]);
});
}); Basically, I apply the same logic as the sitemap plugin support does here: wn-blog-plugin/models/Post.php Lines 672 to 682 in 3fba603
I did this directly at the component level to avoid to load 20 times the post and category page object. If this looks good to @LukeTowers @bennothommo, I could reproduce these steps on all the blog and forum plugin's components. |
@RomainMazB happy for you to provide a PR with that fix - just change the |
@RomainMazB are you still interested in submitting a PR? |
All the four components of this plugin use model helper methods
setUrl
onPost
andCategory
models which generate the blog or category url from the corresponding page passed in parameter of the method:wn-blog-plugin/models/Category.php
Lines 87 to 94 in 3fba603
wn-blog-plugin/models/Post.php
Lines 157 to 176 in 3fba603
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:Same is true for the page integrating the
blogPosts
,blogCategories
andblogRssFeed
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:
This property value could be passed to the
setUrl
method and generate the good url structure like this:The text was updated successfully, but these errors were encountered: