TYPO3 Extension Development - Dmitry Dulepov - E-Book

TYPO3 Extension Development E-Book

Dmitry Dulepov

0,0
34,79 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

In Detail



TYPO3 is the enterprise-level content management system for the Web. It is large, feature-rich and very flexible, a lot of this flexibility comes through extensions written by the community. Extensions make it possible to use TYPO3 to drive any type of website, including e-commerce, blogs, social networks, catalogs, and many more. TYPO3 can be completely customized using extensions; however creating an extension can be a challenging task.



This book describes TYPO3 extension creation as it should be done. It starts from an overview of the TYPO3 API and provides recommendations on when and how to use this extensive API. Next it describes the TYPO3 extension generation process. It covers all generation options and emphasizes important decisions that extension developers should be aware of.



Since front-end plugins are the most popular TYPO3 extension type, the book has a chapter dedicated to the process of programming front-end plugins. We then move on to back-end plugin programming. All chapters stress best practices and come with unique tips from the author, who wants his experience to be shared with the TYPO3 community. The book finishes with a chapter about improving code and writing documentation.



This is the first book in English to cover TYPO3 extension development in detail. The author is a member of the TYPO3 core team and developer of many popular TYPO3 extensions. Both novice and experienced TYPO3 programmers can use this book to build the extension they need, following best practices, and saving a lot of time that would otherwise have been spent pouring though the documentation.

Approach



The book is structured so that following the chapters in order builds a TYPO3 extension from the ground up. Experienced developers can use individual chapters independently to get only the information that they need.



Each chapter is divided so that the first part contains a description and discussion of the topic covered followed by a coding example with explanation of how principles and techniques from the first part are followed in the code.



The reader is encouraged not only to read the book but also to look into the discussed classes and actually code the extension while reading the book.

Who this book is for



This book is for PHP developers who want to develop a TYPO3 extension. It assumes the reader has experience with PHP, XML, and HTML. No prior knowledge about TYPO3 extension programming or the TYPO3 API is presumed.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB

Seitenzahl: 267

Veröffentlichungsjahr: 2008

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

TYPO3 Extension Development
Credits
About the Author
About the Reviewer
Preface
What This Book Covers
What You Need for This Book
Who is This Book For
Conventions
Reader Feedback
Customer Support
Downloading the Example Code for the Book
Errata
Piracy
Questions
1. About TYPO3 API
Overview of TYPO3 API
PHP Classes and Files
t3lib_
tslib_
tx_
ux_
user_
How Data Is Stored in TYPO3
Common TYPO3 API
Database API
Extension Management
Helper Functions (t3lib_div)
GET/POST Functions
String Functions
Array Functions
XML Processing
File, Directory, and URL Functions
Debug Functions
System Functions
TYPO3-Specific Functions
TYPO3 File References
Language Support
Reference Index
Hooks
Backend API
TCEforms
TCEmain
Record Manipulation
Clearing Cache
Frontend API
TSFE
Content Objects
Plugin API
Summary
2. Anatomy of TYPO3 Extension
TYPO3 Extension Categories
Category: Frontend
Category: Frontend plugins
Category: Backend
Category: Backend module
Category: Services
Category: Examples
Category: Templates
Category: Documentation
Category: Miscellaneous
Extension Files
Common Files
ext_emconf.php
ext_conf_template.txt
ext_tables.php
ext_tables.sql
ext_tables_static+adt.sql
ext_localconf.php
ext_icon.gif
tca.php
class.ext_update.php
Frontend Plugin Files
pi Files
TypoScript Templates
Backend Module and its Files
Module Function Files
Documentation Files
Summary
3. Planning Extensions
Why is Planning Important?
How to Plan
Gathering Requirements
Implementation Planning
Documentation Planning
TYPO3-Specific Planning
Extension Keys
Database Structure
Field Names
Indexes
Database Relations
Planning Our Extension
Requirements
Functionality
Usability and Expandability
Technical
Extension key
Frontend Plugins
Backend Module
Other Classes
Extension Database Structure
Documentation
Summary
4. Generating Extensions
Why Generation?
Preparing for Generation
Generation Steps
Entering an Extension key
Entering Extension Information
Set Up Languages
Creating New Database Tables
Extending Existing Tables
Creating Frontend Plugins
Creating Backend Modules
Integrating into Existing Modules
Adding Clickmenu Items
Creating Services
Static TypoScript Code
Adding TSConfig
Generating the Extension
Adjusting Extensions
Clean Up
Changing Fields
Hide Tables
Summary
5. Frontend Plugin: An In-Depth Study
Frontend Plugins: The Basics
Concepts
Plugin Configuration
TypoScript Configuration
Flexform Configuration
Templating
Localization
Being Localization-Aware
Localizing Strings
Fetching Localized Records
Character Set Handling
Caching
Caching in TYPO3
Cached and Non-Cached Output
Using cHash
Two Things to Avoid
no_cache=1
set_no_cache()
Advanced: Embedding USER_INT into USER
Summary
6. Programming Frontend Plugins
Review and Update Generated Files
Frontend Plugin Files
class.tx_feuserlist_pi1.php
locallang.xml
Other Related Files
TypoScript Files
ext_localconf.php
ext_tables.php
Non-Reviewed Files
Clean Up Extension
Programming the Plugin
General Workflow
Adding Files
Templates
Flexform Configuration
eID
Defining Functions
Initializing an Extension
Checking the Environment
Loading Configuration
Modifying the Flexform Data Source
Dispatching Calls
Using Templates
Template Basics
Using Templates
Creating a Single view
Creating a Template
Adding stdWrap for Fields
Programming Single View
Creating a List View
Creating a Template
Modifying the TypoScript Template
Programming List View
What is Missing in the List View
Creating Search
Including Styles and Scripts
Adding a Search Box to the Template
Adding a Search Condition
What About Cache?
Creating JavaScript for Autocomplete
Creating PHP Code for Autocomplete
Adding Hooks
What Can Be Optimized?
Summary
7. Programming Backend Modules
Planning a Backend Module
Functionality of a Backend Module
Frontend Classes
Database Structure
Adjusting the Database
ext_tables.sql
ext_tables.php
tca.php
Columns
Types
Palettes
Implementing a Frontend Hook
Backend Modules: The Basics
What Is a Backend Module?
Module Functions
Backend Module Files
Backend API
t3lib_BEfunc
t3lib_TCEmain
t3lib_TCEforms
t3lib_htmlmail
t3lib_refindex
$BE_USER
Implementing a Backend Module
Files and Classes
A Note about Backend HTML
Implementing the Main Class
Implementing the List of Last Logins
Implementing Monthly View
Implementing a List of Active Users
Implementing Page Statistics
Summary
8. Finalizing Extensions
Overview
Updating Code Files
Checking the Code
Using extdeveval to Beautify your Code
Script Documentation
Adding a Function Index
Reformatting the Code
Writing Documentation (Extension Manual)
Documentation Template
Template Structure
Styles in the Template
Images in the Documentation
Writing Documentation
Making Documentation Available
Uploading Extensions to TER
Summary
Index

TYPO3 Extension Development

Dmitry Dulepov

TYPO3 Extension Development

Copyright © 2008 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author(s), Packt Publishing, nor its dealers or distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: September 2008

Production Reference: 1190908

Published by Packt Publishing Ltd.

32 Lincoln Road

Olton

Birmingham, B27 6PA, UK.

ISBN 978-1-847192-12-7

www.packtpub.com

Cover Image by Vinayak Chittar (<[email protected]>)

Credits

Author

Dmitry Dulepov

Reviewer

Ingo Renner

Acquisition Editor

Adil Ahmed

Development Editor

Nikhil Bangera

Technical Editor

Dhiraj Bellani

Copy Editor

Sumathi Sridhar

Editorial Team Leader

Mithil Kulkarni

Project Manager

Abhijeet Deobhakta

Project Coordinator

Rajashree Hamine

Indexers

Rekha Nair

Monica Ajmera

Proofreader

Angie Butcher

Production Coordinators

Aparna Bhagat

Rajni Thorat

Cover Designer

Aparna Bhagat

About the Author

Dmitry Dulepov is a TYPO3 core team member and developer of several popular extensions (such as RealURL, TemplaVoila, comments, ratings, and others). He is known by his active support of the TYPO3 community through TYPO3 mailing lists. In 2008 Dmitry won the contest to appear on the first TYPO3 playing cards. He runs a popular blog where he regularly publishes original tips and articles about various TYPO3 features. In addition to his continuous TYPO3 core and extension development, Dmitry provides support for the TYPO3 translation team on behalf of the core team.

The author would like to thank everyone who helped with the book. This includes but is not limited to:

Packt Publishing, for their very prompt answering of every question and giving helpful comments.Ingo Renner, who reviewed the book and made really good additions, notes, and corrections.My colleagues from Netcreators BV, especially to Ben van 't Ende, who always said that the book is important and Michiel Roos, who gave me some ideas during our work together on a TYPO3 project.My wife and kids, who understood the importance of this work and supported me in this work; my cats, who reminded me to make breaks and stretch by jumping on my knees in the least expected moments.To the TYPO3 community, who helped me to understand typically met issues with TYPO3 extension development.

The author is sure that the book will be useful to the reader and hopes to see more great extensions from the readers of the book soon.

About the Reviewer

Ingo Renner has been active in the TYPO3 project for about five years now. He started contributing to the project with a table-less template for the popular news extension "tt_news". Since then he has been active in many other areas including the content rendering group, digital asset management project, and the core team. Besides that, he's also known as a co-development leader for tt_news, current maintainer of tt_address, and lead developer for TYPO3's blog extension TIMTAB. In 2007, he joined the core team and was suddenly charged with the role of the release manager for TYPO3 4.2 - TYPO3's recent release. In March 2008, he graduated from the University of Applied Sciences, Darmstadt, with a Master of Science in Computer Science.

Ingo is a freelancer specialized in TYPO3 core development and always looking for interesting projects to improve the TYPO3 core.

Preface

This is the first book in English to cover TYPO3 extension development in detail. The author is a member of the TYPO3 core team and developer of many popular TYPO3 extensions. Both novice and experienced TYPO3 programmers can use this book to build the extension they need following best practices and saving a lot of time that would otherwise have been spent pouring though the documentation.

The book is structured so that following the chapters in order builds a TYPO3 extension from the ground up. Experienced developers can use individual chapters independently to get only the information that they need.

Each chapter is divided so that the first part contains a description and discussion of the topic covered followed by a coding example with an explanation of how principles and techniques from the first part are followed in the code.

The reader is encouraged not only to read the book but also to look into the discussed classes and actually code the extension while reading the book.

What This Book Covers

Chapter 1 gives an overview of the TYPO3 API and tells about the most important classes in TYPO3.

Chapter 2 describes files in the TYPO3 extension, what role they play and how to use them.

Chapter 3 focuses on planning. Planning makes extensions better. It makes the project successful.

Chapter 4 walks the reader through the process of extension generation. All options are explained, several issues are pointed out, and useful tips provided.

Chapter 5 focuses on the Frontend plugin theory. It also provides a lot of tips to make extensions effective.

Chapter 6 is dedicated to practical progamming. The reader will see how to make list, search, and single views, use AJAX from the Frontend plugin, and create useful TypoScript for the plugin.

Chapter 7 focuses on the Backend module programming.

Chapter 8 describes how to write documentation for the extension and polish the code before releasing it to TER.

What You Need for This Book

The author assumes that the reader has the following knowledge:

PHP programming The reader is expected to have some experience with PHP and knowledge about PHP classes in general. No prior TYPO3 programming experience is needed.TYPO3 as an administrator Basic knowledge of TYPO3, TypoScript, and TYPO3 extension idea.TYPO3 documentation The reader should know what is TSRef, TSConfig, TYPO3 Core API. The reader should be able to find these documents in the Documentation section of the http://typo3.org/ website.Basic knowledge about phpDoc.

Who is This Book For

This book is for PHP developers who want to develop a TYPO3 extension. It assumes the reader has experience with PHP, XML, and HTML. No prior knowledge about TYPO3 extension programming or the TYPO3 API is presumed.

Reader Feedback

Feedback from our readers is always welcome. Let us know what you think about this book, what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply drop an email to <[email protected]>, making sure to mention the book title in the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or email <[email protected]>.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer Support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the Example Code for the Book

Visit http://www.packtpub.com/files/code/2127_Code.zip to directly download the example code.

Note

The downloadable files contain instructions on how to use them.

Errata

Although we have taken every care to ensure the accuracy of our contents, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in text or code—we would be grateful if you would report this to us. By doing this you can save other readers from frustration, and help to improve subsequent versions of this book. If you find any errata, report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the let us know link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata added to the list of existing errata. The existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide the location address or website name immediately so we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at <[email protected]> if you are having a problem with some aspect of the book, and we will do our best to address it.

Chapter 1. About TYPO3 API

Let's get a picture of TYPO3 API before we start to talk about extension creation. There are a lot of files, functions, and function groups in TYPO3 API. Beginners can easily miss the function they should use in a particular part of the code.

This chapter is going to discuss TYPO3 API. We will not go into the details, but will provide pointers to where developers can look. The best way to read this chapter is to keep a copy of each discussed PHP file and read function description inside that file along with the description mentioned in the book.

After completing this chapter, the reader will have a general picture of TYPO3 API, and will be able to find the necessary functions in the API.

Overview of TYPO3 API

TYPO3 is a large system with lots of PHP classes. Trying to learn each class in order to learn the API is time consuming, and will not provide an understanding of the system as a whole. It is much easier to logically split the system into blocks and look at the API from this perspective.

As seen from the user experience, TYPO3 has two main parts: the Frontend (or FE) and the Backend (or BE). Website visitors see the FE of TYPO3. Website editors create and modify the website content from the BE of TYPO3.

The TYPO3 API can be divided approximately the same way: FE API and BE API. The FE API includes classes to create website output, while the BE API includes classes for content manipulation and other functionality to help editors do their work in an effective way. Extensions can extend existing or add new APIs to the system. One of the best examples is TemplaVoila. It adds point-and-click templates to the TYPO3 BE and flexible content elements to the FE.

However, there is one more part, which is not visible to website visitors or editors but used by both FE and BE API. There is no name for it in TYPO3. In this book, we will call it the Common API. An example of such an API is the database API. It would be wrong to use different database layers for BE and FE (otherwise programmers would have to learn more APIs and would tend to use the one most convenient instead of the one "assigned" to Backend or Frontend). So, TYPO3 has only one layer that works with the database. All system classes and extensions are expected to use this API to access and retrieve data from the database.

While we can logically separate the TYPO3 API into three parts, it should be noted that most TYPO3 classes and functions have a very long history. They are constantly updated, but their age is still visible. Most classes are in the same file system directory (t3lib), and it is hard to tell where the class really belongs just by looking at its name. However, this applies only to the file system. Generally, these classes include functions for a single API group only. So logically, they are well-designed and separated from other groups.

The TYPO3 core team makes every effort to keep the API clear, logical, and effective. Extension developers can learn a lot about TYPO3 by looking into the implementation of the TYPO3 API while programming extensions. This is really a good way to become a TYPO3 professional.

In the following sections, we will look at each API group and certain classes inside them. Due to the large number of API classes, it is not possible to cover them all. It would take the whole book alone to cover them all. So, we are going to cover only those classes that extension developers will most likely meet or use during extension development. We are going to start with the most basic and universal classes and move on to more specialized classes. Note that this chapter will provide only an overview of the API, and not a detailed description of each function. We will look deeper into many of these classes later in the book.

But first, we need to discuss certain basic issues about TYPO3 from a developer's view.

PHP Classes and Files

There are certain conventions concerning file and class naming in TYPO3. They must be used in extensions too and knowing these conventions helps to locate files quickly.

Each class file starts with class. and is followed by a class name in lower case. The file ends with a .php extension. There are certain exceptions to this rule, and they will be described in this book. These exceptions exist due to historical reasons, and no new class may become an exception.

Classes have a certain prefix that declares where they belong in the TYPO3 class hierarchy. The following system prefixes are defined:

t3lib_tslib_tx_ux_user_

Each prefix corresponds to a "library" if it ends with "lib_", and to a "namespace" otherwise. "Library" is just a way to say that a "namespace" belongs to TYPO3. In other words, library classes are TYPO3 classes. Extensions cannot introduce new library classes.

t3lib_

t3lib stands for "TYPO3 library". This name is historical, and everyone just calls it t3lib (tee-three-lib). t3lib is the largest collection of classes in TYPO3. It includes most Common, FE, and BE classes. It is easy to say that a class belongs to t3lib by looking at its name. Here are some examples:

t3lib_DBt3lib_divt3lib_TCEmain

We will look at some t3lib classes later in this chapter.

tslib_

tslib stands for "TypoScript Library", The name has historical reasons as well and everyone calls this library tslib (tee-es-lib). It is located in the typo3/sysext/cms/tslib directory (inside the cms system extension). Most of these classes are already included when code runs in FE. So there is no need to include them explicitly in extensions.

The library classes are responsible for the rendering of the website and most of the FE logic. The base class for FE plugins (modules that extend TYPO3 Frontend functionality) is also located here.

We will discuss tslib classes in the Frontend API section.

Here is a list of some classes in tslib:

tslib_fe

This is the main FE class in TYPO3. It represents a page that a website visitor sees in the browser. There is only one instance of this class in TYPO3 FE, and it is available as and generally referred as "TSFE".

tslib_cobj

This is one of the exclusions to generic naming rule mentioned earlier. This class is located in the file named class.tslib_content.php, but the class name is different. This class implements content objects. Content objects is a TYPO3 way of generating different types of content. There are many content objects, for example, TEXT, IMAGE, or HMENU. They are the same content objects as found in TypoScript. FE plugins from extensions are USER or USER_INT content objects. Instances of this class can either be created directly (see later in this chapter), or by calling .

tslib_fetce

This is an attempt to bring some BE functions (such as clear_cacheCmd) to the FE. While this class exists, it is not really updated and should not be used.

tslib_feuserauth

This is an internal class that authenticates website visitors ("Frontend users"). This class is created and used by TSFE. An instance of this class is always available as .

tslib_pibase

This is a base class for FE plugins. We will cover it in detail in Chapter 5.

tx_

This namespace is reserved for extensions ("tx" stands for "TYPO3 Extensions"). All extension classes must begin with tx_ (with the exception of "ux" classes).

ux_

This namespace is reserved for XCLASSes. XCLASS is a way to subclass a class in TYPO3 and make use of the subclass instead of the parent class in a way that is transparent to all the other classes in the system. Normally, XCLASSes are provided by extensions. XCLASSes take the class and file name of the parent class but prepend it with ux_, as in class.ux_tslib_fe.php or class.ux_t3lib_tcemain.php.

While XCLASSes may seem the easiest way to modify system behavior, they should be avoided and used only if there is absolutely no other way to change system behavior. The fundamental limitations of XCLASSes is that there can be only one XCLASS for a given class in the system. If two extensions try to XCLASS the same class, only the last one will succeed.

user_

This namespace is reserved for PHP functions outside of a class. TYPO3 will refuse to call any function outside of a class that is not prefixed with user_. If an extension key has this prefix, it means that the extension is private. Such extensions cannot be sent to the TYPO3 Extension Repository. Typically, such extensions are created for testing purposes.

How Data Is Stored in TYPO3

TYPO3 uses a database (typically MySQL) and the file system to store data. The file system keeps configuration files, some cache files, images, and uploaded files. The database stores pages, content elements, and lots of system data (such as TypoScript templates, logs, and so on).

All (to be precise most, but for our purpose, all) tables in TYPO3 roughly follow the same structure. They have a set of predefined (reserved) fields. TYPO3 will not work properly if one or more of the required fields is missing. Examples of predefined fields are uid (unique identifier of the record), pid (id of the page where this record is located), crdate (record creation time), tstamp (last update time), cruser_id (uid of the Backend user, who created this record). A table may also contain other reserved fields. If it does, TYPO3 will automatically provide additional functionality for the table. The best examples of such fields are deleted (indicates whether a record is deleted), starttime (indicates when a record becomes visible in the FE), endtime (indicates when a record stops being visible), and hidden (indicates whether a record is hidden). There are other fields, which will be discussed later. All these fields are managed by the system, and extensions usually do not change them.

TYPO3 comes with several default tables. These main tables are:

pages and pages_language_overlay

The pages table stores page data (uid, title, and so on), while the pages_language_overlay table stores translations of the page data.

tt_content

This table stores information about content elements. This is usually one of the largest tables in the system.

be_*

This table stores information related to BE users.

cache_*

This table stores cache data.

fe_*

This table stores information related to FE users.

sys_*

This tables stores various system data.

tx_*

This table stores tables from extensions.

If an extension provides a new table, it must ensure that the table name has a certain format. The table name must start with tx_, followed first by the extension key without underscores and next by an underscore, and the table name. For example, an extension with the extension key my_ext can have the following valid table names:

tx_myext_datatx_myext_elements

The following table names are not valid:

datamyext_datamy_ext_datadata_my_exttx_my_ext_data

We will discuss tables in more detail when we generate extensions later in this book. At the moment, it is important to remember two things:

There are certain naming conventions for tables.Each table must have a certain set of fields.