Attention
TYPO3 v6 has reached its end-of-life April 18th, 2017 and is not maintained by the community anymore. Looking for a stable version? Use the version switch on the top left.
There is no further ELTS support. It is strongly recommended updating your project.
Functions typically used and nice to know¶
These functions are generally just nice to know. They provide functionality which you will often need in TYPO3 applications and therefore they will save you time and make your applications easier for others to understand as well since you use commonly known functions.
Please take time to learn these functions!
\TYPO3\CMS\Core\Utility\GeneralUtility¶
Function |
Comments |
---|---|
|
Check if an item exists in a comma-separated list of items. if (\TYPO3\CMS\Core\Utility\GeneralUtility::inList('gif,jpg,png', $ext)) {//...}
|
|
Returns true if the first part of input string matches the second argument. \TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($path, PATH_site);
|
|
Formats a number of bytes as Kb/Mb/Gb for visual output. $size = ' (' . \TYPO3\CMS\Core\Utility\GeneralUtility::formatSize(filesize($v)) . 'bytes)';
|
|
Evaluates a string as an email address. if ($email && \TYPO3\CMS\Core\Utility\GeneralUtility::validEmail($email)) {
|
|
Various flavors of exploding a string by a token.
array_unique(\TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $rawExtList, 1));
\TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(chr(10), $content);
// Make integer list
implode(\TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $row['subgroup']), ',');
$p = \TYPO3\CMS\Core\Utility\GeneralUtility::revExplode('/', $path, 2);
|
|
Merging arrays with fixes for "PHP-bugs"
|
|
Serialization of PHP variables into XML. These functions are made to serialize and unserialize PHParrays to XML files. They are used for the FlexForms content in TYPO3, Data Structure definitions etc. The XML output is optimized for readability since associative keys are used as tagnames. This also means that only alphanumeric characters are allowed in the tag names andonly keys not starting with numbers (so watch your usage of keys!). However there are options you can set to avoid this problem. Numeric keys are stored with the default tagname "numIndex" but can be overridden to other formats). The function handles input values from the PHP array in a binary-safe way; All characters below 32 (except 9,10,13) will trigger the content to be converted to a base64-string. The PHP variable type of the data is preserved as long as the types are strings, arrays, integers and booleans. Strings are the default type unless the "type" attribute is set.
\TYPO3\CMS\Core\Utility\GeneralUtility::array2xml_cs($this->FORMCFG['c'],'T3FormWizard');
if ($this->xmlStorage) {
$cfgArr = \TYPO3\CMS\Core\Utility\GeneralUtility::xml2array($row[$this->P['field']]);
}
|
|
Reading / Writing files
$templateCode = \TYPO3\CMS\Core\Utility\GeneralUtility::getURL($templateFile);
\TYPO3\CMS\Core\Utility\GeneralUtility::writeFile($extDirPath . $theFile, $fileData['content']);
|
|
Splits a reference to a file in 5 parts. Alternative to "path_info"
and fixes some "PHP-bugs" which makes |
|
Read content of file system directories.
if (@is_dir($path)) {
$directories = \TYPO3\CMS\Core\Utility\GeneralUtility::get_dirs($path);
if (is_array($directories)) {
foreach($directories as $dirName) {
...
}
}
}
$sFiles = \TYPO3\CMS\Core\Utility\GeneralUtility::getFilesInDir(PATH_typo3conf ,'', 1, 1);
$files = \TYPO3\CMS\Core\Utility\GeneralUtility::getFilesInDir($dir, 'png,jpg,gif');
// Get all files with absolute paths prefixed:
$fileList_abs =
\TYPO3\CMS\Core\Utility\GeneralUtility::getAllFilesAndFoldersInPath(array(), $absPath, 'php,inc');
// Traverse files and remove abs path from each (becomes relative)
$fileList_rel =
\TYPO3\CMS\Core\Utility\GeneralUtility::removePrefixPathFromList($fileList_abs, $absPath);
|
|
Implodes a multidimensional array into GET-parameters (e.g.
$pString = \TYPO3\CMS\Core\Utility\GeneralUtility::implodeArrayForUrl('', $params);
|
|
Works on HTML tag attributes
$attribs = \TYPO3\CMS\Core\Utility\GeneralUtility::get_tag_attributes('<' . $subparts[0] . '>');
$tag = '<img ' . \TYPO3\CMS\Core\Utility\GeneralUtility::implodeAttributes($attribs, 1) . ' />';
|
|
Resolves |
|
General purpose functions for calling user functions (creating hooks). See the chapter about Creating hooks in this document for detailed description of these functions.
function procItems($items,$iArray,$config,$table,$row,$field) {
global $TCA;
$params=array();
$params['items'] = &$items;
$params['config'] = $config;
$params['TSconfig'] = $iArray;
$params['table'] = $table;
$params['row'] = $row;
$params['field'] = $field;
\TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction(
$config['itemsProcFunc'],
$params,
$this
);
return $items;
}
$_procObj = &\TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($_classRef);
$_procObj->pObj = &$this;
$value = $_procObj->transform_rte($value,$this);
|
|
Returns the URL to the current script. You can pass an array with associative keys corresponding to the GET-vars you wish to add to the URL. If you set them empty, they will remove existing GET-vars from the current URL. |
\TYPO3\CMS\Core\Utility\MathUtility¶
Function |
Comments |
---|---|
|
Forces the input variable (integer) into the boundaries of $min and $max: \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($row['priority'], 1, 5);
|
|
Tests if the input is an integer. |
\TYPO3\CMS\Backend\Utility\BackendUtility¶
Function |
Comments |
---|---|
|
Functions for selecting records by uid or field value.
// Getting array with title field from a page:
\TYPO3\CMS\Backend\Utility\BackendUtility::getRecord('pages', intval($row['shortcut']), 'title');
// Getting a full record with permission WHERE clause
$pageinfo = \TYPO3\CMS\Backend\Utility\BackendUtility::getRecord(
'pages',
$id,
'*',
($perms_clause ? ' AND ' . $perms_clause : '')
);
// Checking if the id-parameter is an alias.
if (!\TYPO3\CMS\Core\Utility\GeneralUtility::testInt($id)) {
list($idPartR) =
\TYPO3\CMS\Backend\Utility\BackendUtility::getRecordsByField('pages', 'alias', $id);
$id = intval($idPartR['uid']);
}
|
|
Returns the path (visually) of a page $uid, fx. "/First page/Second page/Another subpage" $label = \TYPO3\CMS\Backend\Utility\BackendUtility::getRecordPath(
intval($row['shortcut']),
$perms_clause,
20
);
|
|
Returns a page record (of page with $id) with an extra field
$perms_clause = $GLOBALS['BE_USER']->getPagePermsClause(1);
$pageinfo = \TYPO3\CMS\Backend\Utility\BackendUtility::readPageAccess($id, $perms_clause);
|
|
Date/Time formatting functions using date/time format from
\TYPO3\CMS\Backend\Utility\BackendUtility::datetime($row['crdate'])
\TYPO3\CMS\Backend\Utility\BackendUtility::datetime($row['item_mtime'])
$agePrefixes = ' min| hrs| days| yrs';
\TYPO3\CMS\Backend\Utility\BackendUtility::calcAge(time()-$row['crdate'], $agePrefixes);
|
|
Returns title attribute information for a page-record informing about id, alias, doktype, hidden, starttime, endtime, fe_group etc. $out = \TYPO3\CMS\Backend\Utility\BackendUtility::titleAttribForPages($row, '', 0);
$out = \TYPO3\CMS\Backend\Utility\BackendUtility::titleAttribForPages($row, '1=1 ' . $this->clause, 0);
|
|
Returns image tags for thumbnails
\TYPO3\CMS\Backend\Utility\BackendUtility::getThumbNail(
$this->doc->backPath . 'thumbs.php',
$filepath,
'hspace="5" vspace="5" border="1"'
);
|
|
Get/Set cache values.
Example of how both functions are used together; first // Parsing the user TS (or getting from cache)
$userTS = implode($TSdataArray,chr(10) . '[GLOBAL]' . chr(10));
$hash = md5('pageTS:' . $userTS);
$cachedContent = \TYPO3\CMS\Backend\Utility\BackendUtility::getHash($hash, 0);
$TSconfig = array();
if (isset($cachedContent)) {
$TSconfig = unserialize($cachedContent);
} else {
$parseObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\TypoScript\\Parser\\TypoScriptParser');
$parseObj->parse($userTS);
$TSconfig = $parseObj->setup;
\TYPO3\CMS\Backend\Utility\BackendUtility::storeHash($hash,serialize($TSconfig), 'IDENT');
}
|
|
$line.= \TYPO3\CMS\Backend\Utility\BackendUtility::getRecordTitle('tt_content', $row, 1);
$outputValue = nl2br(
htmlspecialchars(
trim(
\TYPO3\CMS\Core\Utility\GeneralUtility::fixed_lgd_cs(
\TYPO3\CMS\Backend\Utility\BackendUtility::getProcessedValue(
$table,
$fieldName,
$row[$fieldName]
),
250
)
)
)
);
|
|
Returns the Page TSconfig for page with id, $id. This example shows how an object path, $modTSconfig = $GLOBALS['BE_USER']->getTSConfig(
'mod.web_list',
\TYPO3\CMS\Backend\Utility\BackendUtility::getPagesTSconfig($id)
);
|
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility¶
Function |
Comments |
---|---|
|
Adding fields to an existing table definition in $TCA For usage in // tt_address modified
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns(
'tt_address',
array(
'module_sys_dmail_category' => array('config' => array('type' => 'passthrough')),
'module_sys_dmail_html' => array('config' => array('type' => 'passthrough'))
)
);
|
|
Makes fields visible in the TCEforms by adding them to all or selected "types"-configurations For usage in \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
'fe_users',
'tx_myext_newfield;;;;1-1-1, tx_myext_another_field'
);
|
|
Add table name to default list of allowed tables on pages (in $PAGES_TYPES) For usage in \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::allowTableOnStandardPages('tt_board');
|
|
Adds a module (main or sub) to the backend interface. Note Extbase-based modules use a different registration API. For usage in \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addModule(
'user',
'setup',
'after:task',
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($_EXTKEY) . 'mod/'
);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addModule(
'tools',
'txcoreunittestM1',
'',
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($_EXTKEY) . 'mod1/'
);
|
|
Adds a "Function menu module" ("third level module") to an existing function menu for some other backend module For usage in \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::insertModuleFunction(
'web_func',
'tx_cmsplaintextimport_webfunc',
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($_EXTKEY) .
'class.tx_cmsplaintextimport_webfunc.php',
'LLL:EXT:cms_plaintext_import/locallang.php:menu_1'
);
|
|
Adds an entry to the list of plugins in content elements of type "Insert plugin" Note Extbase-based plug-ins use a different registration API. For usage in \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addPlugin(
array(
'LLL:EXT:examples/Resources/Private/Language/locallang_db.xlf:tt_content.list_type_pi1',
$_EXTKEY . '_pi1'
),
'list_type'
);
|
|
Add PlugIn to Static Template #43 When adding a frontend plugin you will have to add both an entry to
the TCA definition of For usage in \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addPItoST43($_EXTKEY);
|