PHP code example of 2amigos / yii2-taggable-behavior
1. Go to this page and download the library: Download 2amigos/yii2-taggable-behavior library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
2amigos / yii2-taggable-behavior example snippets
use dosamigos\taggable\Taggable;
class Tour extends ActiveRecord
{
public function behaviors() {
return [
[
'class' => Taggable::className(),
],
];
}
}
/**
* @return \yii\db\ActiveQuery
*/
public function getTags()
{
return $this->hasMany(Tag::className(), ['id' => 'tag_id'])->viaTable('tbl_tour_tag_assn', ['tour_id' => 'id']);
}
/**
* @inheritdoc
*/
public function rules()
{
return [
// ...
[['tagNames'], 'safe'],
// ...
];
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
// for different configurations, please see the code
// we have created tables and relationship in order to
// use defaults settings
Taggable::className(),
];
}
// On TagController (example)
// actionList to return matched tags
public function actionList($query)
{
$models = Tag::findAllByName($query);
$items = [];
foreach ($models as $model) {
$items[] = ['name' => $model->name];
}
// We know we can use ContentNegotiator filter
// this way is easier to show you here :)
Yii::$app->response->format = Response::FORMAT_JSON;
return $items;
}
// On our form
<?= $form->field($model, 'tagNames')->widget(SelectizeTextInput::className(), [
// calls an action that returns a JSON object with matched
// tags
'loadUrl' => ['tag/list'],
'options' => ['class' => 'form-control'],
'clientOptions' => [
'plugins' => ['remove_button'],
'valueField' => 'name',
'labelField' => 'name',
'searchField' => ['name'],
'create' => true,
],
])->hint('Use commas to separate tags')
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.