Slug Felder manipulieren

Slug Felder manipulieren

In den Seiteneigenschaften von TYPO3 befindet sich das Slug Feld. Die Anzeige des Feldes ist in der Regel notwendig und sinnvoll. Es kann aber vorkommen, dass nicht jeder Redakteur im System einen beliebigen Slug eingeben können soll. Mit einem Trick könnt ihr die Buttons zum Neu-Erzeugen von Slugs ausblenden. Wir zeigen euch wie.

Button zum Slug Bearbeiten ausblenden

Wir betreuen eine Vielzahl von Universitäten und Hochschulen im TYPO3 Umfeld. Was nahezu bei all diesen Einrichtungen identisch ist, ist eine Vielzahl an Redakteuren und tief verschachtelte Seiten im CMS. Durch das individuelle Anpassen eines Slugs könnte somit jeder eine hochwertige Seite auf Root-Ebene simulieren, in dem man einen Slug "/wichtig" vergibt - auch wenn diese Seite eher unwichtig und in fünfter Ebene ist. Dies gilt es oftmals zu verhindern.

Per TCA ist es in TYPO3 leider nicht möglich für verschiedene Backendbenutzer-Gruppen die Buttons "Slug neu erzeugen" und "Slug bearbeiten" auszublenden. Wir möchten jedoch, dass nur "Chefredakteure" (individuelle) Slugs erzeugen können. Wenn es euch auch reicht, einen oder beide Buttons per CSS auszublenden, haben wir hier die Lösung für euch.

Diese CSS-Datei könnt ihr z.B. in eurem Sitepackage ablegen:

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

Über die ext_tables.php könnt ihr CSS über PHP ladbar machen:

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

Und dann wollen wir die Datei für normale Redakteure einblenden in einer 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 diesem Beispiel wird das CSS nicht eingebunden, wenn die Benutzergruppe 123 einem Benutzer (z.B. Chefredakteure) zugewiesen wurde. Auch wird das CSS nicht eingebunden, wenn man beispielsweise einen News-Datensatz zur Bearbeitung öffnet oder wenn man Administrator ist.

Eine passende BackendUtility könnte übrigens so aussehen:

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

 

Slug Präfix ausblenden

Während der sichtbare Präfix im Slugfeld (z.B. https://domain.org/site1) innerhalb der Seiten wirklich sinnvoll ist, ist der Präfix bei Extensions (z.B. tx_news_domain_model_news.path_segment) eher verwirrend, da der Pfad zur Detailseite nicht mit angezeigt wird. Diesen Präfix können wir für einzelne Extensions einfach ausblenden.

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 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...

Zum Beitrag

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.

Zum Beitrag

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.

Zum Beitrag

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...

Zum Beitrag

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.

Zum Beitrag

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...

Zum Beitrag