extend joomla forms using plugins
DESCRIPTION
Presentation given by Jisse Reitsma during Joomla World Conference 2014 in Cancun, on how to write a Joomla Content Plugin that allows you to add or modify existing Joomla Forms.TRANSCRIPT
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Extend Joomla FormsUsing Plugins
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
My name is Jisse Reitsma
Developer
Owner of Yireo
Joomla! & Magento
Extensions & services
Tutorials & blogs
Sponsoring JWC2014
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Plugins I wrote
Language Domains
Auto Login IP
Demo Site
Fancybox
HTTP Authenticatie
New Relic
Piwik
WebP
ScriptMerge
SEF Test
SSL Redirection
Static Content
Trademark
32+ MageBridge plugins
10+ SimpleLists plugins
8+ Dynamic404 plugins
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Book release in November 2014
English
368 pages
Available through:
Yireo Shop (Europe)
Amazon (US & world wide)
#deadtreeformat (no ebook)
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Extend Joomla Forms Using Plugins
Part I - Events & JForm
Part II - Code
Part III - Braindump
Presentation online: http://slideshare.net/yireo
Tweets: @yireo / @jissereitsma
Code: github.com/yireo/JoomlaPluginsBook
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Part I:Events & JForm
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Extensions
Component
Modules
Plugins
Libraries
Language files
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Plugin groups
Authentication
Content
System
User
Editors
Editor Buttons
Search
Finder
CAPTCHA
Extension
Quick Icon
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Content events
onContentPrepareForm
onContentAfterSave
onContentAfterDelete
onContentBeforeDisplay
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
JForm
Joomla core library for handling forms
XML form definition + PHP logic
Tasks
Form generation (HTML)
Validation
Filtering
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
MVC
Model
View
Controller
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
MVC workflow (simple)
User requests page
Controller calls view
View fetches data from model
View shows page
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
MVC workflow (form)
User requests form
Controller calls view
View shows form
User submits form (POST)
Controller picks up on POST
Controller validates & filters form
Model stores form data
Controller redirects back to form or other page
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
MVC workflow (form)
User requests form
Controller calls view
View shows form (JForm)
User submits form (POST)
Controller picks up on POST
Controller validates & filters form (JForm)
Model stores form data
Controller redirects back to form or other page
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Part II:Code
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Content events
onContentPrepareForm
onContentAfterSave
onContentAfterDelete
onContentBeforeDisplay
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Goal
Add a custom field to backends article form
Store that fields value in a separate database table
Show that fields value in the same form again
Show that fields value in the frontend
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Goal
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Goal
Add a custom field to backends article form
Store that fields value in a separate database table
Show that fields value in the same form again
Show that fields value in the frontend
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Goal
Add a custom field to backends article form
onContentPrepareForm
Store that fields value in a separate database table
onContentAfterSave
Show that fields value in the same form again
onContentPrepareForm
Show that fields value in the frontend
onContentBeforeDisplay
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Content Plugin files
plugins/content/example/example.php
plugins/content/example/example.xml
plugins/content/exampe/form/default.xml
administrator/languages/en-GB/en-GB.plg_content_example.ini
administrator/languages/en-GB/en-GB.plg_content_example.sys.ini
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Content Plugin files
plugins/content/example/example.php
plugins/content/example/example.xml
plugins/content/exampe/form/default.xml
administrator/languages/en-GB/en-GB.plg_content_example.ini
administrator/languages/en-GB/en-GB.plg_content_example.sys.ini
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Language files (1/2)
PLG_CONTENT_EXAMPLE="Content Example"PLG_CONTENT_EXAMPLE_DESC="An example Content Plugin"
PLG_CONTENT_EXAMPLE_FIELD_EXAMPLE_LABEL=”Test”PLG_CONTENT_EXAMPLE_FIELD_EXAMPLE_DESC=”New field”
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Language files (2/2)
*.sys.ini = Always loaded
Add plugin title + description, but not much more
*.ini = Only loaded specifically
When editing a plugin in backend
When showing plugin in frontend ($autoLoadLanguage = true)
Add all language strings you need (backend + frontend)
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Content Plugin files
plugins/content/example/example.php
plugins/content/example/example.xml
plugins/content/exampe/form/default.xml
administrator/languages/en-GB/en-GB.plg_content_example.ini
administrator/languages/en-GB/en-GB.plg_content_example.sys.ini
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
XML manifest
<?xml version="1.0" encoding="utf8"?><extension version="3.0" type="plugin" group="content"> <name>PLG_CONTENT_EXAMPLE</name> <description>PLG_CONTENT_EXAMPLE_DESC</description> <version>0.0.1</version> <files> <filename plugin=”example”>example.php</filename> <folder>form</folder> </files> <languages> <language tag=”enGB”>enGB.plg_content_example.ini</language> <language tag=”enGB”>enGB.plg_content_example.sys.ini</language> </languages></extension>
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Content Plugin files
plugins/content/example/example.php
plugins/content/example/example.xml
plugins/content/exampe/form/default.xml
administrator/languages/en-GB/en-GB.plg_content_example.ini
administrator/languages/en-GB/en-GB.plg_content_example.sys.ini
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
XML form
<?xml version="1.0" encoding="utf8"?><form> <fields> <fieldset name="jmetadata"> <field name="example" type="text" label="PLG_CONTENT_EXAMPLE_FIELD_EXAMPLE_LABEL" description="PLG_CONTENT_EXAMPLE_FIELD_EXAMPLE_DESC" /> </fieldset> </fields></form>
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Content Plugin files
plugins/content/example/example.php
plugins/content/example/example.xml
plugins/content/exampe/form/default.xml
administrator/languages/en-GB/en-GB.plg_content_example.ini
administrator/languages/en-GB/en-GB.plg_content_example.sys.ini
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Content Plugin - basic structure
<?phpdefined('_JEXEC') or die();
class plgContentExample extends JPlugin{}
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Content Plugin - basic structure
<?phpdefined('_JEXEC') or die();
class plgContentExample extends JPlugin{ public function onContentPrepareForm($form, $data) {} public function onContentAfterSave($context, $item, $isNew) {} public function onContentBeforeDisplay($context, $item, $params, $page) {}}
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Goal
Add a custom field to backends article form
onContentPrepareForm
Store that fields value in a separate database table
onContentAfterSave
Show that fields value in the same form again
onContentPrepareForm
Show that fields value in the frontend
onContentBeforeDisplay
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
onContentPrepareForm
public function onContentPrepareForm($form, $data){ ... JForm::addFormPath(__DIR__.'/form'); // subfolder “form” $form>loadFile('default'); // file “form/default.xml” ... return true;}
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
onContentPrepareForm
public function onContentPrepareForm($form, $data){ if (!($form instanceof JForm)) { $this>_subject>setError('JERROR_NOT_A_FORM'); return false; }
$name = $form>getName(); if (!in_array($name, array('com_content.article'))) { return true; }
...
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
onContentPrepareForm
... if(!empty($data>id))
{
$data = $this>loadExample($data);
}
return true;}
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
loadExample
protected function loadExample($data){ $db = JFactory::getDbo(); $query = $db>getQuery(true); $query>select('*') >from($db>quoteName('#__example')) >where($db>quoteName('content_id') . ' = '.$data>id); $db>setQuery($query);
$exampleData = $db>loadAssoc(); $data>example = $exampleData['example']; return $data;}
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Goal
Add a custom field to backends article form
onContentPrepareForm
Store that fields value in a separate database table
onContentAfterSave
Show that fields value in the same form again
onContentPrepareForm
Show that fields value in the frontend
onContentBeforeDisplay
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
onContentAfterSave
public function onContentAfterSave( $context, $article, $isNew) { if (!in_array($context, array('com_content.article'))) { return true; }
...
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
onContentAfterSave
... $jinput = JFactory::getApplication()>input; $form = $jinput>post>get('jform', null, 'array'); if (is_array($form) && isset($form['example'])) { $example = $form['example']; } else { return true; } ...
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
onContentAfterSave
... $content_id = $article>id; $this>saveExample($content_id, $context, $example); return true;
}
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
saveExample
protected function saveExample($content_id, $context, $test){ $db = JFactory::getDbo();
$query = $db>getQuery(true); $query>select($db>quoteName('content_id')) >from($db>quoteName('#__example')) >where($db>quoteName('content_id').'='.$content_id);
$db>setQuery($query); $db>execute() $exists = (bool)$db>getNumRows(); ...
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
saveExample
... $data = new stdClass(); $data>content_id = $content_id; $data>context = $context; $data>example = $example; if($exists) { $result = $db>updateObject('#__example', $data, 'content_id'); } else { $result = $db>insertObject('#__example', $data); }
return true;}
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Goal
Add a custom field to backends article form
onContentPrepareForm
Store that fields value in a separate database table
onContentAfterSave
Show that fields value in the same form again
onContentPrepareForm
Show that fields value in the frontend
onContentBeforeDisplay
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
onContentBeforeDisplay
public function onContentBeforeDisplay( $context, &$row, &$params, $page = 0){ if(!empty($row>id)) { $row = $this>loadExample($row); }
if (!empty($row>example)) { $row>text .= '<p>EXAMPLE: '.$row>example.'<p>'; }}
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Part 3:Braindump
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Forms are everywhere
Article form
Category form
User profile form
Menu-Item form
Contact form
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
JForm manipulation
$form->load($xml)
$form->loadFile($file)
$form->bind($data)
$form->getField($name, $group, $value)
$form->removeField($field)
$form->setField($xml)
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
JForm output
$form->getInput($field, $group)
$form->getLabel($field, $group)
$form->getName($field, $group)
$form->getValue($field, $group)
$form->renderField($field, $group)
$form->getFieldset($fieldset)
$form->getFieldsets($group)
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Showing new field values
4 times the same event
onContentPrepare
onContentAfterTitle
onContentBeforeDisplay
onContentAfterDisplay
Template override
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Loading a language file - Joomla! 2.5
public function __construct(& $subject, $config){ parent::__construct($subject, $config); $this>loadLanguage();}
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
Loading a language file - Joomla! 3.x
protected $autoloadLanguage = true;
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
github.com/yireo/JoomlaPluginsBook/chapter05
https://github.com/yireo/JoomlaPluginsBook/tree/master/chapter05
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14
@yireo
opening up technology