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 ''; } }

TYPO3: Finding unused files in fileadmin

Do you want to delete unused or orphaned files in fileadmin or another storage location? Unfortunately, there's no direct core functionality for this. But a small command in your site package can...

Go to news

TYPO3: Editors with individual user_upload folders

Perhaps you're familiar with this client requirement? Editors should be able to add videos using the "Add media by URL" button. But the files shouldn't be located in fileadmin/user_upload/, but rather...

Go to news

TYPO3: Finding pages in mixed mode

In TYPO3, Mixed Mode refers to translated pages that contain content only partially related to the corresponding content in the main language. This is indicated in the backend by an error message. But...

Go to news

Extbase Extensions: Think extensibility with data, site and language

Today, I have a small request for the TYPO3 extension authors out there: Make sure your extensions are extensible. This will also promote the distribution of the corresponding plugins.

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