TYPO3 Extension development using new Extbase framework

81
TYPO3 Extension development using new Extbase framework 2011/02/18, T3Ski11 Tokyo Christian Trabold

description

My presentation for the TYPO3 community day in Tokyo, Japan. The code is available at https://github.com/ctrabold/t3ski-workshop. Due to copyright issues I had to remove all pictures of Miffy.

Transcript of TYPO3 Extension development using new Extbase framework

Page 1: TYPO3 Extension development using new Extbase framework

TYPO3 Extension development using new Extbase framework

2011/02/18, T3Ski11 TokyoChristian Trabold

Page 2: TYPO3 Extension development using new Extbase framework

About me

Christian TraboldSenior Developerdkd Internet Service GmbH

TYPO3 since 2004

Focus on quality assurance

Author of TYPO3 Cookbook, O'Reilly

Page 3: TYPO3 Extension development using new Extbase framework
Page 4: TYPO3 Extension development using new Extbase framework

320 x 480Vertical Resolution

Carrier 12:34 PMWeb Page Title

http://host.domain.tld

Page 5: TYPO3 Extension development using new Extbase framework

Agenda - Part 1

- TYPO3 basics

- Find extensions with TER

- Get your ideas into TYPO3

- Question and answers

Page 6: TYPO3 Extension development using new Extbase framework

Agenda - Part 2

- Create example application

- See kickstarter in action

- Customize !les to your needs

- Question and answers

Page 7: TYPO3 Extension development using new Extbase framework

Goal of this workshop

Page 8: TYPO3 Extension development using new Extbase framework

Get the big picture

Page 9: TYPO3 Extension development using new Extbase framework

Understand concepts

Page 10: TYPO3 Extension development using new Extbase framework

Find nice Sushi bars

Page 11: TYPO3 Extension development using new Extbase framework
Page 12: TYPO3 Extension development using new Extbase framework
Page 13: TYPO3 Extension development using new Extbase framework

320 x 480Vertical Resolution

Carrier 12:34 PMWeb Page Title

http://host.domain.tld

Page 14: TYPO3 Extension development using new Extbase framework

Part 1TYPO3 Basics

Page 15: TYPO3 Extension development using new Extbase framework

http://www.flickr.com/photos/grendelkhan/121671781/sizes/l/in/photostream/

Extensions

Frontend

Backend

TYPO3 is like Sushi

Page 16: TYPO3 Extension development using new Extbase framework

Extensions extendeverything

Page 17: TYPO3 Extension development using new Extbase framework

Extensions containyour PHP code

Page 18: TYPO3 Extension development using new Extbase framework

Extensions containyour ideas

Page 19: TYPO3 Extension development using new Extbase framework

Inspiring people to share

Page 20: TYPO3 Extension development using new Extbase framework

How to !nd extensions

Page 21: TYPO3 Extension development using new Extbase framework

typo3.org/extensions/

Page 22: TYPO3 Extension development using new Extbase framework

List extensions

Page 23: TYPO3 Extension development using new Extbase framework

Extension details

Page 24: TYPO3 Extension development using new Extbase framework

View !les

Page 25: TYPO3 Extension development using new Extbase framework

Extension manager

Page 26: TYPO3 Extension development using new Extbase framework

Extension details

Page 27: TYPO3 Extension development using new Extbase framework

Read documentation

Page 28: TYPO3 Extension development using new Extbase framework

Look into !les

Page 29: TYPO3 Extension development using new Extbase framework

Security and con!dence

It is very important that you check every detail of an extensionbefore you install.

Page 30: TYPO3 Extension development using new Extbase framework

What if nothing !ts?

Page 31: TYPO3 Extension development using new Extbase framework

http://www.flickr.com/photos/rreid/420388478/

Add your own creation

Page 32: TYPO3 Extension development using new Extbase framework

How do you startyour own creation?

Page 33: TYPO3 Extension development using new Extbase framework

Understandextension structure

Page 34: TYPO3 Extension development using new Extbase framework

Install extbase kickstarter

Page 35: TYPO3 Extension development using new Extbase framework

Andstart coding!

Page 36: TYPO3 Extension development using new Extbase framework

Why do I need a kickstarter?

Page 37: TYPO3 Extension development using new Extbase framework

Faster and less errors

Page 38: TYPO3 Extension development using new Extbase framework

The kickstarter does all the boring work

Page 39: TYPO3 Extension development using new Extbase framework

You can focus on your ideas and design

= more fun!

Page 40: TYPO3 Extension development using new Extbase framework

TYPO3 has old and new extension styles

Old pi_baseNew extbase

Page 41: TYPO3 Extension development using new Extbase framework

Old pi_baseNew extbase

We will focus on the new extbase kickstarter

Page 42: TYPO3 Extension development using new Extbase framework

!"" ChangeLog!"" doc#   !"" wizard_form.dat#   %"" wizard_form.html!"" ext_emconf.php!"" ext_icon.gif!"" ext_localconf.php!"" ext_tables.php!"" ext_tables.sql!"" flexform_tx_sushifinder_bars_flex.xml!"" icon_tx_sushifinder_bars.gif!"" locallang_db.xml!"" pi1#   %"" class.tx_sushifinder_pi1.php!"" README.txt%"" tca.php

Old = less structurehard to maintain

Page 43: TYPO3 Extension development using new Extbase framework

!"" ChangeLog!"" doc#   !"" wizard_form.dat#   %"" wizard_form.html!"" ext_emconf.php!"" ext_icon.gif!"" ext_localconf.php!"" ext_tables.php!"" ext_tables.sql!"" flexform_tx_sushifinder_bars_flex.xml!"" icon_tx_sushifinder_bars.gif!"" locallang_db.xml!"" pi1#   %"" class.tx_sushifinder_pi1.php!"" README.txt%"" tca.php

You tend to build huge classes

?

Page 44: TYPO3 Extension development using new Extbase framework

<?php/*************************************************************** * Copyright notice * * (c) 1999-2004 Kasper SkÌ´rhÌüj ([email protected]) * (c) 2004-2009 Rupert Germann ([email protected]) * All rights reserved * * This script is part of the TYPO3 project. The TYPO3 project is * free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * The GNU General Public License can be found at * http://www.gnu.org/copyleft/gpl.html. * A copy is found in the textfile GPL.txt and important notices to the license * from the author is found in LICENSE.txt distributed with these scripts. * * * This script is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * This copyright notice MUST APPEAR in all copies of the script! ***************************************************************//** * class.tx_ttnews.php * * versatile news system for TYPO3. * $Id: class.tx_ttnews.php 27058 2009-11-27 12:24:02Z rupi $ * * TypoScript setup: * @See ext/tt_news/pi/static/ts_new/setup.txt * @See tt_news Manual: http://typo3.org/documentation/document-library/extension-manuals/tt_news/current/ * @See TSref: http://typo3.org/documentation/document-library/references/doc_core_tsref/current/ * * @author Rupert Germann <[email protected]> * @co-author Ingo Renner <[email protected]> */

require_once (PATH_tslib . 'class.tslib_pibase.php');require_once (PATH_t3lib . 'class.t3lib_htmlmail.php');

require_once (t3lib_extMgm::extPath('tt_news') . 'lib/class.tx_ttnews_catmenu.php');require_once (t3lib_extMgm::extPath('tt_news') . 'lib/class.tx_ttnews_helpers.php');require_once (t3lib_extMgm::extPath('tt_news') . 'lib/class.tx_ttnews_cache.php');

Page 45: TYPO3 Extension development using new Extbase framework

* * @author Rupert Germann <[email protected]> * @package TYPO3 * @subpackage tt_news */class tx_ttnews extends tslib_pibase { // Default plugin variables: var $prefixId = 'tx_ttnews'; // Same as class name var $scriptRelPath = 'pi/class.tx_ttnews.php'; // Path to this script relative to the extension dir. var $extKey = 'tt_news'; // The extension key.

var $hObj; // class with helper functions var $tt_news_uid = 0; // the uid of the current news record in SINGLE view var $pid_list = 0; var $config = array(); // the processed TypoScript configuration array var $confArr; // extension config from extmanager var $genericMarkerConf; var $sViewSplitLConf = array(); var $langArr = array(); // the languages found in the tt_news sysfolder var $sys_language_mode = ''; var $alternatingLayouts = 0; var $allowCaching = 1; var $catExclusive = ''; var $actuallySelectedCategories = ''; var $arcExclusive = 0; var $fieldNames = array(); var $searchFieldList = 'short,bodytext,author,keywords,links,imagecaption,title'; var $theCode = ''; // the current code var $codes; // list of all codes var $rdfToc = ''; var $templateCode = '';

var $versioningEnabled = false; // is the extension 'version' loaded var $vPrev = false; // do we display a versioning preview var $categories = array(); var $pageArray = array(); // internal cache with an array of the pages in the pid-list var $pointerName = 'pointer'; var $SIM_ACCESS_TIME = 0; // var $renderFields = array(); var $errors = array();

var $enableFields = ''; var $enableCatFields = ''; var $SPaddWhere = ''; var $catlistWhere = '';

var $token = '';

var $debugTimes = FALSE; // debug parsetimes var $useDevlog = TRUE; // write parsetimes to devlog instead printing debug messages

Page 46: TYPO3 Extension development using new Extbase framework

var $parsetimeThreshold = 0.1; // log only functions which need more than x.xx seconds var $writeCachingInfoToDevlog = FALSE; // 1 = write only cache misses to devlog, 2 = write cache hit info, too

var $start_time = NULL; var $global_start_time = 0; var $start_code_line = 0;

var $cache; var $cache_amenuPeriods = FALSE; var $cache_categoryCount = FALSE; var $cache_categories = FALSE;

/** * Main news function: calls the init_news() function and decides by the given CODEs which of the * functions to display news should by called. * * @param string $content : function output is added to this * @param array $conf : configuration array * @returnstring $content: complete content generated by the tt_news plugin */ function main_news($content, $conf) { $this->confArr = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['tt_news']); if ($this->confArr['writeParseTimesToDevlog']) { $this->debugTimes = TRUE; } if ($this->confArr['parsetimeThreshold']) { $this->parsetimeThreshold = floatval(trim($this->confArr['parsetimeThreshold'])); }

$this->hObj = new tx_ttnews_helpers($this);

if ($this->debugTimes) { $this->hObj->getParsetime(__METHOD__); }

$this->conf = $conf; //store configuration

// leave early if USER_INT $this->convertToUserIntObject = $this->conf['convertToUserIntObject'] ? 1 : 0; if (t3lib_div::int_from_ver(TYPO3_version) >= 4003000 && $this->convertToUserIntObject && $this->cObj->getUserObjectType() == tslib_cObj::OBJECTTYPE_USER) { $this->cObj->convertToUserIntObject(); return; }

$this->preInit();

if ($this->conf['enableConfigValidation']) {

Page 47: TYPO3 Extension development using new Extbase framework

if (count($this->errors)) { return $this->hObj->displayErrors(); } }

$this->init(); foreach ($this->codes as $theCode) {

$theCode = (string) strtoupper(trim($theCode)); $this->theCode = $theCode; // initialize category vars $this->initCategoryVars(); $this->initGenericMarkers();

switch ($theCode) { case 'SINGLE' : case 'SINGLE2' : $content .= $this->displaySingle(); break; case 'VERSION_PREVIEW' : $content .= $this->displayVersionPreview(); break; case 'LATEST' : case 'LIST' : case 'LIST2' : case 'LIST3' : case 'HEADER_LIST' : case 'SEARCH' : case 'XML' : $content .= $this->displayList(); break; case 'AMENU' : $content .= $this->displayArchiveMenu(); break; case 'CATMENU' : $content .= $this->displayCatMenu(); break; default :

if ($this->debugTimes) { $this->hObj->getParsetime(__METHOD__ . ' extraCodesHook start'); }

// hook for processing of extra codes if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['tt_news']['extraCodesHook'])) { foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['tt_news']['extraCodesHook'] as $_classRef) { $_procObj = & t3lib_div::getUserObj($_classRef); $content .= $_procObj->extraCodesProcessor($this); } } else { // code not known and no hook found to handle it -> displayerror $this->errors[] = 'CODE "' . $theCode . '" not known'; }

Page 48: TYPO3 Extension development using new Extbase framework

if ($this->debugTimes) { $this->hObj->getParsetime(__METHOD__ . ' extraCodesHook end'); }

break; } }

// check errors array again if ($this->conf['enableConfigValidation']) { if (count($this->errors)) { return $this->hObj->displayErrors(); } }

if ($this->debugTimes) { $this->hObj->getParsetime(__FUNCTION__, true); }

return $this->cObj->stdWrap($content, $this->conf['stdWrap.']); }

/** * [Describe function...] * * @return[type] ... */ function preInit() {

$this->pi_initPIflexForm(); // Init FlexForm configuration for plugin

$flexformTyposcript = $this->pi_getFFvalue($this->cObj->data['pi_flexform'], 'myTS','s_misc'); if ($flexformTyposcript) { require_once(PATH_t3lib.'class.t3lib_tsparser.php'); $tsparser = t3lib_div::makeInstance('t3lib_tsparser'); // Copy conf into existing setup $tsparser->setup = $this->conf; // Parse the new Typoscript $tsparser->parse($flexformTyposcript); // Copy the resulting setup back into conf $this->conf = $tsparser->setup; }

// "CODE" decides what is rendered: codes can be set by TS or FF with priority on FF $code = $this->pi_getFFvalue($this->cObj->data['pi_flexform'], 'what_to_display', 'sDEF'); $this->config['code'] = ($code ? $code : $this->cObj->stdWrap($this->conf['code'], $this->conf['code.']));

if ($this->conf['displayCurrentRecord']) { $this->config['code'] = $this->conf['defaultCode'] ? trim($this->conf['defaultCode']) : 'SINGLE'; $this->tt_news_uid = $this->cObj->data['uid'];

Page 49: TYPO3 Extension development using new Extbase framework

}

// get codes and decide which function is used to process the content $codes = t3lib_div::trimExplode(',', $this->config['code'] ? $this->config['code'] : $this->conf['defaultCode'], 1); if (! count($codes)) { // no code at all $codes = array(); $this->errors[] = 'No code given'; }

$this->codes = $codes; }

/** * Init Function: here all the needed configuration values are stored in class variables.. * * @param array $conf : configuration array from TS * @returnvoid */ function init() {

$this->pi_loadLL(); // Loading language-labels $this->pi_setPiVarDefaults(); // Set default piVars from TS

$this->SIM_ACCESS_TIME = $GLOBALS['SIM_ACCESS_TIME']; // fallback for TYPO3 < 4.2 if (! $this->SIM_ACCESS_TIME) { $simTime = $GLOBALS['SIM_EXEC_TIME']; $this->SIM_ACCESS_TIME = $simTime - ($simTime % 60); }

$this->initCaching();

$this->local_cObj = t3lib_div::makeInstance('tslib_cObj'); // Local cObj. $this->enableFields = $this->getEnableFields('tt_news');

if ($this->tt_news_uid === 0) { // no tt_news_uid set by displayCurrentRecord $this->tt_news_uid = intval($this->piVars['tt_news']); // Get the submitted uid of a news (if any) }

if (! isset($this->conf['compatVersion']) || ! preg_match('/^\d+\.\d+\.\d+$/', $this->conf['compatVersion'])) { $this->conf['compatVersion'] = $this->hObj->getCurrentVersion(); } $this->token = md5(microtime());

if (t3lib_extMgm::isLoaded('version')) { $this->versioningEnabled = true; } // load available syslanguages $this->initLanguages(); // sys_language_mode defines what to do if the requested translation is not found

Page 50: TYPO3 Extension development using new Extbase framework

$this->sys_language_mode = ($this->conf['sys_language_mode'] ? $this->conf['sys_language_mode'] : $GLOBALS['TSFE']->sys_language_mode);

if ($this->conf['searchFieldList']) { // get fieldnames from the tt_news db-table $this->fieldNames = array_keys($GLOBALS['TYPO3_DB']->admin_get_fields('tt_news')); $searchFieldList = $this->hObj->validateFields($this->conf['searchFieldList'], $this->fieldNames); if ($searchFieldList) { $this->searchFieldList = $searchFieldList; } } // Archive: $archiveMode = trim($this->conf['archiveMode']); // month, quarter or year listing in AMENU $this->config['archiveMode'] = $archiveMode ? $archiveMode : 'month';

// arcExclusive : -1=only non-archived; 0=don't care; 1=only archived $arcExclusive = $this->pi_getFFvalue($this->cObj->data['pi_flexform'], 'archive', 'sDEF'); $this->arcExclusive = $arcExclusive ? $arcExclusive : intval($this->conf['archive']);

$this->config['datetimeDaysToArchive'] = intval($this->conf['datetimeDaysToArchive']); $this->config['datetimeHoursToArchive'] = intval($this->conf['datetimeHoursToArchive']); $this->config['datetimeMinutesToArchive'] = intval($this->conf['datetimeMinutesToArchive']);

if ($this->conf['useHRDates']) { $this->hObj->convertDates(); }

// list of pages where news records will be taken from if (! $this->conf['dontUsePidList']) { $this->initPidList(); }

// itemLinkTarget is only used for categoryLinkMode 3 (catselector) in framesets $this->conf['itemLinkTarget'] = trim($this->conf['itemLinkTarget']); // id of the page where the search results should be displayed $this->config['searchPid'] = intval($this->conf['searchPid']);

// pages in Single view will be divided by this token $this->config['pageBreakToken'] = trim($this->conf['pageBreakToken']) ? trim($this->conf['pageBreakToken']) : '<---newpage--->';

$this->config['singleViewPointerName'] = trim($this->conf['singleViewPointerName']) ? trim($this->conf['singleViewPointerName']) : 'sViewPointer';

$maxWordsInSingleView = intval($this->pi_getFFvalue($this->cObj->data['pi_flexform'], 'maxWordsInSingleView', 's_misc')); $maxWordsInSingleView = $maxWordsInSingleView ? $maxWordsInSingleView : intval($this->conf['maxWordsInSingleView']); $this->config['maxWordsInSingleView'] = $maxWordsInSingleView ? $maxWordsInSingleView : 0; $this->config['useMultiPageSingleView'] = $this->conf['useMultiPageSingleView'];

// pid of the page with the single view. the old var PIDitemDisplay is still processed if no other value is found $singlePid = $this->pi_getFFvalue($this->cObj->data['pi_flexform'], 'PIDitemDisplay', 's_misc');

Page 51: TYPO3 Extension development using new Extbase framework

Does this tell you anything about the application?

Page 52: TYPO3 Extension development using new Extbase framework

Maybe

#wtf!

Page 53: TYPO3 Extension development using new Extbase framework

New = more structureeasy to maintain

!"" Classes#   !"" Controller#   %"" Domain!"" Configuration#   !"" TCA#   %"" TypoScript!"" ext_emconf.php!"" ext_icon.gif!"" ext_localconf.php!"" ext_tables.php!"" ext_tables.sql!"" kickstarter.json%"" Resources !"" Private %"" Public

Page 54: TYPO3 Extension development using new Extbase framework

!"" Classes#   !"" Controller#   %"" Domain!"" Configuration#   !"" TCA#   %"" TypoScript!"" ext_emconf.php!"" ext_icon.gif!"" ext_localconf.php!"" ext_tables.php!"" ext_tables.sql!"" kickstarter.json%"" Resources !"" Private %"" Public

!

Because it forces you to think in components

Page 55: TYPO3 Extension development using new Extbase framework

How do I get started?

Page 56: TYPO3 Extension development using new Extbase framework

cd ./typo3conf/ext/

git clone git://git.typo3.org/TYPO3v4/Extensions/extbase_kickstarter.git

(one line)

Download extbasekickstarter

Page 57: TYPO3 Extension development using new Extbase framework

cd ./typo3conf/ext/

git clone git://git.typo3.org/TYPO3v4/Extensions/extbase_kickstarter.git

(all in one line)

Download extbase kickstarter

Page 58: TYPO3 Extension development using new Extbase framework

Fast, comfortable version control system

Smooth development even with large files

Graphical interfaces availablehttp://git-scm.com/tools

Download ithttp://git-scm.com/download

Why git?

Page 59: TYPO3 Extension development using new Extbase framework

Use extbasekickstarter

Demo

Page 60: TYPO3 Extension development using new Extbase framework
Page 61: TYPO3 Extension development using new Extbase framework
Page 62: TYPO3 Extension development using new Extbase framework
Page 63: TYPO3 Extension development using new Extbase framework
Page 64: TYPO3 Extension development using new Extbase framework
Page 65: TYPO3 Extension development using new Extbase framework
Page 66: TYPO3 Extension development using new Extbase framework
Page 67: TYPO3 Extension development using new Extbase framework

We created !le withour new Domain Class

.!"" Classes#   !"" Controller#   %"" Domain#   %"" Model#      %"" Bar.php!"" Configuration#   !"" TCA#   %"" TypoScript!"" ext_emconf.php!"" ext_icon.gif!"" ext_localconf.php!"" ext_tables.php!"" ext_tables.sql!"" kickstarter.json%"" Resources

Page 68: TYPO3 Extension development using new Extbase framework

+ Backend forms

Page 69: TYPO3 Extension development using new Extbase framework

You can instantly create new content for our website!

Page 70: TYPO3 Extension development using new Extbase framework

"BREAK"

Page 71: TYPO3 Extension development using new Extbase framework

Part 2Example

Page 72: TYPO3 Extension development using new Extbase framework

Goal: Find Sushi Bars

- Create a web applicationto manage sushi bars

- Click on a bar to get details

- Find bar on Google Maps

Page 73: TYPO3 Extension development using new Extbase framework

Our Bar model

Sushi Bar

- Name

- Location

- Business hours

- Images

Page 74: TYPO3 Extension development using new Extbase framework

Add Relations

Sushi Bar Review

Page 75: TYPO3 Extension development using new Extbase framework

Review model

- Allow Feedback

- Rating

- Statement

Page 76: TYPO3 Extension development using new Extbase framework

Improve view

- Link Website

- Add QRCode to open a map

- Easy to !nd

Page 77: TYPO3 Extension development using new Extbase framework

Final result

Page 78: TYPO3 Extension development using new Extbase framework

Many happy customers!

Page 79: TYPO3 Extension development using new Extbase framework

Further reading

http://forge.typo3.org/projects/typo3v4-mvc

http://wiki.typo3.org/Category:Topic/extbase

Page 80: TYPO3 Extension development using new Extbase framework

どうも有難うThank you!

Page 81: TYPO3 Extension development using new Extbase framework

Have fun with TYPO3!