Commit 2ed59aea authored by Anja Leichsenring's avatar Anja Leichsenring Committed by Mathias Brodala
Browse files

[FEATURE] Backend ViewHelpers for edit creation and editing

Provides Edit- and NewRecord[Link|Uri]ViewHelpers to be used
in all places of the backend, also for extensions.

Remove four dedicated classes from system extensions, that got
replaced with the new implementations.

Also: composer update typo3/testing-framework

Resolves: #84983
Releases: master
Change-Id: I7fc03db101d2f73b63b24e4175d2e02aafa06e95
Reviewed-on: https://review.typo3.org/56934


Tested-by: default avatarTYPO3com <no-reply@typo3.com>
Reviewed-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Georg Ringer's avatarGeorg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer's avatarGeorg Ringer <georg.ringer@gmail.com>
parent 630e26c7
......@@ -4404,16 +4404,16 @@
},
{
"name": "typo3/testing-framework",
"version": "3.5.1",
"version": "3.5.3",
"source": {
"type": "git",
"url": "https://github.com/TYPO3/testing-framework.git",
"reference": "cebc38fa13859e8fc2b52795a138e605cb0ff0f3"
"reference": "53723f9b88385ef2b43fe920cadf7acb77d3fa62"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/TYPO3/testing-framework/zipball/cebc38fa13859e8fc2b52795a138e605cb0ff0f3",
"reference": "cebc38fa13859e8fc2b52795a138e605cb0ff0f3",
"url": "https://api.github.com/repos/TYPO3/testing-framework/zipball/53723f9b88385ef2b43fe920cadf7acb77d3fa62",
"reference": "53723f9b88385ef2b43fe920cadf7acb77d3fa62",
"shasum": ""
},
"require": {
......@@ -4462,7 +4462,7 @@
"tests",
"typo3"
],
"time": "2018-05-09T15:22:35+00:00"
"time": "2018-05-13T12:28:19+00:00"
},
{
"name": "webmozart/assert",
......
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Backend\ViewHelpers\Link;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
/**
* Use this ViewHelper to provide edit links to records. The ViewHelper will
* pass the uid and table to FormEngine.
*
* The uid must be given as a positive integer.
* For new records, use the newRecordViewHelper
*
* = Examples =
*
* <code title="Link to the record-edit action passed to FormEngine">
* <be:link.editRecord uid="42" table="a_table" returnUrl="foo/bar" />
* </code>
* <output>
* <a href="/typo3/index.php?route=/record/edit&edit[a_table][42]=edit&returnUrl=foo/bar">
* Edit record
* </a>
* </output>
*/
class EditRecordViewHelper extends AbstractTagBasedViewHelper
{
/**
* @var string
*/
protected $tagName = 'a';
public function initializeArguments()
{
parent::initializeArguments();
$this->registerUniversalTagAttributes();
$this->registerArgument('uid', 'int', 'uid of record to be edited', true);
$this->registerArgument('table', 'string', 'target database table', true);
$this->registerArgument('returnUrl', 'string', '', false, '');
}
/**
* @return string
* @throws \TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
*/
public function render(): string
{
if ($this->arguments['uid'] < 1) {
throw new \InvalidArgumentException('Uid must be a positive integer, ' . $this->arguments['uid'] . ' given.', 1526127158);
}
if (empty($this->arguments['returnUrl'])) {
$this->arguments['returnUrl'] = GeneralUtility::getIndpEnv('REQUEST_URI');
}
$params = [
'edit' => [$this->arguments['table'] => [$this->arguments['uid'] => 'edit']],
'returnUrl' => $this->arguments['returnUrl']
];
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
$uri = (string)$uriBuilder->buildUriFromRoute('record_edit', $params);
$this->tag->addAttribute('href', $uri);
$this->tag->setContent($this->renderChildren());
$this->tag->forceClosingTag(true);
return $this->tag->render();
}
}
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Backend\ViewHelpers\Link;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
/**
* Use this ViewHelper to provide 'create new record' links.
* The ViewHelper will pass the command to FormEngine.
*
* The table argument is mandatory, it decides what record is to be created.
*
* The pid argument will put the new record on this page, if 0 given it will
* be placed to the root page.
*
* The uid argument accepts only negative values. If this is given, the new
* record will be placed (by sorting field) behind the record with the uid.
* It will end up on the same pid as this given record, so the pid must not
* be given explicitly by pid argument.
*
* An exception will be thrown, if both uid and pid are given.
* An exception will be thrown, if the uid argument is not a negative integer.
*
* To edit records, use the editRecordViewHelper
*
* = Examples =
*
* <code title="Link to create a new record of a_table after record 17 on the same pid">
* <be:link.newRecord table="a_table" returnUrl="foo/bar" uid="-17"/>
* </code>
* <output>
* <a href="/typo3/index.php?route=/record/edit&edit[a_table][-17]=new&returnUrl=foo/bar">
* Edit record
* </a>
* </output>
*
* <code title="Link to create a new record of a_table on root page">
* <be:link.newRecord table="a_table" returnUrl="foo/bar""/>
* </code>
* <output>
* <a href="/typo3/index.php?route=/record/edit&edit[a_table][]=new&returnUrl=foo/bar">
* Edit record
* </a>
* </output>
*
* <code title="Link to create a new record of a_table on page 17">
* <be:link.newRecord table="a_table" returnUrl="foo/bar" pid="17"/>
* </code>
* <output>
* <a href="/typo3/index.php?route=/record/edit&edit[a_table][-17]=new&returnUrl=foo/bar">
* Edit record
* </a>
* </output>
*/
class NewRecordViewHelper extends AbstractTagBasedViewHelper
{
/**
* @var string
*/
protected $tagName = 'a';
public function initializeArguments()
{
parent::initializeArguments();
$this->registerUniversalTagAttributes();
$this->registerArgument('uid', 'int', 'uid < 0 will insert the record after the given uid', false);
$this->registerArgument('pid', 'int', 'the page id where the record will be created', false);
$this->registerArgument('table', 'string', 'target database table', true);
$this->registerArgument('returnUrl', 'string', '', false, '');
}
/**
* @return string
* @throws \TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
*/
public function render(): string
{
if ($this->arguments['uid'] && $this->arguments['pid']) {
throw new \InvalidArgumentException('Can\'t handle both uid and pid for new records', 1526129969);
}
if (isset($this->arguments['uid']) && $this->arguments['uid'] >= 0) {
throw new \InvalidArgumentException('Uid must be negative integer, ' . $this->arguments['uid'] . ' given', 1526134901);
}
if (empty($this->arguments['returnUrl'])) {
$this->arguments['returnUrl'] = GeneralUtility::getIndpEnv('REQUEST_URI');
}
$params = [
'edit' => [$this->arguments['table'] => [$this->arguments['uid'] ?? $this->arguments['pid'] ?? 0 => 'new']],
'returnUrl' => $this->arguments['returnUrl']
];
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
$uri = (string)$uriBuilder->buildUriFromRoute('record_edit', $params);
$this->tag->addAttribute('href', $uri);
$this->tag->setContent($this->renderChildren());
$this->tag->forceClosingTag(true);
return $this->tag->render();
}
}
<?php
namespace TYPO3\CMS\Filelist\ViewHelpers\Uri;
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\Backend\ViewHelpers\Uri;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
......@@ -21,38 +10,51 @@ use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
/**
* Class EditSysFileMetadataRecordViewHelper
* Use this ViewHelper to provide edit links (only the uri) to records. The ViewHelper will
* pass the uid and table to FormEngine.
*
* The uid must be given as a positive integer.
* For new records, use the newRecordViewHelper
*
* = Examples =
*
* <code title="URI to the record-edit action passed to FormEngine">
* <be:uri.editRecord uid="42" table="a_table" returnUrl="foo/bar" />
* </code>
* <output>
* /typo3/index.php?route=/record/edit&edit[a_table][42]=edit&returnUrl=foo/bar
* </output>
*/
class EditSysFileMetadataRecordViewHelper extends AbstractViewHelper
class EditRecordViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;
/**
* Initialize arguments
*/
public function initializeArguments()
{
$this->registerArgument('uid', 'int', '', true);
$this->registerArgument('uid', 'int', 'uid of record to be edited, 0 for creation', true);
$this->registerArgument('table', 'string', 'target database table', true);
$this->registerArgument('returnUrl', 'string', '', false, '');
}
/**
* Renders a link to edit sys_file_metadata
*
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
*
* @return string
* @throws \TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
{
if ($arguments['uid'] < 1) {
throw new \InvalidArgumentException('Uid must be a positive integer, ' . $arguments['uid'] . ' given.', 1526128259);
}
if (empty($arguments['returnUrl'])) {
$arguments['returnUrl'] = GeneralUtility::getIndpEnv('REQUEST_URI');
}
$params = [
'edit' => ['sys_file_metadata' => [$arguments['uid'] => 'edit']],
'edit' => [$arguments['table'] => [$arguments['uid'] => 'edit']],
'returnUrl' => $arguments['returnUrl']
];
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
......
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Backend\ViewHelpers\Uri;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
/**
* Use this ViewHelper to provide 'create new record' links.
* The ViewHelper will pass the command to FormEngine.
*
* The table argument is mandatory, it decides what record is to be created.
*
* The pid argument will put the new record on this page, if 0 given it will
* be placed to the root page.
*
* The uid argument accepts only negative values. If this is given, the new
* record will be placed (by sorting field) behind the record with the uid.
* It will end up on the same pid as this given record, so the pid must not
* be given explicitly by pid argument.
*
* An exception will be thrown, if both uid and pid are given.
* An exception will be thrown, if the uid argument is not a negative integer.
*
* To edit records, use the editRecordViewHelper
*
* = Examples =
*
* <code title="Uri to create a new record of a_table after record 17 on the same pid">
* <be:uri.newRecord table="a_table" returnUrl="foo/bar" uid="-17"/>
* </code>
* <output>
* /typo3/index.php?route=/record/edit&edit[a_table][-17]=new&returnUrl=foo/bar
* </output>
*
* <code title="Uri to create a new record of a_table on root page">
* <be:uri.newRecord table="a_table" returnUrl="foo/bar""/>
* </code>
* <output>
* /typo3/index.php?route=/record/edit&edit[a_table][]=new&returnUrl=foo/bar
* </output>
*
* <code title="Uri to create a new record of a_table on page 17">
* <be:uri.newRecord table="a_table" returnUrl="foo/bar" pid="17"/>
* </code>
* <output>
* /typo3/index.php?route=/record/edit&edit[a_table][-17]=new&returnUrl=foo/bar
* </output>
*/
class NewRecordViewHelper extends AbstractTagBasedViewHelper
{
use CompileWithRenderStatic;
public function initializeArguments()
{
$this->registerArgument('uid', 'int', 'uid < 0 will insert the record after the given uid', false);
$this->registerArgument('pid', 'int', 'the page id where the record will be created', false);
$this->registerArgument('table', 'string', 'target database table', true);
$this->registerArgument('returnUrl', 'string', '', false, '');
}
/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
*
* @return string
* @throws \TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
{
if ($arguments['uid'] && $arguments['pid']) {
throw new \InvalidArgumentException('Can\'t handle both uid and pid for new records', 1526136338);
}
if (isset($arguments['uid']) && $arguments['uid'] >= 0) {
throw new \InvalidArgumentException('Uid must be negative integer, ' . $arguments['uid'] . ' given', 1526136362);
}
if (empty($arguments['returnUrl'])) {
$arguments['returnUrl'] = GeneralUtility::getIndpEnv('REQUEST_URI');
}
$params = [
'edit' => [$arguments['table'] => [$arguments['uid'] ?? $arguments['pid'] ?? 0 => 'new']],
'returnUrl' => $arguments['returnUrl']
];
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
return (string)$uriBuilder->buildUriFromRoute('record_edit', $params);
}
}
<html xmlns:be="http://typo3.org/ns/TYPO3/CMS/Backend/ViewHelpers">
{be:link.editRecord(uid: 21, table: 'b_table')}
</html>
<html xmlns:be="http://typo3.org/ns/TYPO3/CMS/Backend/ViewHelpers">
<be:link.editRecord uid="-42" table="c_table">edit record c_table:-42</be:link.editRecord>
</html>
<html xmlns:be="http://typo3.org/ns/TYPO3/CMS/Backend/ViewHelpers">
<be:link.editRecord uid="42" table="a_table">edit record a_table:42</be:link.editRecord>
</html>
<html xmlns:be="http://typo3.org/ns/TYPO3/CMS/Backend/ViewHelpers">
<be:link.editRecord uid="43" table="c_table" returnUrl="foo/bar">edit record c_table:43</be:link.editRecord>
</html>
<html xmlns:be="http://typo3.org/ns/TYPO3/CMS/Backend/ViewHelpers">
{be:link.newRecord(table: 'b_table', pid:17)}
</html>
<html xmlns:be="http://typo3.org/ns/TYPO3/CMS/Backend/ViewHelpers">
<be:link.newRecord uid="-11" table="c_table">new record at c_table after record with uid 11</be:link.newRecord>
</html>
<html xmlns:be="http://typo3.org/ns/TYPO3/CMS/Backend/ViewHelpers">
<be:link.newRecord table="a_table" pid="17">new record at a_table on page 17</be:link.newRecord>
</html>
<html xmlns:be="http://typo3.org/ns/TYPO3/CMS/Backend/ViewHelpers">
<be:link.newRecord table="c_table" returnUrl="foo/bar" pid="17">new record at c_table</be:link.newRecord>
</html>
<html xmlns:be="http://typo3.org/ns/TYPO3/CMS/Backend/ViewHelpers">
<be:link.newRecord uid="42" table="c_table">if uid given, it must be negative</be:link.newRecord>
</html>
<html xmlns:be="http://typo3.org/ns/TYPO3/CMS/Backend/ViewHelpers">
<be:link.newRecord table="a_table">new record at a_table on root</be:link.newRecord>
</html>
<html xmlns:be="http://typo3.org/ns/TYPO3/CMS/Backend/ViewHelpers">
<be:link.newRecord uid="-42" pid="18" table="c_table">can't handle uid and pid together</be:link.newRecord>
</html>
<html xmlns:be="http://typo3.org/ns/TYPO3/CMS/Backend/ViewHelpers">
{be:uri.editRecord(uid: 21, table: 'b_table')}
</html>
<html xmlns:be="http://typo3.org/ns/TYPO3/CMS/Backend/ViewHelpers">
<be:uri.editRecord uid="-42" table="c_table">edit record c_table:-42</be:uri.editRecord>
</html>
<html xmlns:be="http://typo3.org/ns/TYPO3/CMS/Backend/ViewHelpers">
<be:uri.editRecord uid="42" table="a_table">edit record a_table:42</be:uri.editRecord>
</html>
<html xmlns:be="http://typo3.org/ns/TYPO3/CMS/Backend/ViewHelpers">
<be:uri.editRecord uid="43" table="c_table" returnUrl="foo/bar">edit record c_table:43</be:uri.editRecord>
</html>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment