Custom linktypes

The LinkValidator uses so called "linktypes" to check for different types of links, for example internal or external links.

All "linktypes" have to implement the TYPO3\CMS\Linkvalidator\Linktype\LinktypeInterface.

Classes implementing the LinktypeInterface are automatically registered, if autoconfigure is enabled in Services.yaml.

Alternatively, one can manually tag a custom "linktype" with the linkvalidator.linktype tag:

Vendor\Extension\Linktype\MyCustomLinktype:
  tags:
    - name: linkvalidator.linktype

Due to the autoconfiguration, the identifier has to be provided by the class directly, using the method getIdentifier().

When extending TYPO3\CMS\Linkvalidator\Linktype\AbstractLinktype it is sufficient to set the $identifier class property.

Example

You can find the following example in the extension t3docs/examples.

Extend TYPO3\CMS\Linkvalidator\Linktype\AbstractLinktype to create a custom linktype:

EXT:examples/Classes/LinkValidator/LinkType/ExampleLinkType.php
use TYPO3\CMS\Linkvalidator\Linktype\AbstractLinktype;

class ExampleLinkType extends AbstractLinktype
{
    protected string $identifier = 'example';

    public function checkLink($url, $softRefEntry, $reference)
    {
        $isValidUrl = false;
        // TODO: Implement checkLink() method.
        return $isValidUrl;
    }

    public function getErrorMessage($errorParams)
    {
        $lang = $this->getLanguageService();
        switch ($errorParams['errno'] ?? 0) {
            case 404:
                $message = $lang->sL('LLL:EXT:typo3/sysext/linkvalidator/Resources/Private/Language/Module/locallang.xlf:list.report.pagenotfound404');
                break;
            default:
                // fall back to generic error message
                $message = sprintf($lang->sL('LLL:EXT:typo3/sysext/linkvalidator/Resources/Private/Language/Module/locallang.xlf:list.report.externalerror'), $errorParams['errno']);
        }
        return $message;
    }
}

Activate the new linktype in the page tsconfig:

EXT:examples/Configuration/TsConfig/Page/Extension/Linkvalidator.typoscript
mod.linkvalidator {
   linktypes = db,file,external,example
}

The extension that provides the linktype must have a Configuration/Services.yaml file that contains either:

EXT:examples/Configuration/Services.yaml
services:
   _defaults:
      autoconfigure: true

Or if autoconfiguration is not desired for some reason:

EXT:examples/Configuration/Services.yaml
services:
   T3docs\Examples\LinkValidator\LinkType\ExampleLinkType:
      tags:
         -  name: linkvalidator.linktype

Migration from TYPO3 11 LTS and below

Remove $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['linkvalidator']['checkLinks'] from your ext_localconf.php file.

If autoconfigure is not enabled in your Configuration/Services.(yaml|php), add the tag linkvalidator.linktype manually to your linktype service.

Additionally, make sure to either implement public function getIdentifier(): string or, in case your linktype extends AbstractLinktype, to set the $identifier class property.