Sir Trevor plugin for CakePHP
November 9, 2013 · View on GitHub
A plugin for easily working with the brilliant Sir Trevor from Made by Many in your CakePHP projects.
Installation
The plugin can be installed by either cloning the repository, or adding it as a submodule to your CakePHP project. Both are done from the command line using Git from the root of your CakePHP project.
Cloning the repository
$ git clone https://github.com/martinbean/cakephp-sir-trevor-plugin.git app/Plugin/SirTrevor
Adding as a submodule
$ git submodule add https://github.com/martinbean/cakephp-sir-trevor-plugin.git app/Plugin/SirTrevor
$ git submodule init
Usage
The first step, as with any CakePHP plugin, is to initialize it in your app/Config/bootstrap.php file:
CakePlugin::load('SirTrevor');
Behavior
The plugin comes with a model behavior and some view helpers.
The behavior can either be added to your own content models, or used in conjuction with the Post model that comes with the plugin.
If you want to use the bundled Post model, then you’ll need to run the schema tool to create a posts table for you:
$ cake schema create --plugin SirTrevor
If you want to use the behavior with your own model, then simply attach it like you would any other model:
<?php
class Post extends AppModel {
public $actsAs = array(
'SirTrevor.SirTrevor'
);
}
By default, the behavior assumes your model and corresponding database table has a content field.
The content field merely holds the JSON representation of the Sir Trevor field.
The behavior converts this to a nice array to work with after finding records though.
Helper
The plugin comes with a couple of helpers for saving and displaying Sir Trevor content.
To enable them, add them to the $helpers array in your controllers:
<?php
class PostsController extends AppController {
public $helpers = array(
'SirTrevor.SirTrevor',
'SirTrevor.Markdown'
);
}
The SirTrevor helper contains a method to generate a <textarea> specifically for a Sir Trevor field.
The Markdown helper is used for converting content saved by a Sir Trevor instance into HTML.
Admin forms
To create an admin form in your view, simply use the method in the SirTrevor helper:
<?php
echo $this->Form->create('Post', array('action' => 'add'));
echo $this->Form->input('Post.title');
echo $this->SirTrevor->input('Post.content');
echo $this->Form->end( __('Save Post'));
?>
Displaying Sir Trevor content
The plugin comes with a Markdown library for rendering content saved by Sir Trevor as HTML. The plugin also comes with view elements for the default Sir Trevor block types. You can use them in your views as follows:
<?php
foreach ($post['Block'] as $block) {
$blockName = sprintf('SirTrevor.%s', $block['type']);
echo $this->element($blockName, array('data' => $block['data']));
}
?>
The plugin converts the JSON generated by Sir Trevor into a nice array to work with, so you can loop over blocks and render their data in either the provided view elements, or your own.
Notes
This plugin does not come with the actual Sir Trevor JavaScript library and CSS files. You will need to download these separately, and drop them into your css and js folders in your app’s webroot.
If you use the default model and field name, then you will need to instantiate a Sir Trevor instance in your admin forms as follows:
<script>
(function($) {
new SirTrevor.Editor({
el: $('#PostContent')
});
})(jQuery);
</script>
If you have any issues with this plugin then please feel free to create a new Issue on the GitHub repository. This plugin is licensed under the MIT License.