For coders TYPO3 Tech Corner

Manipulate slug fields

Manipulate slug fields

The slug field is located in the TYPO3 page properties. Displaying the field is usually necessary and useful. However, it can happen that not every editor in the system should be able to enter any slug. You can use a trick to hide the buttons for creating new slugs. We'll show you how.

Hide slug edit button

We support a large number of universities and colleges in the TYPO3 environment. What is almost identical in all of these institutions is a large number of editors and deeply nested pages in the CMS. By customizing a slug, anyone could simulate a high-quality page at the root level by assigning a slug "/important" - even if this page is rather unimportant and in the fifth level. This often needs to be prevented.

Unfortunately, it is not possible to hide the "Create new slug" and "Edit slug" buttons for different backend user groups in TYPO3 via TCA. However, we want only "editors in chief" to be able to create (custom) slugs. If it is enough for you to hide one or both buttons via CSS, we have the solution for you here.

You can store this CSS file in your site package, for example:

// Recreate slug .btn.btn-default.t3js-form-field-slug-recreate { display: none; } // Enable slug field .btn.btn-default.t3js-form-field-slug-toggle { display: none; }

You can make CSS loadable via PHP via ext_tables.php:

$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][\TYPO3\CMS\Backend\Template\ModuleTemplate::class] = ['className' => \In2code\In2template\XClass\Backend\Template\ModuleTemplate::class];

And then we want to show the file for normal editors in a ModuleTemplate.php:

<?php declare(strict_types=1); namespace In2code\In2template\XClass\Backend\Template; use In2code\In2template\Utility\BackendUtility; use TYPO3\CMS\Backend\Template\ModuleTemplate as ModuleTemplateCore; use TYPO3\CMS\Core\Utility\GeneralUtility; /** * Class ModuleTemplate * to add an additional css file in backend context to hide the button (enable slug field) * but only for normal editors (supereditor class can be defined) and only for page edit view (not for news or * event view) */ class ModuleTemplate extends ModuleTemplateCore { protected string $file = '../typo3conf/ext/in2template/Resources/Public/Css/Backend/Editor/noslugenable.css'; /** * Don't add stylesheet if this usergroup is added to user * * @var int */ protected int $usergroup = 123; protected function loadStylesheets() { parent::loadStylesheets(); $this->addEditorStylesheet(); } protected function addEditorStylesheet(): void { if ($this->isDefaultEditor() && $this->isPageEditView()) { $this->pageRenderer->addCssFile($this->file); } } protected function isDefaultEditor(): bool { $currentGroupList = BackendUtility::getBackendUserProperties()['usergroup']; return GeneralUtility::inList($currentGroupList, $this->usergroup) === false && BackendUtility::isAdministrator() === false; } protected function isPageEditView(): bool { $edit = GeneralUtility::_GP('edit'); if ($edit !== null) { if (array_key_exists('pages', $edit)) { return array_values($edit['pages'])[0] === 'edit'; } } return false; } }

In this example, the CSS is not included if the user group 123 has been assigned to a user (e.g. editors-in-chief). Also, the CSS is not included when you open a news record for editing, for example, or when you are an administrator.

By the way, a suitable backend utility could look like this:

<?php declare(strict_types=1); namespace In2code\In2template\Utility; use TYPO3\CMS\Core\Authentication\BackendUserAuthentication; /** * Class BackendUtility */ class BackendUtility { /** * @return array */ public static function getBackendUserProperties(): ?array { $beUserAuthentication = self::getBackendUserAuthentication(); if ($beUserAuthentication !== null) { return $beUserAuthentication->user; } return null; } /** * @return bool */ public static function isAdministrator(): bool { $beUserAuthentication = self::getBackendUserAuthentication(); if ($beUserAuthentication !== null) { return (int)$beUserAuthentication->user['admin'] === 1; } return false; } /** * @return BackendUserAuthentication|null * @SuppressWarnings(PHPMD.Superglobals) */ protected static function getBackendUserAuthentication() { return $GLOBALS['BE_USER']; } }

 

Hide slug prefix

While the visible prefix in the slug field (e.g. https://domain.org/site1) makes sense within the pages, the prefix in extensions (e.g. tx_news_domain_model_news.path_segment) is rather confusing because the path to the detail page is not displayed. We can simply hide this prefix for individual extensions.

EXT:sitepackage/Configuration/TCA/Overrides/tx_news_domain_model_news.php:

/** * Remove slug prefix */ $GLOBALS['TCA']['tx_news_domain_model_news']['columns']['path_segment']['config']['appearance']['prefix'] = \In2code\In2template\Form\SlugPrefix::class . '->getEmptyPrefix';

SlugPrefix.php:

<?php declare(strict_types=1); namespace In2code\In2template\Form; use TYPO3\CMS\Backend\Form\FormDataProvider\TcaSlug; class SlugPrefix { public function getEmptyPrefix(array $parameters, TcaSlug $reference): string { return ''; } }

"Code faster, look at the time" - does this sound familiar to you?

How about time and respect for code quality? Working in a team? Automated tests?

Join us

Lesson cancellation - and now? New portal for the Bavarian school system.

For the Bavarian school system, we developed a new web app based on an existing but very outdated portal. The portal reports lesson cancellations throughout Bavaria and registered users are informed...

Go to news

SQL: Show all tables sorted by size in descending order

Lately I've been using the SQL command more often to find out which tables in the TYPO3 database are the largest. I've published the snippet once.

Go to news

TYPO3 12 with CKEditor 5: Styles in a single selection

If you set a link in the RTE in TYPO3, you may have to choose between different link classes, for example to create buttons in the frontend. What's new in TYPO3 12 is that you can select not just one...

Go to news

Null-Safe Operator in the TYPO3 area

With the introduction of PHP8, problems with undefined arrays or variables in general can arise in many places. Here are a few examples and simple solutions.

Go to news

Delete the first/last lines of a (SQL) file

There isn't much to say about the following commands. Sometimes it can be useful to delete the first (or last) X lines from a file. And if the file is too large to open with a conventional program, a...

Go to news

b13/container: Add and modify child elements in edit view

Unlike gridelements, you cannot manage the child elements in the B13 Container extension when you open the container in the editing view. I would be happy to show you how you can quickly install this...

Go to news