-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow custom slug name for Posts and Post component (#394)
Fixes an issue that prevents the use of anything but "slug" as the post slug property. This merge also contains abstraction of some URL helper methods for all blog components. Credit to @GinoPane. Fixes #392.
- Loading branch information
Showing
7 changed files
with
167 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php namespace RainLab\Blog\Classes; | ||
|
||
use Cms\Classes\Page; | ||
use Cms\Classes\Theme; | ||
use Cms\Classes\ComponentBase; | ||
|
||
abstract class ComponentAbstract extends ComponentBase | ||
{ | ||
/** | ||
* Reference to the page name for linking to posts | ||
* | ||
* @var string | ||
*/ | ||
public $postPage; | ||
|
||
/** | ||
* Reference to the page name for linking to categories | ||
* | ||
* @var string | ||
*/ | ||
public $categoryPage; | ||
|
||
/** | ||
* @param string $componentName | ||
* @param string $page | ||
* @return ComponentBase|null | ||
*/ | ||
protected function getComponent(string $componentName, string $page) | ||
{ | ||
$component = null; | ||
|
||
$page = Page::load(Theme::getActiveTheme(), $page); | ||
|
||
if (!is_null($page)) { | ||
$component = $page->getComponent($componentName); | ||
} | ||
|
||
return $component; | ||
} | ||
|
||
/** | ||
* A helper function to get the real URL parameter name. For example, slug for posts | ||
* can be injected as :post into URL. Real argument is necessary if you want to generate | ||
* valid URLs for such pages | ||
* | ||
* @param ComponentBase|null $component | ||
* @param string $name | ||
* | ||
* @return string|null | ||
*/ | ||
protected function urlProperty(ComponentBase $component = null, string $name = '') | ||
{ | ||
$property = null; | ||
|
||
if ($component !== null && ($property = $component->property($name))) { | ||
preg_match('/{{ :([^ ]+) }}/', $property, $matches); | ||
|
||
if (isset($matches[1])) { | ||
$property = $matches[1]; | ||
} | ||
} else { | ||
$property = $name; | ||
} | ||
|
||
return $property; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters