Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Feature: #97187 - PSR-14 Event for modifying link explanation #1794

Merged
merged 2 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
.. include:: /Includes.rst.txt
.. index:: Events; ModifyLinkExplanationEvent
.. _ModifyLinkExplanationEvent:

=============================
ModifyLinkExplanationEvent
=============================

.. versionadded:: 12.0
This event serves as a more powerful and flexible alternative
for the removed :php:`$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['linkHandler']`
hook.

While the removed hook effectively only allowed to modify the link explanation
of TCA `link` fields in case the resolved link type did not already match
one of those, implemented by TYPO3 itself, does the new Event now allow to
always modify the link explanation of any type. Additionally, this also allows
to modify the `additionalAttributes`, displayed below the actual link
explanation field. This is especially useful for extended link handler setups.

To modify the link explanation, the following methods are available:

- :php:`getLinkExplanation()`: Returns the current link explanation data
- :php:`setLinkExplanation()`: Set the link explanation data
- :php:`getLinkExplanationValue()`: Returns a specific link explanation value
- :php:`setLinkExplanationValue()`: Sets a specific link explanation value

The link explanation array usually contains the following values:

- :php:`text` : The text to show in the link explanation field
- :php:`icon`: The markup for the icon, displayed in front of the link explanation field
- :php:`additionalAttributes`: The markup for additional attributes, displayed below the link explanation field

The current context can be evaluated using the following methods:

- :php:`getLinkData()`: Returns the resolved link data, such as the page uid
- :php:`getLinkParts()`: Returns the resolved link parts, such as `url`, `target` and `additionalParams`
- :php:`getElementData()`: Returns the full FormEngine `$data` array for the current element

API
===

.. include:: /CodeSnippets/Events/Backend/ModifyLinkExplanationEvent.rst.txt

Example
=======

Registration of the Event in your extensions' :file:`Services.yaml`:

.. code-block:: yaml
:caption: EXT:my_extension/Configuration/Services.yaml

MyVendor\MyExtension\Backend\ModifyLinkExplanationEventListener:
tags:
- name: event.listener
identifier: 'my-package/backend/modify-link-explanation'

The corresponding event listener class:

.. code-block:: php
:caption: EXT:my_extension/Classes/Backend/ModifyLinkExplanationEventListener.php

use TYPO3\CMS\Backend\Form\Event\ModifyLinkExplanationEvent;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;

final class ModifyLinkExplanationEventListener
{
public function __construct(protected readonly IconFactory $iconFactory)
{
}

public function __invoke(ModifyLinkExplanationEvent $event): void
{
// Use a custom icon for a custom link type
if ($event->getLinkData()['type'] === 'myCustomLinkType') {
$event->setLinkExplanationValue('icon', $this->iconFactory->getIcon('my-custom-link-icon', Icon::SIZE_SMALL)->render());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
.. Generated by https://github.com/linawolf/t3docs_restructured_api_tools
.. php:namespace:: TYPO3\CMS\Backend\Form\Event

.. php:class:: ModifyLinkExplanationEvent

Listeners to this Event will be able to modify the link explanation array, used in FormEngine for link fields


.. php:method:: getLinkData()

:returntype: array

.. php:method:: getLinkParts()

:returntype: array

.. php:method:: getElementData()

:returntype: array

.. php:method:: getLinkExplanation()

:returntype: array

.. php:method:: setLinkExplanation(array linkExplanation)

:param array $linkExplanation: the linkExplanation

.. php:method:: getLinkExplanationValue(string key, mixed default = NULL)

:param string $key: the key
:param mixed $default: the default, default: NULL
:returntype: mixed

.. php:method:: setLinkExplanationValue(string key, mixed value)

:param string $key: the key
:param mixed $value: the value