the post model provide a method called setUrl (from memory)
you will need to use it to set the url.
you can have a look at how it is done in the blog component blogPosts.php for example.
@chris Hmmmm without prior knowledge of the actual postPage URL (eg ‘blog/post’), this is actually quite difficult to get to work dynamically (for a single blog post).
@apinard the slug I know already that’s easy as it’s part of the model’s record
Hi @chris yes works fine for my own one (since I know the absolute pre-slug path), thing is I am building a plugin which will be used by not only myself, so this path could be different (/blog/post or /blog/{slug} or /blog/p/{slug} or /blog/{y}/{m}/{slug}, etc, etc) this why trying to determine the value that the user has defined for their own post URL format
you have two solutions:
if you are using your code in a component, then you can set a property that will hold the blog page.
If you want to be more generic, you can create a general settings holding the cms page value according to the theme, and you will be able to access this settings from anywhere your code.
Such an example of code is available in the Mall plugin for example.
you can also parse all the cms pages of the active theme, and search the ones containing the blogPost component and then grab the page url and page name as needed.
hi @chris yeah I was hoping to be able to do this via the rainlab component (load it programmatically), something like:
(pseudo code)
$rainLabBlogPosts = new RainLabComponent('blogPosts');
$rainLabBlogPosts->init(); // this loads the settings, eg the path for blog posts which it's probably parsing from the theme file as you proposed in your 2nd solution
$postPagePath = $rainLabBlogPosts->getBlogPostPath(); // /blog/post or /blog/{cat}/ etc
This will take some looking into…guess could parse through theme files even though it’s a bit urrrrrgly
public function getPagesByComponent($component)
{
$site = static::getSite();
$pages = Site::withContext($site->id, function () use ($site) {
// if you dont use multisite use this
// $theme = Theme::getActiveTheme();
$theme = Theme::load($site->theme);
return Page::listInTheme($theme, true);
});
$cmsPages = [];
foreach ($pages as $page) {
if (!$page->hasComponent($component)) {
continue;
}
$cmsPages[$page->baseFileName] = $page->title;
}
return count($cmsPages) < 1
? $this->allPages()
: $cmsPages;
}