first commit
This commit is contained in:
commit
1218dd1517
12716 files changed
+989233
No files matched your search
@@ -0,0 +1,571 @@
|
||||
<?php
|
||||
declare(strict_types = 1);
|
||||
namespace TYPO3\CMS\RteCKEditor\Controller;
|
||||
|
||||
/*
|
||||
* 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!
|
||||
*/
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use TYPO3\CMS\Core\Compatibility\PublicMethodDeprecationTrait;
|
||||
use TYPO3\CMS\Core\Configuration\Richtext;
|
||||
use TYPO3\CMS\Core\LinkHandling\LinkService;
|
||||
use TYPO3\CMS\Core\Localization\LanguageService;
|
||||
use TYPO3\CMS\Core\Page\PageRenderer;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Recordlist\Controller\AbstractLinkBrowserController;
|
||||
|
||||
/**
|
||||
* Extended controller for link browser
|
||||
* @internal This is a specific Backend Controller implementation and is not considered part of the Public TYPO3 API.
|
||||
*/
|
||||
class BrowseLinksController extends AbstractLinkBrowserController
|
||||
{
|
||||
use PublicMethodDeprecationTrait;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $deprecatedPublicMethods = [
|
||||
'renderLinkAttributeFields' => 'Using BrowseLinksController::renderLinkAttributeFields() is deprecated and will not be possible anymore in TYPO3 v10.0.',
|
||||
'getPageConfigLabel' => 'Using BrowseLinksController::getPageConfigLabel() is deprecated and will not be possible anymore in TYPO3 v10.0.',
|
||||
'getDisplayedLinkHandlerId' => 'Using BrowseLinksController::getDisplayedLinkHandlerId() is deprecated and will not be possible anymore in TYPO3 v10.0.',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $editorId;
|
||||
|
||||
/**
|
||||
* TYPO3 language code of the content language
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $contentsLanguage;
|
||||
|
||||
/**
|
||||
* Language service object for localization to the content language
|
||||
*
|
||||
* @var LanguageService
|
||||
*/
|
||||
protected $contentLanguageService;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $buttonConfig = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $thisConfig = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $classesAnchorDefault = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $classesAnchorDefaultTitle = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $classesAnchorClassTitle = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $classesAnchorDefaultTarget = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $classesAnchorJSOptions = [];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $defaultLinkTarget = '';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $additionalAttributes = [];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $siteUrl = '';
|
||||
|
||||
/**
|
||||
* Initialize controller
|
||||
*/
|
||||
protected function init()
|
||||
{
|
||||
parent::init();
|
||||
|
||||
$this->contentLanguageService = GeneralUtility::makeInstance(LanguageService::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
*/
|
||||
protected function initVariables(ServerRequestInterface $request)
|
||||
{
|
||||
parent::initVariables($request);
|
||||
|
||||
$queryParameters = $request->getQueryParams();
|
||||
|
||||
$this->siteUrl = GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
|
||||
|
||||
$this->currentLinkParts = $queryParameters['P']['curUrl'] ?? [];
|
||||
$this->editorId = $queryParameters['editorId'];
|
||||
$this->contentsLanguage = $queryParameters['contentsLanguage'];
|
||||
$this->RTEtsConfigParams = $queryParameters['RTEtsConfigParams'] ?? null;
|
||||
|
||||
$this->contentLanguageService->init($this->contentsLanguage);
|
||||
|
||||
$tcaFieldConf = ['enableRichtext' => true];
|
||||
if (!empty($queryParameters['P']['richtextConfigurationName'])) {
|
||||
$tcaFieldConf['richtextConfiguration'] = $queryParameters['P']['richtextConfigurationName'];
|
||||
}
|
||||
|
||||
/** @var Richtext $richtextConfigurationProvider */
|
||||
$richtextConfigurationProvider = GeneralUtility::makeInstance(Richtext::class);
|
||||
$this->thisConfig = $richtextConfigurationProvider->getConfiguration(
|
||||
$this->parameters['table'],
|
||||
$this->parameters['fieldName'],
|
||||
(int)$this->parameters['pid'],
|
||||
$this->parameters['recordType'],
|
||||
$tcaFieldConf
|
||||
);
|
||||
$this->buttonConfig = $this->thisConfig['buttons']['link'] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize document template object
|
||||
*/
|
||||
protected function initDocumentTemplate()
|
||||
{
|
||||
parent::initDocumentTemplate();
|
||||
|
||||
$pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
|
||||
$pageRenderer->loadRequireJsModule(
|
||||
'TYPO3/CMS/RteCkeditor/RteLinkBrowser',
|
||||
'function(RteLinkBrowser) {
|
||||
RteLinkBrowser.initialize(' . GeneralUtility::quoteJSvalue($this->editorId) . ');
|
||||
}'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize $this->currentLink and $this->currentLinkHandler
|
||||
*/
|
||||
protected function initCurrentUrl()
|
||||
{
|
||||
if (empty($this->currentLinkParts)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!empty($this->currentLinkParts['url'])) {
|
||||
$linkService = GeneralUtility::makeInstance(LinkService::class);
|
||||
$data = $linkService->resolve($this->currentLinkParts['url']);
|
||||
$this->currentLinkParts['type'] = $data['type'];
|
||||
unset($data['type']);
|
||||
$this->currentLinkParts['url'] = $data;
|
||||
if (!empty($this->currentLinkParts['url']['parameters'])) {
|
||||
$this->currentLinkParts['params'] = '&' . $this->currentLinkParts['url']['parameters'];
|
||||
}
|
||||
}
|
||||
|
||||
parent::initCurrentUrl();
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the link attributes for the selected link handler
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function renderLinkAttributeFields()
|
||||
{
|
||||
// Processing the classes configuration
|
||||
if (!empty($this->buttonConfig['properties']['class']['allowedClasses'])) {
|
||||
$classesAnchorArray = is_array($this->buttonConfig['properties']['class']['allowedClasses']) ? $this->buttonConfig['properties']['class']['allowedClasses'] : GeneralUtility::trimExplode(',', $this->buttonConfig['properties']['class']['allowedClasses'], true);
|
||||
// Collecting allowed classes and configured default values
|
||||
$classesAnchor = [
|
||||
'all' => []
|
||||
];
|
||||
|
||||
if (is_array($this->thisConfig['classesAnchor'])) {
|
||||
$readOnlyTitle = $this->isReadonlyTitle();
|
||||
foreach ($this->thisConfig['classesAnchor'] as $label => $conf) {
|
||||
if (in_array($conf['class'], $classesAnchorArray, true)) {
|
||||
$classesAnchor['all'][] = $conf['class'];
|
||||
if ($conf['type'] === $this->displayedLinkHandlerId) {
|
||||
$classesAnchor[$conf['type']][] = $conf['class'];
|
||||
if ($this->buttonConfig[$conf['type']]['properties']['class']['default'] == $conf['class']) {
|
||||
$this->classesAnchorDefault[$conf['type']] = $conf['class'];
|
||||
if ($conf['titleText']) {
|
||||
$this->classesAnchorDefaultTitle[$conf['type']] = $this->contentLanguageService->sL(trim($conf['titleText']));
|
||||
}
|
||||
if (isset($conf['target'])) {
|
||||
$this->classesAnchorDefaultTarget[$conf['type']] = trim($conf['target']);
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($readOnlyTitle && $conf['titleText']) {
|
||||
$this->classesAnchorClassTitle[$conf['class']] = ($this->classesAnchorDefaultTitle[$conf['type']] = $this->contentLanguageService->sL(trim($conf['titleText'])));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isset($this->linkAttributeValues['class'])
|
||||
&& isset($classesAnchor[$this->displayedLinkHandlerId])
|
||||
&& !in_array($this->linkAttributeValues['class'], $classesAnchor[$this->displayedLinkHandlerId], true)
|
||||
) {
|
||||
unset($this->linkAttributeValues['class']);
|
||||
}
|
||||
// Constructing the class selector options
|
||||
foreach ($classesAnchorArray as $class) {
|
||||
if (!in_array($class, $classesAnchor['all']) || in_array($class, $classesAnchor['all']) && is_array($classesAnchor[$this->displayedLinkHandlerId]) && in_array($class, $classesAnchor[$this->displayedLinkHandlerId])) {
|
||||
$selected = '';
|
||||
if ($this->linkAttributeValues['class'] === $class || !$this->linkAttributeValues['class'] && $this->classesAnchorDefault[$this->displayedLinkHandlerId] == $class) {
|
||||
$selected = 'selected="selected"';
|
||||
}
|
||||
$classLabel = !empty($this->thisConfig['classes'][$class]['name'])
|
||||
? $this->getPageConfigLabel($this->thisConfig['classes'][$class]['name'], 0)
|
||||
: $class;
|
||||
$classStyle = !empty($this->thisConfig['classes'][$class]['value'])
|
||||
? $this->thisConfig['classes'][$class]['value']
|
||||
: '';
|
||||
$title = $this->classesAnchorClassTitle[$class] ?? $this->classesAnchorDefaultTitle[$class] ?? '';
|
||||
$this->classesAnchorJSOptions[$this->displayedLinkHandlerId] .= '<option ' . $selected . ' value="' . htmlspecialchars($class) . '"'
|
||||
. ($classStyle ? ' style="' . htmlspecialchars($classStyle) . '"' : '')
|
||||
. 'data-link-title="' . htmlspecialchars($title) . '"'
|
||||
. '>' . htmlspecialchars($classLabel)
|
||||
. '</option>';
|
||||
}
|
||||
}
|
||||
if ($this->classesAnchorJSOptions[$this->displayedLinkHandlerId] && !($this->buttonConfig['properties']['class']['required'] || $this->buttonConfig[$this->displayedLinkHandlerId]['properties']['class']['required'])) {
|
||||
$selected = '';
|
||||
if (!$this->linkAttributeValues['class'] && !$this->classesAnchorDefault[$this->displayedLinkHandlerId]) {
|
||||
$selected = 'selected="selected"';
|
||||
}
|
||||
$this->classesAnchorJSOptions[$this->displayedLinkHandlerId] = '<option ' . $selected . ' value=""></option>' . $this->classesAnchorJSOptions[$this->displayedLinkHandlerId];
|
||||
}
|
||||
}
|
||||
// Default target
|
||||
$this->defaultLinkTarget = $this->classesAnchorDefault[$this->displayedLinkHandlerId] && $this->classesAnchorDefaultTarget[$this->displayedLinkHandlerId]
|
||||
? $this->classesAnchorDefaultTarget[$this->displayedLinkHandlerId]
|
||||
: ($this->buttonConfig[$this->displayedLinkHandlerId]['properties']['target']['default'] ?? $this->buttonConfig['properties']['target']['default'] ?? '');
|
||||
|
||||
// todo: find new name for this option
|
||||
// Initializing additional attributes
|
||||
if ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['rte_ckeditor']['plugins']['TYPO3Link']['additionalAttributes']) {
|
||||
$addAttributes = GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['rte_ckeditor']['plugins']['TYPO3Link']['additionalAttributes'], true);
|
||||
foreach ($addAttributes as $attribute) {
|
||||
$this->additionalAttributes[$attribute] = $this->linkAttributeValues[$attribute] ?? '';
|
||||
}
|
||||
}
|
||||
return parent::renderLinkAttributeFields();
|
||||
}
|
||||
|
||||
/**
|
||||
* Localize a label obtained from Page TSConfig
|
||||
*
|
||||
* @param string $string The label to be localized
|
||||
* @param bool $JScharCode If needs to be converted to an array of char numbers
|
||||
* @return string Localized string
|
||||
*/
|
||||
protected function getPageConfigLabel($string, $JScharCode = true)
|
||||
{
|
||||
if (strpos($string, 'LLL:') !== 0) {
|
||||
$label = $string;
|
||||
} else {
|
||||
$label = $this->getLanguageService()->sL(trim($string));
|
||||
}
|
||||
$label = str_replace(['\\\'', '"'], ['\'', '\\"'], $label);
|
||||
return $JScharCode ? GeneralUtility::quoteJSvalue($label) : $label;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function renderCurrentUrl()
|
||||
{
|
||||
$removeLink = ' <a href="#" class="t3js-removeCurrentLink">' . htmlspecialchars($this->getLanguageService()->getLL('removeLink')) . '</a>';
|
||||
return '
|
||||
<div class="element-browser-panel element-browser-title">' .
|
||||
htmlspecialchars($this->getLanguageService()->getLL('currentLink')) .
|
||||
': ' .
|
||||
htmlspecialchars($this->currentLinkHandler->formatCurrentUrl()) .
|
||||
'<span class="pull-right">' . $removeLink . '</span>' .
|
||||
'</div>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the allowed items or tabs
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
protected function getAllowedItems()
|
||||
{
|
||||
$allowedItems = parent::getAllowedItems();
|
||||
|
||||
$blindLinkOptions = isset($this->thisConfig['blindLinkOptions'])
|
||||
? GeneralUtility::trimExplode(',', $this->thisConfig['blindLinkOptions'], true)
|
||||
: [];
|
||||
$allowedItems = array_diff($allowedItems, $blindLinkOptions);
|
||||
|
||||
if (is_array($this->buttonConfig['options']) && $this->buttonConfig['options']['removeItems']) {
|
||||
$allowedItems = array_diff($allowedItems, GeneralUtility::trimExplode(',', $this->buttonConfig['options']['removeItems'], true));
|
||||
}
|
||||
|
||||
return $allowedItems;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the allowed link attributes
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
protected function getAllowedLinkAttributes()
|
||||
{
|
||||
$allowedLinkAttributes = parent::getAllowedLinkAttributes();
|
||||
|
||||
$blindLinkFields = isset($this->thisConfig['blindLinkFields'])
|
||||
? GeneralUtility::trimExplode(',', $this->thisConfig['blindLinkFields'], true)
|
||||
: [];
|
||||
$allowedLinkAttributes = array_diff($allowedLinkAttributes, $blindLinkFields);
|
||||
|
||||
return $allowedLinkAttributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an array of link attribute field rendering definitions
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
protected function getLinkAttributeFieldDefinitions()
|
||||
{
|
||||
$fieldRenderingDefinitions = parent::getLinkAttributeFieldDefinitions();
|
||||
$fieldRenderingDefinitions['title'] = $this->getTitleField();
|
||||
$fieldRenderingDefinitions['class'] = $this->getClassField();
|
||||
$fieldRenderingDefinitions['target'] = $this->getTargetField();
|
||||
$fieldRenderingDefinitions['rel'] = $this->getRelField();
|
||||
if (empty($this->buttonConfig['queryParametersSelector']['enabled'])) {
|
||||
unset($fieldRenderingDefinitions['params']);
|
||||
}
|
||||
return $fieldRenderingDefinitions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add rel field
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getRelField()
|
||||
{
|
||||
if (empty($this->buttonConfig['relAttribute']['enabled'])) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$currentRel = '';
|
||||
if ($this->displayedLinkHandler === $this->currentLinkHandler
|
||||
&& !empty($this->currentLinkParts)
|
||||
&& isset($this->linkAttributeValues['rel'])
|
||||
&& is_string($this->linkAttributeValues['rel'])
|
||||
) {
|
||||
$currentRel = $this->linkAttributeValues['rel'];
|
||||
}
|
||||
|
||||
return '
|
||||
<form action="" name="lrelform" id="lrelform" class="t3js-dummyform form-horizontal">
|
||||
<div class="form-group form-group-sm">
|
||||
<label class="col-xs-4 control-label">' .
|
||||
htmlspecialchars($this->getLanguageService()->getLL('linkRelationship')) .
|
||||
'</label>
|
||||
<div class="col-xs-8">
|
||||
<input type="text" name="lrel" class="form-control" value="' . htmlspecialchars($currentRel) . '" />
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
';
|
||||
}
|
||||
|
||||
/**
|
||||
* Add target selector
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getTargetField()
|
||||
{
|
||||
$targetSelectorConfig = [];
|
||||
if (is_array($this->buttonConfig['targetSelector'])) {
|
||||
$targetSelectorConfig = $this->buttonConfig['targetSelector'];
|
||||
}
|
||||
$target = $this->linkAttributeValues['target'] ?: $this->defaultLinkTarget;
|
||||
$lang = $this->getLanguageService();
|
||||
$targetSelector = '';
|
||||
|
||||
if (!$targetSelectorConfig['disabled']) {
|
||||
$targetSelector = '
|
||||
<select name="ltarget_type" class="t3js-targetPreselect form-control">
|
||||
<option value=""></option>
|
||||
<option value="_top">' . htmlspecialchars($lang->getLL('top')) . '</option>
|
||||
<option value="_blank">' . htmlspecialchars($lang->getLL('newWindow')) . '</option>
|
||||
</select>
|
||||
';
|
||||
}
|
||||
|
||||
return '
|
||||
<form action="" name="ltargetform" id="ltargetform" class="t3js-dummyform form-horizontal">
|
||||
<div class="form-group form-group-sm" ' . ($targetSelectorConfig['disabled'] ? ' style="display: none;"' : '') . '>
|
||||
<label class="col-xs-4 control-label">' . htmlspecialchars($lang->getLL('target')) . '</label>
|
||||
<div class="col-xs-4">
|
||||
<input type="text" name="ltarget" class="t3js-linkTarget form-control"
|
||||
value="' . htmlspecialchars($target) . '" />
|
||||
</div>
|
||||
<div class="col-xs-4">
|
||||
' . $targetSelector . '
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
';
|
||||
}
|
||||
|
||||
/**
|
||||
* Add title selector
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getTitleField()
|
||||
{
|
||||
if ($this->linkAttributeValues['title']) {
|
||||
$title = $this->linkAttributeValues['title'];
|
||||
} else {
|
||||
$title = $this->classesAnchorDefaultTitle[$this->displayedLinkHandlerId] ?: '';
|
||||
}
|
||||
$readOnlyTitle = $this->isReadonlyTitle();
|
||||
|
||||
if ($readOnlyTitle) {
|
||||
$currentClass = $this->linkAttributeFields['class'];
|
||||
if (!$currentClass) {
|
||||
$currentClass = empty($this->classesAnchorDefault[$this->displayedLinkHandlerId]) ? '' : $this->classesAnchorDefault[$this->displayedLinkHandlerId];
|
||||
}
|
||||
$title = $this->classesAnchorClassTitle[$currentClass] ?? $this->classesAnchorDefaultTitle[$this->displayedLinkHandlerId] ?? '';
|
||||
}
|
||||
return '
|
||||
<form action="" name="ltitleform" id="ltitleform" class="t3js-dummyform form-horizontal">
|
||||
<div class="form-group form-group-sm">
|
||||
<label class="col-xs-4 control-label">
|
||||
' . htmlspecialchars($this->getLanguageService()->getLL('title')) . '
|
||||
</label>
|
||||
<div class="col-xs-8">
|
||||
<input ' . ($readOnlyTitle ? 'disabled' : '') . ' type="text" name="ltitle" class="form-control t3js-linkTitle"
|
||||
value="' . htmlspecialchars($title) . '" />
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
';
|
||||
}
|
||||
|
||||
/**
|
||||
* Return html code for the class selector
|
||||
*
|
||||
* @return string the html code to be added to the form
|
||||
*/
|
||||
protected function getClassField()
|
||||
{
|
||||
$selectClass = '';
|
||||
if ($this->classesAnchorJSOptions[$this->displayedLinkHandlerId]) {
|
||||
$selectClass = '
|
||||
<form action="" name="lclassform" id="lclassform" class="t3js-dummyform form-horizontal">
|
||||
<div class="form-group form-group-sm">
|
||||
<label class="col-xs-4 control-label">
|
||||
' . htmlspecialchars($this->getLanguageService()->getLL('class')) . '
|
||||
</label>
|
||||
<div class="col-xs-8">
|
||||
<select name="lclass" class="t3js-class-selector form-control">
|
||||
' . $this->classesAnchorJSOptions[$this->displayedLinkHandlerId] . '
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
';
|
||||
}
|
||||
return $selectClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the ID of current page
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function getCurrentPageId()
|
||||
{
|
||||
return (int)$this->parameters['pid'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the configuration
|
||||
*
|
||||
* This is only used by RTE currently.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getConfiguration()
|
||||
{
|
||||
return $this->buttonConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get attributes for the body tag
|
||||
*
|
||||
* @return string[] Array of body-tag attributes
|
||||
*/
|
||||
protected function getBodyTagAttributes()
|
||||
{
|
||||
$parameters = parent::getBodyTagAttributes();
|
||||
$parameters['data-site-url'] = $this->siteUrl;
|
||||
$parameters['data-default-link-target'] = $this->defaultLinkTarget;
|
||||
return $parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $overrides
|
||||
*
|
||||
* @return array Array of parameters which have to be added to URLs
|
||||
*/
|
||||
public function getUrlParameters(array $overrides = null)
|
||||
{
|
||||
return [
|
||||
'act' => $overrides['act'] ?? $this->displayedLinkHandlerId,
|
||||
'P' => $overrides['P'] ?? $this->parameters,
|
||||
'editorId' => $this->editorId,
|
||||
'contentsLanguage' => $this->contentsLanguage
|
||||
];
|
||||
}
|
||||
|
||||
protected function isReadonlyTitle(): bool
|
||||
{
|
||||
if (isset($this->buttonConfig[$this->displayedLinkHandlerId]['properties']['title']['readOnly'])) {
|
||||
return (bool)$this->buttonConfig[$this->displayedLinkHandlerId]['properties']['title']['readOnly'];
|
||||
}
|
||||
|
||||
return (bool)($this->buttonConfig['properties']['title']['readOnly'] ?? false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,396 @@
|
||||
<?php
|
||||
declare(strict_types = 1);
|
||||
namespace TYPO3\CMS\RteCKEditor\Form\Element;
|
||||
|
||||
/*
|
||||
* 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!
|
||||
*/
|
||||
|
||||
use TYPO3\CMS\Backend\Form\Element\AbstractFormElement;
|
||||
use TYPO3\CMS\Backend\Routing\UriBuilder;
|
||||
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
|
||||
use TYPO3\CMS\Core\Localization\Locales;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Core\Utility\PathUtility;
|
||||
|
||||
/**
|
||||
* Render rich text editor in FormEngine
|
||||
* @internal This is a specific Backend FormEngine implementation and is not considered part of the Public TYPO3 API.
|
||||
*/
|
||||
class RichTextElement extends AbstractFormElement
|
||||
{
|
||||
/**
|
||||
* Default field information enabled for this element.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $defaultFieldInformation = [
|
||||
'tcaDescription' => [
|
||||
'renderType' => 'tcaDescription',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* Default field wizards enabled for this element.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $defaultFieldWizard = [
|
||||
'localizationStateSelector' => [
|
||||
'renderType' => 'localizationStateSelector',
|
||||
],
|
||||
'otherLanguageContent' => [
|
||||
'renderType' => 'otherLanguageContent',
|
||||
'after' => [
|
||||
'localizationStateSelector'
|
||||
],
|
||||
],
|
||||
'defaultLanguageDifferences' => [
|
||||
'renderType' => 'defaultLanguageDifferences',
|
||||
'after' => [
|
||||
'otherLanguageContent',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* This property contains configuration related to the RTE
|
||||
* But only the .editor configuration part
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $rteConfiguration = [];
|
||||
|
||||
/**
|
||||
* Renders the ckeditor element
|
||||
*
|
||||
* @return array
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function render(): array
|
||||
{
|
||||
$resultArray = $this->initializeResultArray();
|
||||
$parameterArray = $this->data['parameterArray'];
|
||||
$config = $parameterArray['fieldConf']['config'];
|
||||
|
||||
$fieldId = $this->sanitizeFieldId($parameterArray['itemFormElName']);
|
||||
$itemFormElementName = $this->data['parameterArray']['itemFormElName'];
|
||||
|
||||
$value = $this->data['parameterArray']['itemFormElValue'] ?? '';
|
||||
|
||||
$fieldInformationResult = $this->renderFieldInformation();
|
||||
$fieldInformationHtml = $fieldInformationResult['html'];
|
||||
$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
|
||||
|
||||
$fieldControlResult = $this->renderFieldControl();
|
||||
$fieldControlHtml = $fieldControlResult['html'];
|
||||
$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
|
||||
|
||||
$fieldWizardResult = $this->renderFieldWizard();
|
||||
$fieldWizardHtml = $fieldWizardResult['html'];
|
||||
$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
|
||||
|
||||
$attributes = [
|
||||
'style' => 'display:none',
|
||||
'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config),
|
||||
'id' => $fieldId,
|
||||
'name' => htmlspecialchars($itemFormElementName),
|
||||
];
|
||||
|
||||
$html = [];
|
||||
$html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
|
||||
$html[] = $fieldInformationHtml;
|
||||
$html[] = '<div class="form-control-wrap">';
|
||||
$html[] = '<div class="form-wizards-wrap">';
|
||||
$html[] = '<div class="form-wizards-element">';
|
||||
$html[] = '<textarea ' . GeneralUtility::implodeAttributes($attributes, true) . '>';
|
||||
$html[] = htmlspecialchars($value);
|
||||
$html[] = '</textarea>';
|
||||
$html[] = '</div>';
|
||||
if (!empty($fieldControlHtml)) {
|
||||
$html[] = '<div class="form-wizards-items-aside">';
|
||||
$html[] = '<div class="btn-group">';
|
||||
$html[] = $fieldControlHtml;
|
||||
$html[] = '</div>';
|
||||
$html[] = '</div>';
|
||||
}
|
||||
if (!empty($fieldWizardHtml)) {
|
||||
$html[] = '<div class="form-wizards-items-bottom">';
|
||||
$html[] = $fieldWizardHtml;
|
||||
$html[] = '</div>';
|
||||
}
|
||||
$html[] = '</div>';
|
||||
$html[] = '</div>';
|
||||
$html[] = '</div>';
|
||||
|
||||
$resultArray['html'] = implode(LF, $html);
|
||||
|
||||
$this->rteConfiguration = $config['richtextConfiguration']['editor'];
|
||||
$resultArray['requireJsModules'][] = [
|
||||
'ckeditor' => $this->getCkEditorRequireJsModuleCode($fieldId)
|
||||
];
|
||||
|
||||
return $resultArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the contents language iso code
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getLanguageIsoCodeOfContent(): string
|
||||
{
|
||||
$currentLanguageUid = $this->data['databaseRow']['sys_language_uid'];
|
||||
if (is_array($currentLanguageUid)) {
|
||||
$currentLanguageUid = $currentLanguageUid[0];
|
||||
}
|
||||
$contentLanguageUid = (int)max($currentLanguageUid, 0);
|
||||
if ($contentLanguageUid) {
|
||||
$contentLanguage = $this->data['systemLanguageRows'][$currentLanguageUid]['iso'];
|
||||
} else {
|
||||
$contentLanguage = $this->rteConfiguration['config']['defaultContentLanguage'] ?? 'en_US';
|
||||
$languageCodeParts = explode('_', $contentLanguage);
|
||||
$contentLanguage = strtolower($languageCodeParts[0]) . ($languageCodeParts[1] ? '_' . strtoupper($languageCodeParts[1]) : '');
|
||||
// Find the configured language in the list of localization locales
|
||||
$locales = GeneralUtility::makeInstance(Locales::class);
|
||||
// If not found, default to 'en'
|
||||
if (!in_array($contentLanguage, $locales->getLocales(), true)) {
|
||||
$contentLanguage = 'en';
|
||||
}
|
||||
}
|
||||
return $contentLanguage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the JavaScript code for CKEditor module
|
||||
* Compiles the configuration, and then adds plugins
|
||||
*
|
||||
* @param string $fieldId
|
||||
* @return string
|
||||
*/
|
||||
protected function getCkEditorRequireJsModuleCode(string $fieldId): string
|
||||
{
|
||||
$configuration = $this->prepareConfigurationForEditor();
|
||||
|
||||
$externalPlugins = '';
|
||||
foreach ($this->getExtraPlugins() as $extraPluginName => $extraPluginConfig) {
|
||||
$configName = $extraPluginConfig['configName'] ?? $extraPluginName;
|
||||
if (!empty($extraPluginConfig['config']) && is_array($extraPluginConfig['config'])) {
|
||||
if (empty($configuration[$configName])) {
|
||||
$configuration[$configName] = $extraPluginConfig['config'];
|
||||
} elseif (is_array($configuration[$configName])) {
|
||||
$configuration[$configName] = array_replace_recursive($extraPluginConfig['config'], $configuration[$configName]);
|
||||
}
|
||||
}
|
||||
$configuration['extraPlugins'] .= ',' . $extraPluginName;
|
||||
|
||||
$externalPlugins .= 'CKEDITOR.plugins.addExternal(';
|
||||
$externalPlugins .= GeneralUtility::quoteJSvalue($extraPluginName) . ',';
|
||||
$externalPlugins .= GeneralUtility::quoteJSvalue($extraPluginConfig['resource']) . ',';
|
||||
$externalPlugins .= '\'\');';
|
||||
}
|
||||
|
||||
$jsonConfiguration = json_encode($configuration);
|
||||
|
||||
// Make a hash of the configuration and append it to CKEDITOR.timestamp
|
||||
// This will mitigate browser caching issue when plugins are updated
|
||||
$configurationHash = GeneralUtility::shortMD5($jsonConfiguration);
|
||||
|
||||
return 'function(CKEDITOR) {
|
||||
CKEDITOR.timestamp += "-' . $configurationHash . '";
|
||||
' . $externalPlugins . '
|
||||
require([\'jquery\', \'TYPO3/CMS/Backend/FormEngine\'], function($, FormEngine) {
|
||||
$(function(){
|
||||
var escapedFieldSelector = \'#\' + $.escapeSelector(\'' . $fieldId . '\');
|
||||
CKEDITOR.replace("' . $fieldId . '", ' . $jsonConfiguration . ');
|
||||
CKEDITOR.instances["' . $fieldId . '"].on(\'change\', function(e) {
|
||||
var commands = e.sender.commands;
|
||||
CKEDITOR.instances["' . $fieldId . '"].updateElement();
|
||||
FormEngine.Validation.validate();
|
||||
FormEngine.Validation.markFieldAsChanged($(escapedFieldSelector));
|
||||
|
||||
// remember changes done in maximized state and mark field as changed, once minimized again
|
||||
if (typeof commands.maximize !== \'undefined\' && commands.maximize.state === 1) {
|
||||
CKEDITOR.instances["' . $fieldId . '"].on(\'maximize\', function(e) {
|
||||
$(this).off(\'maximize\');
|
||||
FormEngine.Validation.markFieldAsChanged($(escapedFieldSelector));
|
||||
});
|
||||
}
|
||||
});
|
||||
CKEDITOR.instances["' . $fieldId . '"].on(\'mode\', function() {
|
||||
// detect field changes in source mode
|
||||
if (this.mode === \'source\') {
|
||||
var sourceArea = CKEDITOR.instances["' . $fieldId . '"].editable();
|
||||
sourceArea.attachListener(sourceArea, \'change\', function() {
|
||||
FormEngine.Validation.markFieldAsChanged($(escapedFieldSelector));
|
||||
});
|
||||
}
|
||||
});
|
||||
$(document).on(\'inline:sorting-changed\', function() {
|
||||
CKEDITOR.instances["' . $fieldId . '"].destroy();
|
||||
CKEDITOR.replace("' . $fieldId . '", ' . $jsonConfiguration . ');
|
||||
});
|
||||
$(document).on(\'flexform:sorting-changed\', function() {
|
||||
CKEDITOR.instances["' . $fieldId . '"].destroy();
|
||||
CKEDITOR.replace("' . $fieldId . '", ' . $jsonConfiguration . ');
|
||||
});
|
||||
});
|
||||
});
|
||||
}';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get configuration of external/additional plugins
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getExtraPlugins(): array
|
||||
{
|
||||
$urlParameters = [
|
||||
'P' => [
|
||||
'table' => $this->data['tableName'],
|
||||
'uid' => $this->data['databaseRow']['uid'],
|
||||
'fieldName' => $this->data['fieldName'],
|
||||
'recordType' => $this->data['recordTypeValue'],
|
||||
'pid' => $this->data['effectivePid'],
|
||||
'richtextConfigurationName' => $this->data['parameterArray']['fieldConf']['config']['richtextConfigurationName']
|
||||
]
|
||||
];
|
||||
|
||||
$pluginConfiguration = [];
|
||||
if (isset($this->rteConfiguration['externalPlugins']) && is_array($this->rteConfiguration['externalPlugins'])) {
|
||||
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
|
||||
foreach ($this->rteConfiguration['externalPlugins'] as $pluginName => $configuration) {
|
||||
$pluginConfiguration[$pluginName] = [
|
||||
'configName' => $configuration['configName'] ?? $pluginName,
|
||||
'resource' => $this->resolveUrlPath($configuration['resource'])
|
||||
];
|
||||
unset($configuration['configName'], $configuration['resource']);
|
||||
|
||||
if ($configuration['route']) {
|
||||
$configuration['routeUrl'] = (string)$uriBuilder->buildUriFromRoute($configuration['route'], $urlParameters);
|
||||
}
|
||||
|
||||
$pluginConfiguration[$pluginName]['config'] = $configuration;
|
||||
}
|
||||
}
|
||||
return $pluginConfiguration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add configuration to replace LLL: references with the translated value
|
||||
* @param array $configuration
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function replaceLanguageFileReferences(array $configuration): array
|
||||
{
|
||||
foreach ($configuration as $key => $value) {
|
||||
if (is_array($value)) {
|
||||
$configuration[$key] = $this->replaceLanguageFileReferences($value);
|
||||
} elseif (is_string($value) && stripos($value, 'LLL:') === 0) {
|
||||
$configuration[$key] = $this->getLanguageService()->sL($value);
|
||||
}
|
||||
}
|
||||
return $configuration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add configuration to replace absolute EXT: paths with relative ones
|
||||
* @param array $configuration
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function replaceAbsolutePathsToRelativeResourcesPath(array $configuration): array
|
||||
{
|
||||
foreach ($configuration as $key => $value) {
|
||||
if (is_array($value)) {
|
||||
$configuration[$key] = $this->replaceAbsolutePathsToRelativeResourcesPath($value);
|
||||
} elseif (is_string($value) && stripos($value, 'EXT:') === 0) {
|
||||
$configuration[$key] = $this->resolveUrlPath($value);
|
||||
}
|
||||
}
|
||||
return $configuration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves an EXT: syntax file to an absolute web URL
|
||||
*
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
protected function resolveUrlPath(string $value): string
|
||||
{
|
||||
$value = GeneralUtility::getFileAbsFileName($value);
|
||||
return PathUtility::getAbsoluteWebPath($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compiles the configuration set from the outside
|
||||
* to have it easily injected into the CKEditor.
|
||||
*
|
||||
* @return array the configuration
|
||||
*/
|
||||
protected function prepareConfigurationForEditor(): array
|
||||
{
|
||||
// Ensure custom config is empty so nothing additional is loaded
|
||||
// Of course this can be overridden by the editor configuration below
|
||||
$configuration = [
|
||||
'customConfig' => '',
|
||||
];
|
||||
|
||||
if (is_array($this->rteConfiguration['config'])) {
|
||||
$configuration = array_replace_recursive($configuration, $this->rteConfiguration['config']);
|
||||
}
|
||||
// Set the UI language of the editor if not hard-coded by the existing configuration
|
||||
if (empty($configuration['language'])) {
|
||||
$configuration['language'] = $this->getBackendUser()->uc['lang'] ?: ($this->getBackendUser()->user['lang'] ?: 'en');
|
||||
}
|
||||
$configuration['contentsLanguage'] = $this->getLanguageIsoCodeOfContent();
|
||||
|
||||
// Replace all label references
|
||||
$configuration = $this->replaceLanguageFileReferences($configuration);
|
||||
// Replace all paths
|
||||
$configuration = $this->replaceAbsolutePathsToRelativeResourcesPath($configuration);
|
||||
|
||||
// there are some places where we define an array, but it needs to be a list in order to work
|
||||
if (is_array($configuration['extraPlugins'])) {
|
||||
$configuration['extraPlugins'] = implode(',', $configuration['extraPlugins']);
|
||||
}
|
||||
if (is_array($configuration['removePlugins'])) {
|
||||
$configuration['removePlugins'] = implode(',', $configuration['removePlugins']);
|
||||
}
|
||||
if (is_array($configuration['removeButtons'])) {
|
||||
$configuration['removeButtons'] = implode(',', $configuration['removeButtons']);
|
||||
}
|
||||
|
||||
return $configuration;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $itemFormElementName
|
||||
* @return string
|
||||
*/
|
||||
protected function sanitizeFieldId(string $itemFormElementName): string
|
||||
{
|
||||
$fieldId = preg_replace('/[^a-zA-Z0-9_:.-]/', '_', $itemFormElementName);
|
||||
return htmlspecialchars(preg_replace('/^[^a-zA-Z]/', 'x', $fieldId));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BackendUserAuthentication
|
||||
*/
|
||||
protected function getBackendUser()
|
||||
{
|
||||
return $GLOBALS['BE_USER'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
namespace TYPO3\CMS\RteCKEditor\Form\Resolver;
|
||||
|
||||
/*
|
||||
* 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!
|
||||
*/
|
||||
|
||||
use TYPO3\CMS\Backend\Form\NodeFactory;
|
||||
use TYPO3\CMS\Backend\Form\NodeResolverInterface;
|
||||
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
|
||||
use TYPO3\CMS\RteCKEditor\Form\Element\RichTextElement;
|
||||
|
||||
/**
|
||||
* This resolver will return the RichTextElement render class if RTE is enabled for this field.
|
||||
* @internal This is a specific Backend FormEngine implementation and is not considered part of the Public TYPO3 API.
|
||||
*/
|
||||
class RichTextNodeResolver implements NodeResolverInterface
|
||||
{
|
||||
/**
|
||||
* Global options from NodeFactory
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* Default constructor receives full data array
|
||||
*
|
||||
* @param NodeFactory $nodeFactory
|
||||
* @param array $data
|
||||
*/
|
||||
public function __construct(NodeFactory $nodeFactory, array $data)
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns RichTextElement as class name if RTE widget should be rendered.
|
||||
*
|
||||
* @return string|void New class name or void if this resolver does not change current class name.
|
||||
*/
|
||||
public function resolve()
|
||||
{
|
||||
$parameterArray = $this->data['parameterArray'];
|
||||
$backendUser = $this->getBackendUserAuthentication();
|
||||
if (// This field is not read only
|
||||
!$parameterArray['fieldConf']['config']['readOnly']
|
||||
// If RTE is generally enabled by user settings and RTE object registry can return something valid
|
||||
&& $backendUser->isRTE()
|
||||
// If RTE is enabled for field
|
||||
&& isset($parameterArray['fieldConf']['config']['enableRichtext'])
|
||||
&& (bool)$parameterArray['fieldConf']['config']['enableRichtext'] === true
|
||||
// If RTE config is found (prepared by TcaText data provider)
|
||||
&& isset($parameterArray['fieldConf']['config']['richtextConfiguration'])
|
||||
&& is_array($parameterArray['fieldConf']['config']['richtextConfiguration'])
|
||||
// If RTE is not disabled on configuration level
|
||||
&& !$parameterArray['fieldConf']['config']['richtextConfiguration']['disabled']
|
||||
) {
|
||||
return RichTextElement::class;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BackendUserAuthentication
|
||||
*/
|
||||
protected function getBackendUserAuthentication()
|
||||
{
|
||||
return $GLOBALS['BE_USER'];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user