joomla! plugin development

45
Presentation “Joomla! Plugin Development” - http://slideshare.net/yireo Jisse Reitsma ([email protected]) - Twitter @yireo Joomla! plugin development

Upload: yireo

Post on 10-May-2015

3.263 views

Category:

Technology


0 download

DESCRIPTION

Presentation on Joomla! plugin development

TRANSCRIPT

Page 1: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Joomla! plugindevelopment

Page 2: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

My name is Jisse Reitsma

Developer

Owner of Yireo

Joomla! & Magento

Extensies & development

Tutorials & blogs

Page 3: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Plugins I wrote so far

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: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Book release in Autumn 2014

English

About 200-300 pages

Complete reference guide

Available through Amazon (plus some other channels)

Page 5: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Developing plugins yourself

Part I - Why a plugin?

Part II - A bit of programming

Part III - Hmmm, braindump

Presentation online: http://slideshare.net/yireo

Tweets: @yireo

Page 6: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Part 1:Why a plugin?

Page 7: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Joomla! extensions

Component

Modules

Plugins

Libraries

Language files

Page 8: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Definition of a Joomla! plugin

A Joomla! extension that offers functionality based on a triggered event. The Joomla! core has various core-events by default, but every Joomla! extension can trigger additional events.

Page 9: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Plugin-groups (core)

Authentication

Content

System

User

Editors

Editors Xtd

Search

Finder

CAPTCHA

Extension

Quickicon

Page 10: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Plugin-groups (3rd party)

VirtueMart [product, custom]

JomSocial [community]

K2 [fields]

Yireo

Dynamic404 Match Lookups

SimpleLists Content / Link

MageBridge Product Sales / Newsletter Subscriptions

Page 11: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

User-plugin events

onUserLogin

onUserLogout

onUserAuthenticate

onUserLoginFailure

onUserBeforeSave / onUserAfterSave

onUserBeforeDelete / onUserAfterDelete

Page 12: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

System-plugin events

onAfterInitialise

onAfterRoute

onAfterDispatch

onBeforeRender / onAfterRender

onBeforeCompileHead

Page 13: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Part 2:A bit of programming

Page 14: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Reasons for a custom plugin

Modifying HTML-code

Tweaking document-headers

Extending an user-formulier

Extra authentication-type

... and a lot more

Page 15: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

System Plugin files

plugins/system/example/example.php

plugins/system/example/example.xml

plugins/system/example/index.html

administrator/languages/en-GB/en-GB.plg_system_example.ini

administrator/languages/en-GB/en-GB.plg_system_example.sys.ini

Page 16: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

System Plugin files

plugins/system/example/example.php

plugins/system/example/example.xml

plugins/system/example/index.html

administrator/languages/en-GB/en-GB.plg_system_example.ini

administrator/languages/en-GB/en-GB.plg_system_example.sys.ini

Page 17: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Language files (1/2)

PLG_SYSTEM_EXAMPLE="System ­ Example"PLG_SYSTEM_EXAMPLE_DESC="An example System Plugin"

Page 18: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

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 19: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

System Plugin files

plugins/system/example/example.php

plugins/system/example/example.xml

plugins/system/example/index.html

administrator/languages/en-GB/en-GB.plg_system_example.ini

administrator/languages/en-GB/en-GB.plg_system_example.sys.ini

Page 20: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

index.html

Page 21: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

System Plugin files

plugins/system/example/example.php

plugins/system/example/example.xml

plugins/system/example/index.html

administrator/languages/en-GB/en-GB.plg_system_example.ini

administrator/languages/en-GB/en-GB.plg_system_example.sys.ini

Page 22: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

XML-manifest

<?xml version="1.0" encoding="utf­8"?><extension version="3.0" type="plugin" group="system">  <name>PLG_SYSTEM_EXAMPLE</name>  <description>PLG_SYSTEM_EXAMPLE_DESC</description>  <version>0.0.1</version>  <files>    <filename plugin=”example”>example.php</filename>    <filename>index.html</filename>  </files>  <languages>    <language tag=”en­GB”>en­GB.plg_system_example.ini</language>    <language tag=”en­GB”>en­GB.plg_system_example.sys.ini</language>  </languages></extension>

Page 23: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

System Plugin files

plugins/system/example/example.php

plugins/system/example/example.xml

plugins/system/example/index.html

administrator/languages/en-GB/en-GB.plg_system_example.ini

administrator/languages/en-GB/en-GB.plg_system_example.sys.ini

Page 24: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

System Plugin - basic structure

<?phpdefined('_JEXEC') or die();jimport( 'joomla.plugin.plugin' );class plgSystemExample extends JPlugin{}

Page 25: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Reasons for a custom plugin

Modifying HTML-code

Tweaking document-headers

Extending an user-formulier

Extra authentication-type

... and a lot more

Page 26: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

System Plugin - hacking HTML

<?phpdefined('_JEXEC') or die();jimport( 'joomla.plugin.plugin' );class plgSystemExample extends JPlugin{    public function onAfterRender()    {        $body = JResponse::getBody();        // @todo: Do something with the $body        JResponse::setBody($body);    }}

Page 27: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Reasons for a custom plugin

Modifying HTML-code

Tweaking document-headers

Extending an user-formulier

Extra authentication-type

... and a lot more

Page 28: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

System Plugin - add headers

<?phpdefined('_JEXEC') or die();jimport( 'joomla.plugin.plugin' );class plgSystemExample extends JPlugin{    public function onAfterDispatch()    {        $document = JFactory::getDocument();        $document­>setGenerator('drupal');        $document­>addScriptDeclaration($script);    }}

Page 29: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Reasons for a custom plugin

Modifying HTML-code

Tweaking document-headers

Extending an user-formulier

Extra authentication-type

... and a lot more

Page 30: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Content Plugin - remove a field

<?phpdefined('_JEXEC') or die();jimport( 'joomla.plugin.plugin' );class plgContentExample extends JPlugin{    public function onContentPrepareForm($form, $data)    {        $form­>removeField('name', 'profile');        return true;    }}

Page 31: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Reasons for a custom plugin

Modifying HTML-code

Tweaking document-headers

Extending an user-formulier

Extra authentication-type

... and a lot more

Page 32: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Authenticatie Plugin (1/1)

<?phpdefined('_JEXEC') or die();jimport( 'joomla.plugin.plugin' );class plgAuthenticationExample extends JPlugin{  public function onUserAuthenticate($credentials, $options, &$response )  {    if ($credentials['password'] == 'joomla') {      $response­>status = JAuthentication::STATUS_SUCCESS;      $response­>error_message = '';    } else {      $response­>status = JAuthentication::STATUS_FAILURE;      $response­>error_message = JText::_('JGLOBAL_AUTH_INVALID_PASS')    }  }}

Page 33: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Reasons for a custom plugin

Modifying HTML-code

Tweaking document-headers

Extending an user-formulier

Extra authentication-type

... and a lot more

Page 34: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Part 3:Braindump

Page 35: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Calling a content-event [1/2]

Scenario: Own component is offering content-object ($item)

Purpose is to modify the $item title through extra plugins

Use of content-event onContentContent

All $item-properties can be modified through this event

Dispatch event in component-view

For example in views/item/view.html.php

Page 36: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Calling a content-event [2/2]

JPluginHelper::importPlugin( 'content' );

$dispatcher = JDispatcher::getInstance();

$arguments = array(&$item, &$item_params, 0);

$results = $dispatcher­>trigger('onPrepareContent', $arguments

);

Note $item is a PHP reference (ampersand &).

Return-variabele $results contains at most an event-status.

Page 37: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Calling your own event (1/3)

Create a custom component

components/com_example

Create a custom plugin-group

plugins/custom

Create a custom plugin

plugins/custom/example/example.php

plugins/custom/example/example.xml

plugins/custom/example/index.html

Page 38: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Calling your own event (2/3)

jimport( 'joomla.plugin.plugin' );class plgCustomExample extends JPlugin{    public function doSomething()    {

        // @todo: Do something    }}

Page 39: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Calling your own event (3/3)

Add this to the right place in your MVC-component:

JPluginHelper::importPlugin('custom');$dispatcher = JDispatcher::getInstance();$arguments = null;$results = $dispatcher­>trigger(

'doSomething', $arguments);

Page 40: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Checks (1/2)

Handy variables:$app = JFactory::getApplication();$document = JFactory::getDocument();

Check whether the current application is the frontend:if(!JFactory::getApplication()­>isSite()) return false;

Check whether the current output is an HTML document:if (JFactory::getDocument()­>getType() != 'html') return false;

Page 41: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Checks (2/2)

Ignore A JAX-requests:$input = JFactory::getApplication()­>input;if ($input­>getCmd('tmpl') == 'component') return false;if ($input­>getCmd('format') == 'raw') return false;if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') return false;

Page 42: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Parameters in J1.5 vs J2.5

private function getParams(){  jimport('joomla.version');  $version = new JVersion();  if(version_compare($version­>RELEASE, '1.5', 'eq')) {    $plugin = JPluginHelper::getPlugin('system','example');       $params = new JParameter($plugin­>params);    return $params;  } else {    return $this­>params;  }}

Page 43: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Loading a language file - Joomla! 2.5

public function __construct(& $subject, $config){  parent::__construct($subject, $config);  $this­>loadLanguage();}

Page 44: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

Loading a language file - Joomla! 3.x

protected $autoloadLanguage = true;

Page 45: Joomla! Plugin Development

Presentation “Joomla! Plugin Development” - http://slideshare.net/yireoJisse Reitsma ([email protected]) - Twitter @yireo

tweet @yireo