54
Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireo Jisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14 Extend Joomla Forms Using Plugins

Extend Joomla Forms Using Plugins

  • Upload
    yireo

  • View
    432

  • Download
    6

Embed Size (px)

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.

Citation preview

Page 1: Extend Joomla Forms Using Plugins

Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14

Extend Joomla FormsUsing Plugins

Page 2: Extend Joomla Forms Using 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

Page 3: Extend Joomla Forms Using Plugins

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

Page 4: Extend Joomla Forms Using 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)

Page 5: Extend Joomla Forms Using Plugins

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

Page 6: Extend Joomla Forms Using Plugins

Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14

Part I:Events & JForm

Page 7: Extend Joomla Forms Using Plugins

Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14

Extensions

Component

Modules

Plugins

Libraries

Language files

Page 8: Extend Joomla Forms Using Plugins

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

Page 9: Extend Joomla Forms Using Plugins

Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14

Content events

onContentPrepareForm

onContentAfterSave

onContentAfterDelete

onContentBeforeDisplay

Page 10: Extend Joomla Forms Using Plugins

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

Page 11: Extend Joomla Forms Using Plugins

Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14

MVC

Model

View

Controller

Page 12: Extend Joomla Forms Using Plugins

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

Page 13: Extend Joomla Forms Using Plugins

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

Page 14: Extend Joomla Forms Using Plugins

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

Page 15: Extend Joomla Forms Using Plugins

Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14

Part II:Code

Page 16: Extend Joomla Forms Using Plugins

Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14

Content events

onContentPrepareForm

onContentAfterSave

onContentAfterDelete

onContentBeforeDisplay

Page 17: Extend Joomla Forms Using Plugins

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

Page 18: Extend Joomla Forms Using Plugins

Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14

Page 19: Extend Joomla Forms Using Plugins

Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14

Goal

Page 20: Extend Joomla Forms Using Plugins

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

Page 21: Extend Joomla Forms Using Plugins

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

Page 22: Extend Joomla Forms Using Plugins

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

Page 23: Extend Joomla Forms Using Plugins

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

Page 24: Extend Joomla Forms Using Plugins

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”

Page 25: Extend Joomla Forms Using Plugins

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)

Page 26: Extend Joomla Forms Using Plugins

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

Page 27: Extend Joomla Forms Using Plugins

Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14

XML manifest

<?xml version="1.0" encoding="utf­8"?><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=”en­GB”>en­GB.plg_content_example.ini</language>    <language tag=”en­GB”>en­GB.plg_content_example.sys.ini</language>  </languages></extension>

Page 28: Extend Joomla Forms Using Plugins

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

Page 29: Extend Joomla Forms Using Plugins

Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14

XML form

<?xml version="1.0" encoding="utf­8"?><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>

Page 30: Extend Joomla Forms Using Plugins

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

Page 31: Extend Joomla Forms Using Plugins

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{}

Page 32: Extend Joomla Forms Using Plugins

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) {}}

Page 33: Extend Joomla Forms Using Plugins

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

Page 34: Extend Joomla Forms Using Plugins

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;}

Page 35: Extend Joomla Forms Using Plugins

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;    }

    ...

Page 36: Extend Joomla Forms Using Plugins

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;}

Page 37: Extend Joomla Forms Using Plugins

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;}

Page 38: Extend Joomla Forms Using Plugins

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

Page 39: Extend Joomla Forms Using Plugins

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;    }

    ...

Page 40: Extend Joomla Forms Using Plugins

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;    }    ...

Page 41: Extend Joomla Forms Using Plugins

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;

}

Page 42: Extend Joomla Forms Using Plugins

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();    ...

Page 43: Extend Joomla Forms Using Plugins

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;}

Page 44: Extend Joomla Forms Using Plugins

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

Page 45: Extend Joomla Forms Using Plugins

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>';    }}

Page 46: Extend Joomla Forms Using Plugins

Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14

Part 3:Braindump

Page 47: Extend Joomla Forms Using Plugins

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

Page 48: Extend Joomla Forms Using Plugins

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)

Page 49: Extend Joomla Forms Using Plugins

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)

Page 50: Extend Joomla Forms Using Plugins

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

Page 51: Extend Joomla Forms Using Plugins

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();}

Page 52: Extend Joomla Forms Using Plugins

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;

Page 53: Extend Joomla Forms Using Plugins

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

Page 54: Extend Joomla Forms Using Plugins

Presentation “Extend Joomla Forms Using Plugins” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo @jissereitsma #jwc14

@yireo

opening up technology