Thursday, April 2, 2020

How to limit the TYPO3 category tree to a subset of categories for extension records

In many TYPO3 projects I've been working in, the TYPO3 category system is used to structure content by one or multiple categories. A typical category tree I often see is build up as shown in the example below:

Full TYPO3 category tree

This is a very plain way to create a category tree and the structure in the example is limited to 3 independent main categories (Events, Products, Staff). 

Quite often, the shown example category tree is used system wide in TYPO3 and all main categories are shown for all record types. This can be confusing for editors, since when you for example want to assign categories for e.g. event records, why should one see and be able to select the categories "Products" and "Staff" including all subcategories?

Fortunately TYPO3 can be configured to limit the category tree for tables to a given root category. As an example, I limit the shown categories for event records to only "Event" categories. I assume that the category "Events" has the sys_category UID 1.

PageTS example

TCEFORM.tx_sfeventmgt_domain_model_event.category.config.treeConfig.rootUid = 1

In PageTS such configuration options can be set for any record as long as the following configuration path is met: TCEFORM.[tableName].[fieldName].[propertyName]

The fieldName is usually "categories" or "category", but this can also be different depending on how categories are implemented in 3rd party extensions.

The PageTS setting can also be set in TCA as shown below.

TCA example

$GLOBALS['TCA']['tx_sfeventmgt_domain_model_event']['columns']['category']['config']['treeConfig']['rootUid'] = 1;

As a result, the category tree for event records is now limited to the category "Events" and all subcategories.

TYPO3 category tree limited to a subcategories of one main category

I think this is way more clear for an editor than it was before. In general, this can be configured for every table in the TYPO3 TCA (e.g. pages, files, extensions, ...)

The configuration only allows to define one UID as root for the category tree. If more flexibility is needed to limiting the category tree, then TCEFORM.[tableName].[fieldName].[config][foreign_table_where] may be the place to add own custom conditions.

Wednesday, February 19, 2020

Apache rewrite rule to replace %20-%20 with a dash (#) in URLs

Some old(?) versions of Microsoft Excel replace a dash (#) in an URL with "%20-%20". The following example shows, how Excel transforms URLs:

Original URL:
https://www.domain.tld/some/path/#my-anchor

URL when clicked in Excel:
https://www.domain.tld/some/path/%20-%20my-anchor

This may lead to unexpected behavior on webserver application level e.g. when routing can not be resolved successfully and the request will results in an 404 error.

The probably best way would be to fix this behavior "somehow" in Excel, but this does not always seem to be possible as described in this stackoverflow question.

In order work around this problem for a certain application on a webserver, I added a simple redirect which replaces the "%20-%20" with a "#" using the following .htaccess rewrite rule:

RewriteRule ^(.*)\ \-\ (.*)$ /$1#$2 [NE,L,R=301]

This is for sure not a general solution for the problem, put works perfectly when you only have to fix incoming links for a given application.