sebkln / content_slug

Adds a slug field for human-readable anchors ("domain.com/page/#my-section") to TYPO3 content elements. By default, this anchor is rendered as the header's id attribute.
GNU General Public License v2.0
11 stars 5 forks source link

Workspace version of record does not accept same content slug due to uniqueInPid #7

Open jacobsenj opened 3 years ago

jacobsenj commented 3 years ago

Like you statet in issue #3 about language versions, there's an issue with the content slug and workspaces (versions of records), too.

I changed the tca of the field to use the slug type in TYPO3 core and added a prefix class with a local composer patch:

--- /dev/null
+++ Classes/FormEngine/SlugPrefix.php
@@ -0,0 +1,14 @@
+<?php
+declare(strict_types = 1);
+
+namespace Sebkln\ContentSlug\FormEngine;
+
+use TYPO3\CMS\Backend\Form\FormDataProvider\TcaSlug;
+
+class SlugPrefix
+{
+    public function getPrefix(array $parameters, TcaSlug $reference): string
+    {
+        return '#';
+    }
+}

--- Configuration/TCA/Overrides/tt_content.php
+++ Configuration/TCA/Overrides/tt_content.php
@@ -7,16 +7,22 @@
         'exclude' => true,
         'label' => 'LLL:EXT:content_slug/Resources/Private/Language/locallang_db.xlf:tt_content.tx_content_slug_fragment',
         'config' => [
-            'type' => 'input',
+            'type' => 'slug',
             'size' => 50,
             'max' => 80,
-            'eval' => 'trim,Sebkln\\ContentSlug\\Evaluation\\FragmentEvaluation,uniqueInPid',
-            'default' => '',
-            'fieldControl' => [
-                'importControl' => [
-                    'renderType' => 'generateFragmentFromHeaderControl'
-                ]
-            ]
+            'generatorOptions' => [
+                'fields' => ['header'],
+                'fieldSeparator' => '-',
+                'replacements' => [
+                    '/' => '-'
+                ],
+            ],
+            'appearance' => [
+                'prefix' => \Sebkln\ContentSlug\FormEngine\SlugPrefix::class . '->getPrefix',
+            ],
+            'fallbackCharacter' => '-',
+            'eval' => 'trim,Sebkln\\ContentSlug\\Evaluation\\FragmentEvaluation,uniqueInPid',
+            'default' => ''
         ],
     ],
     'tx_content_slug_link' => [

Now I'm totally allowed to stay on the defined content slug in the workspace version of the record:

image

Maybe this is of interest for you, too.

Cheers Jens

jacobsenj commented 3 years ago

I wasn't sure about Sebkln\\ContentSlug\\Evaluation\\FragmentEvaluation in the eval key. Maybe it could be removed after the change to the slug type, too.

sebkln commented 3 years ago

Thank you for your report. I'll look into Workspaces.

Please note: I did not apply your patch to test the exact behaviour. But you adjusted the TCA close to a former state of this extension. As you can see from the commit message, using the TCA type slug made the fragment field mandatory. This may not be an issue for your project, but I want to keep this field optional.

When I finished the fragment hook for page links, I can revisit the TCA of the fragment field.

jacobsenj commented 3 years ago

As you can see from the commit message, using the TCA type slug made the fragment field mandatory. This may not be an issue for your project, but I want to keep this field optional.

Thanks for pointing this out. Indeed, it would add a default-[a-z0-9]{10} value if the content element header would be empty, so i changed it to allow to stay empty if the header is empty using postModifiers in the tca configuration for the field. In my special use case it will make sense to have a slug for each content element where the header field is filled. By having it this way editors are not forced to set the slug initially after adding a content element.

Updated patch, if of interest ```patch --- /dev/null +++ Classes/FormEngine/Slug.php @@ -0,0 +1,23 @@ + true, 'label' => 'LLL:EXT:content_slug/Resources/Private/Language/locallang_db.xlf:tt_content.tx_content_slug_fragment', 'config' => [ - 'type' => 'input', + 'type' => 'slug', 'size' => 50, 'max' => 80, - 'eval' => 'trim,Sebkln\\ContentSlug\\Evaluation\\FragmentEvaluation,uniqueInPid', - 'default' => '', - 'fieldControl' => [ - 'importControl' => [ - 'renderType' => 'generateFragmentFromHeaderControl' - ] - ] + 'generatorOptions' => [ + 'fields' => ['header'], + 'fieldSeparator' => '-', + 'replacements' => [ + '/' => '-' + ], + 'postModifiers' => [ + \Sebkln\ContentSlug\FormEngine\Slug::class . '->modifySlug', + ], + ], + 'appearance' => [ + 'prefix' => \Sebkln\ContentSlug\FormEngine\Slug::class . '->getPrefix', + ], + 'fallbackCharacter' => '-', + 'eval' => 'trim,Sebkln\\ContentSlug\\Evaluation\\FragmentEvaluation,uniqueInPid', + 'default' => '' ], ], 'tx_content_slug_link' => [ ```