TYPO3 offers a full API for adding Context Sensitive Help to especially all database tables and fields. This is normally expressed by small comments and an icon linking to a window with a full explanation of a field - or a feature in a module if you choose to use it in that way.
In the example above the context sensitive help appears when "Show field descriptions" is enabled.
These are some basic facts about how CSH works:
Context Sensitive Help (CSH) labels are stored in locallang files inside of extensions, typically in a main file with English and sub-files with the individual languages.
The CSH locallang files are typically named 'locallang_csh_*.php' or "locallang_csh_*.xml"
They are translated as any other locallang file on typo3.org (for "php" versions) or by the backend module in the extension "llxmltranslate" (for the recommended "xml" version)!
CSH labels can override or add themselves to existing values thus allowing for local, customized help. Very flexible.
The global array $TCA_DESCR is reserved to contain CSH labels. CSH labels are loaded as they are needed. Thus the class rendering the form will make an API call to the $LANG object to have the CSH labels loaded - if any - for the table "pages".
In this process the $TCA_DESCR array will look like this before the API call:
Notice that the key ["pages"]["refs"] has a file reference pointing to a locallang file which contains the labels we need. Nothing more. These default values found in $TCA_DESCR is set by API calls in t3lib/stddb/tables.php:
/**
* Setting up TCA_DESCR - Context Sensitive Help
*/
t3lib_extMgm::addLLrefForTCAdescr('pages','EXT:lang/locallang_csh_pages.php');
t3lib_extMgm::addLLrefForTCAdescr('be_users','EXT:lang/locallang_csh_be_users.php');t3lib_extMgm::addLLrefForTCAdescr('be_groups','EXT:lang/locallang_csh_be_groups.php');t3lib_extMgm::addLLrefForTCAdescr('sys_filemounts','EXT:lang/locallang_csh_sysfilem.php');t3lib_extMgm::addLLrefForTCAdescr('_MOD_tools_em','EXT:lang/locallang_csh_em.php');The red line above is the line setting the file for the "pages" table. Notice that other extensions might supply additional files and add additional files to be includes after the defaults ones above! In that case those will override/add to the existing values. The extension "context_help" is doing just that - it includes a whole bunch of locallang files with description of basically the whole "cms" extension.
Well, inside of the class t3lib_TCEforms an API call is made to load the actual labels for the pages table:
if ($this->edit_showFieldHelp || $this->doLoadTableDescr($table)){$GLOBALS['LANG']->loadSingleTableDescription($table);
}
So labels for $table is loaded - and if table is "pages" then this will be the result back in $TCA_DESCR:
As you can see labels are loaded from the file sysext/lang/locallang_csh_pages.php. The content of this file looks like this (partly):
<?php/*** Default TCA_DESCR for "pages"*/$LOCAL_LANG = Array ( 'default' => Array ( 'title.description' => 'Enter the title of the page or folder.', 'title.syntax' => 'You must enter a page title. The field is required.', 'doktype.description' => 'Select the page type. This affects . . . ses.', 'doktype.details' => 'The \'Standard\' type represents a . . . any problems).', 'TSconfig.description' => 'Page TypoScript configuration.', 'TSconfig.details' => 'Basically \'TypoScript\' is a . . . alled).', 'TSconfig.syntax' => 'Basic TypoScr. . . \'Conditions\' and \'Constants\'.', ));?>
Notice how the actual labels in the locallang file contains periods (.) which defines [fieldname].[type-key].[special options]
Fieldname is the field from the table in question
Type-key is one of these values:
description : A short description of the field (as shown in the editing form)
details : A more lengthy description adding some details. Only visible in the external popup window.
syntax : A description of the syntax of the content in the field in question. Use this if the field must have some special code format entered.
image : A reference to an image
image_descr : Description for the image
seeAlso : References to other relevant CSH entries.
alttitle : Alternative title for field/table
special options : Here you can add for example a plus-sign '+'. Means the value of the label is not substituting any existing value but rather adding to it (separated with a single line break). This makes sense only if you are supplying overriding values for existing previously loaded values.
Notice:
A field key can be prefixed with "_" which will prevent it from being shown in the translation tools. This is useful for "seeAlso" and "image" since they should not be translated to other languages!
Currently "description", "details" and "syntax" fields accept limited XHTML content: <strong>, <em>, <b>, <i>. However, don't use important markup for the “description” field since it will stripped when shown in TCEforms.
Example
Looking at the "context_help" extension you will see many "locallang_csh_*.php" files. One is named "locallang_csh_pages.php" and the first lines from that looks like this:
<?php
/**
* Default TCA_DESCR for "pages"
*/
$LOCAL_LANG = Array (
'default' => Array (
'title.description.+' => 'This is normally shown in the website navigation.',
'layout.description' => 'Select a layout for the page. Any effect depends on the website template.',
Notice the red plus-sign in the "title.description" label - this value is added to the existing title.
The other key, "layout.description", is an addition which did not previously exist in the $TCA_DESCR for the pages-table - but that makes sense here since the "context_help" depends on the "cms" extension being loaded which would have added the field "layout" to the database on beforehand! (the "layout" field in the pages-table is not a part of the core as you might have guessed by now...)
The keys in $TCA_DESCR is by default pointing to database tables, for example "pages". However if you wish to use CSH in your modules you can use keys name by this syntax:
_MOD_[module_name] - Placed in the “Backend module” category
xMOD_[extension key with tx_ infront] - Placed in the “Other” category
xEXT_[extension key] - for description of extensions.
xGLOSSARY_[group] - for glossaries, will be marked up in other CSH.
Normally modules will have their name in the $MCONF variable. That would allow you to load the available labels for your module by this API call:
$key = '_MOD_'.$MCONF['name'];
$LANG->loadSingleTableDescription($key);
... and you would now have your labels loaded in $TCA_DESCR[$key]['columns'].
Notice: You will still have to set up the locallang file with the CSH labels by a API call to t3lib_extMgm::addLLrefForTCAdescr(), possibly in a "ext_tables.php" file.
First of all you are strongly encouraged to use the locallang file structure where the default document sets "EXT" as value for the localized labels so that sub-files are included. This will load the system less and make it all easier to manage.
Then there are a few other rules to follow:
Prefix the locallang files "locallang_csh_" so that translators can easily spot these files (which has a secondary priority compared with other locallang files!).
Observe the filename length, which should be maximum 31 chars in total! Since the prefix "locallang_csh_" takes 14 chars, the extension ".php" takes four and any "subfile-suffix" (fx. ".dk") would take three, there is 31-14-4-3 = 10 chars left. So lets say you have 9 characters to name the file to be safe.
Examples where "pages" (5 chars) is the unique name:
locallang_csh_pages.php=>23 chars
locallang_csh_pages.dk.php=>26 chars
Observe the label-key naming by the syntax [fieldname].[type-key].[special option] (see previous section)
Label-key names that are prefixed "_" can safely be used - the prefix is simply removed! This is encouraged for the "seeAlso" and "image" field names since those are in common for all languages and therefore doesn't need translation (the typo3.org translation tool ignores label-keys which are prefixed "_").
When used with database tables: Blank fieldnames are used for information about the database table itself - non-blank fieldnames are expected to point to the actual fieldnames.
For the locallang-XML files which are translated by a backend module you can place images in a subfolder, "cshimages/", to where the locallang-file is located and they will be shown in a selector box inside the translation tool.
type-key | Syntax |
|---|---|
description | Text / XHTML. Mandatory. |
details | Text / XHTML. Optional. |
syntax | Text / XHTML. Optional. |
image_descr | Text. Optional. |
image | Reference to an image (gif,png,jpg) which will be shown below the syntax field (before seeAlso) The reference must be
You can supply a comma list of image references in order to show more than one image. The image_descr value will be splitted per linebreak and shown under each image. |
seeAlso | Internal hyperlink system for related elements. References to other TCA_DESCR elements or URLs. Syntax:
External URLs will open in a blank window. The links will be in italics. Internal references will open in the same window For internal references the permission for table/field read access will be checked and if it fails, the reference will not be shown. Example: pages:starttime , pages:endtime , tt_content:header , Link to TYPO3.org | http://typo3.org/ |
alttitle | Alternative title shown in CSH pop-up window. For database tables and fields the title from TCA is fetched by default, however overridden by this value if it is not blank. For modules (tablename prefixed "_MOD_") you must specify this value, otherwise you will see the bare key outputted. |
In all cases of "Text" above <strong>, <em>, <b>, and <i> is allowed as HTML tags. Make HTML-tag names and attributes in lowercase! Must be XHTML compliant.
Apart from $TCA_DESCR labels (for example "description") shown directly in the context of a form or a module you can always click the little "?" icon:
This window will show you all details including possible links to related descriptions and external URLs.
In this window you can also click a link to see the full description of the whole table/module from which this single field stems:
Implementing CSH for tables and fields of a table is very easy. The display of description, help icon etc. is automatically done by the form rendering class as long as you do the configuration properly. Lets look at two examples:
Say you have created an extension named "myext" and you have extended the pages-table with a new field, "tx_myext_test". What you need to do is to:
Create a file named something like "locallang_csh_pages.php" in your extension directory. Then enter PHP-code along these lines:
<?php/*** Default TCA_DESCR for my new field in the "pages" table*/$LOCAL_LANG = Array ( 'default' => Array ( 'tx_myext_test.description' => 'Enter some test content', 'tx_myext_test.details' => 'You can enter any content you like in this field', ));?>
Then add this line to the ext_tables.php file of your extension:
t3lib_extMgm::addLLrefForTCAdescr('pages','EXT:myext/locallang_csh_pages.php');
That's it.
Implementing CSH in your own modules is a little more difficult. In addition to the two mandatory steps of a) creating a locallang_csh_*.php file in your extension directory and b) calling the t3lib_extMgm::addLLrefForTCAdescr() API function in the ext_tables.php file you might also have to manually load the labels and manually insert the labels where you want them to appear. Whether you need this step or not depends on your method of application:
If you use these functions the descriptions are not loaded automatically for you. You have to do that manually in the initialization of your module:
First, load the description labels for the module. You do that best in the init() function of your script class:
// Descriptions:
$this->descrTable = "_MOD_".$this->MCONF["name"];
if ($BE_USER->uc["edit_showFieldHelp"]){
$LANG->loadSingleTableDescription($this->descrTable);
}
It's assumed that $this->MCONF equals the global $MCONF var that contain module configuration - this delivers the unique module name.
Then secondly - but most important - is that you check for the User Configuration setting "edit_showFieldHelp" (highlighted with red)
Then for each position in your document where you want to have a help icon and possibly help-text (description field) you will have to call an API function; t3lib_BEfunc::helpTextIcon(), t3lib_BEfunc::helpText() or both. These will check if help is available and if so, return a help icon / help text.
This approach is user in the Extension Manager for example. Here it is a good choice because there are descriptions for all settings for extensions and we want to link them to the help icons in a table column
Example 1
The most simple and straight forward way to include the icon/helptext would be something like this:
$HTMLcode.=
t3lib_BEfunc::helpTextIcon($this->descrTable,"quickEdit_selElement",$GLOBALS["BACK_PATH"]).
t3lib_BEfunc::helpText($this->descrTable,"quickEdit_selElement",$GLOBALS["BACK_PATH"]).
"<br/>";
These lines assumes that
$this->descrTable points to the "tablename" (in this case the string "_MOD_".$MCONF["name"])
"quickEdit_selElement" is a "fieldname" defined in the locallang_csh file
$GLOBALS["BACK_PATH"] is correctly pointing back to the TYPO3_mainDir (that is necessary for modules outside of the main directory)
The locallang_csh file for this example would look like this:
<?php/*** Default TCA_DESCR for "_MOD_web_layout"*/$LOCAL_LANG = Array ( 'default' => Array ( 'quickEdit.description' => 'The Quick Editor gives you direct . . . ', 'quickEdit.details' => 'The Quick Editor is designed to cut . . .', 'quickEdit_selElement.description' => 'This is an overview of th. . . ', 'columns.description' => 'By the \"Columns\" view you can control t. . . ', ),);?>
(Location is "sysext/cms/locallang_csh_weblayout.php" and in ext_tables.php for the "cms" extension you will find this line to associate the locallang file with CSH for the Web>Page module: t3lib_extMgm::addLLrefForTCAdescr('_MOD_web_layout','EXT:cms/locallang_csh_weblayout.php'); )
Notice the key " quickEdit_selElement.description" which will provide the description for the help icon in the example.
Example 2
A more advanced - and better - approach is to create a function internally in the script class of the module for handling the help texts. This is an example from the Extension Manager module (mod/tools/em/index.php):
function helpCol($key) {
global $BE_USER;
if ($BE_USER->uc["edit_showFieldHelp"]) {
$hT = trim(t3lib_BEfunc::helpText($this->descrTable,"emconf_".$key,$this->doc->backPath));
return '<td>'.
($hT?$hT:
t3lib_BEfunc::helpTextIcon(
$this->descrTable,
"emconf_".$key,
$this->doc->backPath
)).
'</td>';
}
}
The basic elements are the same, but its more comfortable to work with.
If you want more examples you can make a source code search for the function ->loadSingleTableDescription and $TCA_DESCR and then you should find a number of examples to learn from as well.
This is the quick method. Calling this function instead of t3lib_BEfunc::helpText() will deliver an output which is either a help-icon or a table with both icon and description text depending on the current users configuration. In addition it will automatically load the description files for the $table parameter given.
$HTMLcode.=t3lib_BEfunc::cshItem($tableIdent,'quickEdit',$BACK_PATH);