Skip to content
This repository has been archived by the owner on Dec 3, 2018. It is now read-only.

Commit

Permalink
Merge pull request #106 from davidnewcomb/custom_sluggable_behavior
Browse files Browse the repository at this point in the history
add Podium.slugGenerator to specify your own slugGenerator
  • Loading branch information
Bizley committed Apr 23, 2017
2 parents fd9af65 + 48dbc47 commit 16b3f83
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 8 deletions.
13 changes: 13 additions & 0 deletions src/Podium.php
Expand Up @@ -5,6 +5,7 @@
use bizley\podium\log\DbTarget;
use bizley\podium\maintenance\Maintenance;
use bizley\podium\models\Activity;
use bizley\podium\slugs\PodiumSluggableBehavior;
use Yii;
use yii\base\Action;
use yii\base\Application;
Expand Down Expand Up @@ -52,6 +53,7 @@
* @property PodiumComponent $podiumComponent
* @property array $loginUrl
* @property array $registerUrl
* @property string $slugGenerator
*/
class Podium extends Module implements BootstrapInterface
{
Expand Down Expand Up @@ -164,6 +166,14 @@ class Podium extends Module implements BootstrapInterface
*/
public $denyCallback;

/**
* @var string Qualified name of class responsible for generating Podium slugs.
* You can implement your own generator by extending bizley\podium\slugs\PodiumSluggableBehavior -
* see this class for details.
* @since 0.8
*/
public $slugGenerator;

/**
* @inheritdoc
*/
Expand All @@ -185,6 +195,9 @@ public function init()
if (Yii::$app instanceof WebApplication) {
$this->podiumComponent->registerComponents();
$this->layout = 'main';
if (!is_string($this->slugGenerator)) {
$this->slugGenerator = PodiumSluggableBehavior::className();
}
} else {
$this->podiumComponent->registerConsoleComponents();
}
Expand Down
6 changes: 4 additions & 2 deletions src/models/db/CategoryActiveRecord.php
Expand Up @@ -3,9 +3,10 @@
namespace bizley\podium\models\db;

use bizley\podium\db\ActiveRecord;
use yii\behaviors\SluggableBehavior;
use yii\behaviors\TimestampBehavior;
use yii\helpers\HtmlPurifier;
use bizley\podium\Podium;
use bizley\podium\slugs\PodiumSluggableBehavior;

/**
* Category model
Expand Down Expand Up @@ -41,8 +42,9 @@ public function behaviors()
return [
TimestampBehavior::className(),
[
'class' => SluggableBehavior::className(),
'class' => Podium::getInstance()->slugGenerator,
'attribute' => 'name',
'type' => PodiumSluggableBehavior::CATEGORY
]
];
}
Expand Down
6 changes: 4 additions & 2 deletions src/models/db/ForumActiveRecord.php
Expand Up @@ -5,7 +5,8 @@
use bizley\podium\db\ActiveRecord;
use bizley\podium\models\Category;
use bizley\podium\models\Post;
use yii\behaviors\SluggableBehavior;
use bizley\podium\Podium;
use bizley\podium\slugs\PodiumSluggableBehavior;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveQuery;
use yii\helpers\HtmlPurifier;
Expand Down Expand Up @@ -46,8 +47,9 @@ public function behaviors()
return [
TimestampBehavior::className(),
[
'class' => SluggableBehavior::className(),
'class' => Podium::getInstance()->slugGenerator,
'attribute' => 'name',
'type' => PodiumSluggableBehavior::FORUM
]
];
}
Expand Down
5 changes: 3 additions & 2 deletions src/models/db/ThreadActiveRecord.php
Expand Up @@ -11,8 +11,8 @@
use bizley\podium\models\ThreadView;
use bizley\podium\models\User;
use bizley\podium\Podium;
use bizley\podium\slugs\PodiumSluggableBehavior;
use Yii;
use yii\behaviors\SluggableBehavior;
use yii\behaviors\TimestampBehavior;
use yii\helpers\HtmlPurifier;

Expand Down Expand Up @@ -101,8 +101,9 @@ public function behaviors()
return [
TimestampBehavior::className(),
[
'class' => SluggableBehavior::className(),
'class' => Podium::getInstance()->slugGenerator,
'attribute' => 'name',
'type' => PodiumSluggableBehavior::THREAD
],
];
}
Expand Down
5 changes: 3 additions & 2 deletions src/models/db/UserActiveRecord.php
Expand Up @@ -10,10 +10,10 @@
use bizley\podium\models\Mod;
use bizley\podium\models\User;
use bizley\podium\Podium;
use bizley\podium\slugs\PodiumSluggableBehavior;
use Exception;
use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\SluggableBehavior;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveQuery;
use yii\web\IdentityInterface;
Expand Down Expand Up @@ -69,8 +69,9 @@ public function behaviors()
return [
TimestampBehavior::className(),
[
'class' => SluggableBehavior::className(),
'class' => Podium::getInstance()->slugGenerator,
'attribute' => 'username',
'type' => PodiumSluggableBehavior::USER
],
];
}
Expand Down
57 changes: 57 additions & 0 deletions src/slugs/PodiumSluggableBehavior.php
@@ -0,0 +1,57 @@
<?php

namespace bizley\podium\slugs;

use yii\behaviors\SluggableBehavior;

/**
* Slug generator behavior
*
* To implement your own slug generators create a class that extends
* PodiumSluggableBehavior and override SluggableBehavior::generateSlug().
*
* For example add to configuration:
*
* ```
* 'modules' => [
* 'podium' => [
* 'class' => 'bizley\podium\Podium',
* 'slugGenerator' => MyPodiumSlugGenerator::className(),
* ```
*
* and then create a class:
*
* ```
* class MyPodiumSlugGenerator extends PodiumSluggableBehavior
* {
*
* protected function generateSlug($slugParts)
* {
* if ($this->type == self::THREAD) {
* $s = substr($slugParts[0], 1);
* return str_replace("/", "-", $s);
* } else {
* return parent::generateSlug ( $slugParts );
* }
* }
* }
* ```
*
* @author David Newcomb <david.newcomb@bigsoft.co.uk>
* @since 0.8
*/

class PodiumSluggableBehavior extends SluggableBehavior
{

const CATEGORY = "category";
const FORUM = "forum";
const THREAD = "thread";
const USER = "user";

/**
* @var string Use PodiumSluggableBehavior::CATEGORY,...
*/
public $type;

}

0 comments on commit 16b3f83

Please sign in to comment.