Skip to content

Commit

Permalink
[FEATURE] Allow custom linktype labels in EXT:linkvalidator
Browse files Browse the repository at this point in the history
If a custom linktype is registered for the Linkvalidator, it is now
possible to define the label which will be displayed in the
backend module.

A new interface `LabelledLinktypeInterface` provides a method
`getReadableName()` to return the custom label. It is implemented
by default in the `AbstractLinktype` class, so any custom
linktype extending from this will be able to override the method.

When no abstract class is extended, compositing a class
with the `LabelledLinktypeInterface` implementation is possible.

Resolves: #103090
Releases: main
Change-Id: I8255aca6529d104be19c8af2b7cd505cd8749d7d
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/82876
Tested-by: core-ci <[email protected]>
Reviewed-by: Garvin Hicking <[email protected]>
Tested-by: Stefan Bürk <[email protected]>
Reviewed-by: Stefan Bürk <[email protected]>
Tested-by: Garvin Hicking <[email protected]>
  • Loading branch information
sypets authored and sbuerk committed Sep 13, 2024
1 parent 33ab235 commit e906a4a
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
.. include:: /Includes.rst.txt

.. _feature-103090-1707479280:

===================================================
Feature: #103090 - Make linktype label configurable
===================================================

See :issue:`103090`

Description
===========

It is now possible to provide a translated label for custom linktypes.

For this, a new interface
:php:`\TYPO3\CMS\Linkvalidator\Linktype\LabelledLinktypeInterface` has been
created, which offers the method :php:`getReadableName` for implementation.
That method can return the translated label.

The default abstract implementation
:php:`\TYPO3\CMS\Linkvalidator\Linktype\AbstractLinktype` has been enhanced
to implement that interface. Any custom class extending this abstract is
able to override the method :php:`getReadableName` to provide the
custom translation.

Example extending the abstract:
-------------------------------

.. code-block:: php
:caption: EXT:extension/Classes/Linktype/CustomLinktype.php
use TYPO3\CMS\Linkvalidator\Linktype\AbstractLinktype;
#[Autoconfigure(public: true)]
class CustomLinktype extends AbstractLinktype
{
public function getReadableName(): string
{
$type = $this->getIdentifier();
return $this->getLanguageService()->sL(
'LLL:EXT:linkvalidator_example/Resources/Private/Language/Module/locallang.xlf:linktype_'
. $type
) ?: $type;
}
}
Example implementing the interface:
-----------------------------------

.. code-block:: php
:caption: EXT:extension/Classes/Linktype/CustomLinktype.php
use TYPO3\CMS\Linkvalidator\Linktype\LinktypeInterface;
use TYPO3\CMS\Linkvalidator\Linktype\LabelledLinktypeInterface;
#[Autoconfigure(public: true)]
class CustomLinktype implements LinktypeInterface, LabelledLinktypeInterface
{
// implement all LinktypeInterface methods:
// getIdentifier, checkLink, setAdditionalConfig, ...
// Implement the LabelledLinktypeInterface method getReadableName()
public function getReadableName(): string
{
$type = $this->getIdentifier();
return $this->getLanguageService()->sL(
'LLL:EXT:linkvalidator_example/Resources/Private/Language/Module/locallang.xlf:linktype_'
. $type
) ?: $type;
}
}
Impact
======

Custom linktype classes should now configure a label by implementing the method
:php:`LabelledLinktypeInterface::getReadableName()`.

All existing custom implementations of the `AbstractLinktype` class or the `LinktypeInterface`
will continue to work as before, and will just continue to use the internal name of
the linktype, instead of a translated label.


.. index:: Backend, ext:linkvalidator
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Linkvalidator\LinkAnalyzer;
use TYPO3\CMS\Linkvalidator\Linktype\LabelledLinktypeInterface;
use TYPO3\CMS\Linkvalidator\Linktype\LinktypeRegistry;
use TYPO3\CMS\Linkvalidator\Repository\BrokenLinkRepository;
use TYPO3\CMS\Linkvalidator\Repository\PagesRepository;
Expand Down Expand Up @@ -432,10 +433,14 @@ protected function getCheckOptions(string $prefix): array
continue;
}
$isChecked = !empty($this->checkOpt[$prefix][$type]);
$linkType = $this->linktypeRegistry->getLinktype($type);
$linktypeLabel = ($linkType instanceof LabelledLinktypeInterface)
? ($linkType->getReadableName() ?: $linkType->getIdentifier())
: $type;
$options['optionsByType'][$type] = [
'id' => $prefix . '_SET_' . $type,
'name' => $prefix . '_SET[' . $type . ']',
'label' => $this->getLanguageService()->sL('LLL:EXT:linkvalidator/Resources/Private/Language/Module/locallang.xlf:hooks.' . $type) ?: $type,
'label' => $linktypeLabel,
'checked' => $isChecked,
'count' => (!empty($brokenLinksInformation[$type]) ? $brokenLinksInformation[$type] : '0'),
];
Expand Down
13 changes: 11 additions & 2 deletions typo3/sysext/linkvalidator/Classes/Linktype/AbstractLinktype.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@
/**
* This class provides Check Base plugin implementation
*/
abstract class AbstractLinktype implements LinktypeInterface
abstract class AbstractLinktype implements LinktypeInterface, LabelledLinktypeInterface
{
/**
* Contains parameters needed for the rendering of the error message
*
* @var array
*/
protected array $errorParams = [];

protected string $identifier = '';

public function getIdentifier(): string
Expand Down Expand Up @@ -98,4 +97,14 @@ protected function getLanguageService(): LanguageService
{
return $GLOBALS['LANG'];
}

/**
* Get localized label for this linktype to be displayed in Backend user interface.
* Custom Linktypes should override this and provide language labels for their type.
*/
public function getReadableName(): string
{
$type = $this->getIdentifier();
return $this->getLanguageService()->sL('LLL:EXT:linkvalidator/Resources/Private/Language/Module/locallang.xlf:hooks.' . $type) ?: $type;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

namespace TYPO3\CMS\Linkvalidator\Linktype;

/**
* This class is used for composition in addition to LinktypeInterface,
* and provides the ability to expand implementing classes with the possibility
* to provide a custom Linktype label. It is utilized in the abstract class
* `AbstractLinktype`.
*/
interface LabelledLinktypeInterface
{
/**
* Get localized label for this Linktype to be displayed in Backend user interface.
* Implementing classes should implement this method.
*/
public function getReadableName(): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,10 @@
:param array $errorParams: All parameters needed for the rendering of the error message
:returntype: string
:returns: string Validation error message

.. php:method:: getReadableName()
Get localized label for this linktype to be displayed in Backend user interface.
Custom Linktypes should override this and provide language labels for their type.

:returntype: string
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.. Generated by https://github.com/TYPO3-Documentation/t3docs-codesnippets
.. php:namespace:: TYPO3\CMS\Linkvalidator\Linktype
.. php:interface:: LabelledLinktypeInterface
This class provides interface implementation.
This class is used for composition in addition to LinktypeInterface,
and provides the ability to expand implementing classes with the possibility
to provide a custom Linktype label. It is utilized in the abstract class
`AbstractLinktype`.

.. php:method:: getReadableName()
Get localized label for this linktype to be displayed in Backend user interface.
Custom Linktypes should override this and provide language labels for their type.

:returntype: string
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@
'targetFileName' => 'CodeSnippets/Api/LinktypeInterface.rst.txt',
'withCode' => false,
],
[
'action' => 'createPhpClassDocs',
'class' => \TYPO3\CMS\Linkvalidator\Linktype\LabelledLinktypeInterface::class,
'targetFileName' => 'CodeSnippets/Api/LabelledLinktypeInterface.rst.txt',
'withCode' => false,
],
];
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ of third party extensions. For the complete API have a look into the code.

AbstractLinktype
LinktypeInterface
LabelledLinktypeInterface
BeforeRecordIsAnalyzedEvent <https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/Events/Events/Linkvalidator/BeforeRecordIsAnalyzedEvent.html>
ModifyValidatorTaskEmailEvent <https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/Events/Events/Linkvalidator/ModifyValidatorTaskEmailEvent.html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.. include:: /Includes.rst.txt

.. _linkvalidatorapi-LabelledLinktypeInterface:

=========================
LabelledLinktypeInterface
=========================

.. include:: /CodeSnippets/Api/LabelledLinktypeInterface.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class directly, using the method :php:`getIdentifier()`.
When extending :ref:`\TYPO3\CMS\Linkvalidator\Linktype\AbstractLinktype <linkvalidatorapi-AbstractLinktype>`
it is sufficient to set the :php:`$identifier` class property.

For custom naming of a linktype, the additional interface
:ref:`\TYPO3\CMS\Linkvalidator\Linktype\LabelledLinktypeInterface <linkvalidatorapi-LabelledLinktypeInterface>`.
can be implemented, which is also part of the default `AbstractLinktype` implementation.
The method `getReadableName()` is used to return the custom localized name of
a linktype.

Example
=======

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use PHPUnit\Framework\Attributes\Test;
use TYPO3\CMS\Linkvalidator\LinkAnalyzer;
use TYPO3\CMS\Linkvalidator\Linktype\LabelledLinktypeInterface;
use TYPO3\CMS\Linkvalidator\Linktype\LinktypeInterface;
use TYPO3\CMS\Linkvalidator\Linktype\LinktypeRegistry;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
Expand Down Expand Up @@ -69,7 +70,7 @@ public function registrationThrowsExceptionOnDuplicateIdentifier(): void

protected function getLinkType(string $identifier = ''): LinktypeInterface
{
return new class ($identifier) implements LinktypeInterface {
return new class ($identifier) implements LinktypeInterface, LabelledLinktypeInterface {
private string $identifier;
public function __construct(string $identifier)
{
Expand Down Expand Up @@ -100,6 +101,11 @@ public function getErrorMessage(array $errorParams): string
{
return '';
}

public function getReadableName(): string
{
return '';
}
};
}
}

0 comments on commit e906a4a

Please sign in to comment.