Skip to content
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

add flexibility to Post setUrl method #506

Merged
merged 7 commits into from
Jul 11, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions components/Posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ protected function prepareVars()
protected function listPosts()
{
$category = $this->category ? $this->category->id : null;
$categorySlug = $this->category ? $this->category->slug : null;

/*
* List all the posts, eager load their categories
Expand All @@ -219,8 +220,8 @@ protected function listPosts()
/*
* Add a "url" helper attribute for linking to each post and category
*/
$posts->each(function($post) {
$post->setUrl($this->postPage, $this->controller);
$posts->each(function($post) use ($categorySlug) {
$post->setUrl($this->postPage, $this->controller, ['category' => $categorySlug]);

$post->categories->each(function($category) {
$category->setUrl($this->categoryPage, $this->controller);
Expand Down
14 changes: 8 additions & 6 deletions models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,19 @@ public function beforeSave()
* Sets the "url" attribute with a URL to this object.
* @param string $pageName
* @param Controller $controller
* @param array $params Override request URL parameters
*
* @return string
*/
public function setUrl($pageName, $controller)
public function setUrl($pageName, $controller, $params = [])
{
$params = [
'id' => $this->id,
'slug' => $this->slug
];
$category = $this->categories->count() ? $this->categories->first()->slug : null;
LukeTowers marked this conversation as resolved.
Show resolved Hide resolved

$params['category'] = $this->categories->count() ? $this->categories->first()->slug : null;
$params = array_merge([
'id' => $this->id,
'slug' => $this->slug,
LukeTowers marked this conversation as resolved.
Show resolved Hide resolved
'category' => $category,
], $params);

bennothommo marked this conversation as resolved.
Show resolved Hide resolved
// Expose published year, month and day as URL parameters.
if ($this->published) {
Expand Down