Draft plugin for CakePHP 3
This Draft plugin give you the ability to quickly create draft system for your models., (*1)
This plugin is the Grafikart's Cakephp-Draft plugin for CakePHP 3.*, (*2)
, (*3)
Installation
Requirements
Steps to install
composer require romano83/cakephp3-draft:1.*
How to use
In your config\bootstrap.php
file, add this line, (*4)
Plugin::load('Romano83/cakephp3-draft');
Or, (*5)
Plugin::loadAll();
The plugin is now loaded and you can add the DraftBehavior
in your tables:, (*6)
<?php
namespace MyApp\Model\Table;
use Cake\ORM\Table;
class PostsTable extends Table
{
public function initialize(array $config)
{
$this->addBehavior('Romano83/Cakephp3Draft.Draft');
}
}
By default, the plugin use an "online" field to set the state of a content.
* online = -1 when the content is a draft
* online = 0 when the content is offline
* online = 1 when content is online, (*7)
Customization
If you want to use custom fields, you can override default behavior configuration :, (*8)
<?php
namespace MyApp\Model\Table;
use Cake\ORM\Table;
class PostsTable extends Table
{
public function initialize(array $config)
{
$this->addBehavior(
'Romano83/Cakephp3Draft.Draft', [
'conditions' => [
'draft' => 1
]
]
);
}
}
For instance, this configuration will use a "draft" field (set to 1), to create Draft., (*9)
Methods
With the plugin attached, the model will have new method, getDraftId(Table $table, array $conditions = [])
witch return draft ID.
The first parameter is a \Cake\ORM\Table
instance and the second one is an optional array., (*10)
How to implement this method
Here, is an example of how to use this method in your PostsController
:, (*11)
public function add(){
$post = $this->Posts->newEntity();
if($this->request->is(['post', 'put'])){
// Do your stuff here...
}else{
$post->id = $this->Posts->getDraftId($this->Posts); // get the last draft Id or create new one if needed
}
}
// OR
public function add(){
$post = $this->Posts->newEntity();
if($this->request->is(['post', 'put'])){
// Do your stuff here...
}else{
$post->id = $this->Posts->getDraftId($this->Posts, ['user_id' => 2]); // Get a draft Id for a content belonging to user 2 (or create a new one)
}
}
If you want to clean your table from drafts, you can implement this method:, (*12)
$this->Posts->cleanDrafts($this->Posts);
How to contribute
- Create a ticket in GitHub, if you have found a bug.
- Create a new branch if you want do a PR.
- Your code must follow the Coding Standard of CakePHP.
- You must add testcases if needed.
Special thanks
-
Grafikart for the first version of this plugin !