Create your own page type plugin
To create our own page type plugin we will need to know some basic Laravel skills. This simplest plugin only contains a blade template.
For example, the Preview plugin:
When editing a page, it will show a Preview tab and display some useful links. The plugin setting plugin.page-tab-preview value of this Preview plugin is below:
{
"blade_file": "preview",
"tab_name": "__(preview)",
"php_class": ""
}
Code explain:
- How the CMS know it's a plugin? There is a record in CMS setting page, it's plugin.page-tab-preview. It's also the plugin view folder name. It must start with page-tab- as it will attach to the page create/edit form.
- "blade_file": It's the blade template file in the resource/views/vendor/laravel-cms/plugins/page-tab-preview/preview.blade.php
- "tab_name": The tab name display on the page create/edit form. We can leave it empty if we want to hide it.
- "php_class": We don't need any PHP class for this plugin, so leave it empty.
Example of Sub Page plugin:
{
"plugin_name": "Sub Page",
"blade_file": "sub-page",
"tab_name": "__(sub,page)",
"php_class": "App\\LaravelCms\\Plugins\\SubPage\\Controllers\\SubPageController",
"number_per_page": 40,
"display_main_image": false
}
Code explain:
- "php_class": The main controller PHP full classpath of this plugin. Every action of create/edit/store/update/destory will trigger the same method in this class. We can put the class in any location, in our app controller folder or even in the vendor. eg. App\\Http\\Controllers\\MyCmsPlugin
- "number_per_page": this is a variable our plugin to use in the PHP class or template. We can set up as many variables as we want.
When the methods of the php_class will be triggered?
- The php_class can has create/edit/store/update/destory/show methods. This same method will trigger when the same action happens.
- eg. When we click a link to edit a page, The edit() method of our php_class will be invoked, we can pass some extra variables to our plugin blade template.
- When click the save button to update the page, the update() method of the php_class will be invoked. We can do some extra update logic here.
- When create a new page, the create() method of our php_class will be invoked & store() method invoked after click the save button.
- Same as the show() & destory() methods. It's use the same stand method name of the Laravel controller.
- We can only keep the methods we need. eg. the Sub Page plugin only has edit() & update() methods.
- The page type plugin does need the show() & index() methods.
What parameters I can use for the methods?
- store($form_data, $page, $plugin_settings) & update($form_data, $page, $plugin_settings):
$form_data includes all the data submitted from the page form, include the data from ALL plugin blade templates.
$page is the instance of the saved page, it will NOT include the data of the plugins.
$plugin_settings are the settings of our plugin from the JSON data. it's an array. - edit($id, $page, $plugin_settings) & detory($id, $page, $plugin_settings)
$id is the page id, $page & $plugin_settings are the same as above - create($form_data, $page, $plugin_settings)
$form_data is NULL for create() method
$page is NULL for create() method
$plugin_settings are the settings of our plugin from the JSON data. it's an array. - Click here to see the source code of the Sub Page controller for better understand.
Step by step myplugin001 example:
- Go to the folder your-laravel-project\vendor\alexstack and copy laravel-cms-plugin-sub-page to laravel-cms-plugin-myplugin001
- Go to the folder laravel-cms-plugin-myplugin001 replace code or rename folder/filename from sub-page to myplugin001
- Go to the cms setting page, add a plugin, name: page-tab-myplugin001 value:
{ "blade_file": "myplugin001", "tab_name": "__(myplugin001)", "php_class": "" }
- Copy the view folder from your-laravel-project\vendor\alexstack\laravel-cms-plugin-myplugin001\src\resources\views\plugins\page-tab-myplugin001 to your-laravel-project\resources\views\vendor\laravel-cms\plugins\page-tab-myplugin001
- Now you can find the myplugin001 page tab when you add or edit a page. It will display the blade template content.