[FEATURE] Add argument "fields" to editRecord ViewHelpers 98/62298/8
authorSybille Peters <sypets@gmx.de>
Mon, 11 Nov 2019 15:24:58 +0000 (16:24 +0100)
committerBenni Mack <benni@typo3.org>
Tue, 31 Dec 2019 13:26:47 +0000 (14:26 +0100)
The <be:link.editRecord> and <be:url.editRecord> now have an option
argument "fields" which can be used to limit the edit form to only
these fields (instead of the entire record).

The fields should be passed in a comma separated list.

Resolves: #89644
Releases: master
Change-Id: I28913c072578aacd1da95c3bacebea35f8958bac
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/62298
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Oliver Bartsch <bo@cedev.de>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
Reviewed-by: Daniel Siepmann <coding@daniel-siepmann.de>
Reviewed-by: Benni Mack <benni@typo3.org>
typo3/sysext/backend/Classes/ViewHelpers/Link/EditRecordViewHelper.php
typo3/sysext/backend/Classes/ViewHelpers/Uri/EditRecordViewHelper.php
typo3/sysext/backend/Tests/Functional/ViewHelpers/Fixtures/Link/EditRecordViewHelper/WithUidTableAndField.html [new file with mode: 0644]
typo3/sysext/backend/Tests/Functional/ViewHelpers/Fixtures/Link/EditRecordViewHelper/WithUidTableAndFields.html [new file with mode: 0644]
typo3/sysext/backend/Tests/Functional/ViewHelpers/Fixtures/Uri/EditRecordViewHelper/WithUidTableAndField.html [new file with mode: 0644]
typo3/sysext/backend/Tests/Functional/ViewHelpers/Fixtures/Uri/EditRecordViewHelper/WithUidTableAndFields.html [new file with mode: 0644]
typo3/sysext/backend/Tests/Functional/ViewHelpers/Link/EditRecordViewHelperTest.php
typo3/sysext/backend/Tests/Functional/ViewHelpers/Uri/EditRecordViewHelperTest.php
typo3/sysext/core/Documentation/Changelog/master/Feature-89644-AddOptionalArgumentFieldsToEditRecordViewHelpers.rst [new file with mode: 0644]

index 579dfd6..d6c9b15 100644 (file)
@@ -42,6 +42,18 @@ use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
  * Link to edit page uid=3 and then return back to the BE module "web_MyextensionList"::
  *
  *    <be.link.editRecord uid="3" table="pages" returnUrl="{f:be.uri(route: 'web_MyextensionList')}">
+ *
+ * Link to edit only the fields title and subtitle of page uid=42 and return to foo/bar::
+ *
+ *    <be:link.editRecord uid="42" table="pages" fields="title,subtitle" returnUrl="foo/bar">
+ *        Edit record
+ *    </be:link.editRecord>
+ *
+ * Output::
+ *
+ *    <a href="/typo3/index.php?route=/record/edit&edit[pages][42]=edit&returnUrl=foo/bar&columnsOnly=title,subtitle">
+ *        Edit record
+ *    </a>
  */
 class EditRecordViewHelper extends AbstractTagBasedViewHelper
 {
@@ -56,6 +68,7 @@ class EditRecordViewHelper extends AbstractTagBasedViewHelper
         $this->registerUniversalTagAttributes();
         $this->registerArgument('uid', 'int', 'uid of record to be edited', true);
         $this->registerArgument('table', 'string', 'target database table', true);
+        $this->registerArgument('fields', 'string', 'Edit only these fields (comma separated list)', false);
         $this->registerArgument('returnUrl', 'string', 'return to this URL after closing the edit dialog', false, '');
     }
 
@@ -76,6 +89,9 @@ class EditRecordViewHelper extends AbstractTagBasedViewHelper
             'edit' => [$this->arguments['table'] => [$this->arguments['uid'] => 'edit']],
             'returnUrl' => $this->arguments['returnUrl']
         ];
+        if ($this->arguments['fields'] ?? false) {
+            $params['columnsOnly'] = $this->arguments['fields'];
+        }
         $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
         $uri = (string)$uriBuilder->buildUriFromRoute('record_edit', $params);
         $this->tag->addAttribute('href', $uri);
index 0b13020..8261d85 100644 (file)
@@ -37,6 +37,13 @@ use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
  *    <be:uri.editRecord uid="42" table="a_table" returnUrl="foo/bar" />
  *
  * ``/typo3/index.php?route=/record/edit&edit[a_table][42]=edit&returnUrl=foo/bar``
+ *
+ * URI to the edit record action: edit only the fields title and subtitle of
+ * page uid=42 and return to foo/bar::
+ *
+ *    <be:uri.editRecord uid="42" table="pages" fields="title,subtitle" returnUrl="foo/bar" />
+ *
+ * ``<a href="/typo3/index.php?route=/record/edit&edit[pages][42]=edit&returnUrl=foo/bar&columnsOnly=title,subtitle">``
  */
 class EditRecordViewHelper extends AbstractViewHelper
 {
@@ -46,6 +53,7 @@ class EditRecordViewHelper extends AbstractViewHelper
     {
         $this->registerArgument('uid', 'int', 'uid of record to be edited, 0 for creation', true);
         $this->registerArgument('table', 'string', 'target database table', true);
+        $this->registerArgument('fields', 'string', 'Edit only these fields (comma separated list)', false);
         $this->registerArgument('returnUrl', 'string', '', false, '');
     }
 
@@ -70,6 +78,9 @@ class EditRecordViewHelper extends AbstractViewHelper
             'edit' => [$arguments['table'] => [$arguments['uid'] => 'edit']],
             'returnUrl' => $arguments['returnUrl']
         ];
+        if ($arguments['fields'] ?? false) {
+            $params['columnsOnly'] = $arguments['fields'];
+        }
         $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
         return (string)$uriBuilder->buildUriFromRoute('record_edit', $params);
     }
diff --git a/typo3/sysext/backend/Tests/Functional/ViewHelpers/Fixtures/Link/EditRecordViewHelper/WithUidTableAndField.html b/typo3/sysext/backend/Tests/Functional/ViewHelpers/Fixtures/Link/EditRecordViewHelper/WithUidTableAndField.html
new file mode 100644 (file)
index 0000000..3016853
--- /dev/null
@@ -0,0 +1,3 @@
+<html xmlns:be="http://typo3.org/ns/TYPO3/CMS/Backend/ViewHelpers">
+<be:link.editRecord uid="43" table="c_table" fields="canonical_url">edit record c_table:42</be:link.editRecord>
+</html>
diff --git a/typo3/sysext/backend/Tests/Functional/ViewHelpers/Fixtures/Link/EditRecordViewHelper/WithUidTableAndFields.html b/typo3/sysext/backend/Tests/Functional/ViewHelpers/Fixtures/Link/EditRecordViewHelper/WithUidTableAndFields.html
new file mode 100644 (file)
index 0000000..9c55b47
--- /dev/null
@@ -0,0 +1,3 @@
+<html xmlns:be="http://typo3.org/ns/TYPO3/CMS/Backend/ViewHelpers">
+<be:link.editRecord uid="43" table="c_table" fields="canonical_url,title">edit record c_table:42</be:link.editRecord>
+</html>
diff --git a/typo3/sysext/backend/Tests/Functional/ViewHelpers/Fixtures/Uri/EditRecordViewHelper/WithUidTableAndField.html b/typo3/sysext/backend/Tests/Functional/ViewHelpers/Fixtures/Uri/EditRecordViewHelper/WithUidTableAndField.html
new file mode 100644 (file)
index 0000000..c98f5c2
--- /dev/null
@@ -0,0 +1,3 @@
+<html xmlns:be="http://typo3.org/ns/TYPO3/CMS/Backend/ViewHelpers">
+<be:uri.editRecord uid="43" table="c_table" fields="canonical_url">edit record c_table:43</be:uri.editRecord>
+</html>
diff --git a/typo3/sysext/backend/Tests/Functional/ViewHelpers/Fixtures/Uri/EditRecordViewHelper/WithUidTableAndFields.html b/typo3/sysext/backend/Tests/Functional/ViewHelpers/Fixtures/Uri/EditRecordViewHelper/WithUidTableAndFields.html
new file mode 100644 (file)
index 0000000..5590dcd
--- /dev/null
@@ -0,0 +1,3 @@
+<html xmlns:be="http://typo3.org/ns/TYPO3/CMS/Backend/ViewHelpers">
+<be:uri.editRecord uid="43" table="c_table" fields="canonical_url,title">edit record c_table:43</be:uri.editRecord>
+</html>
index 3c43173..01baaec 100644 (file)
@@ -68,6 +68,34 @@ class EditRecordViewHelperTest extends FunctionalTestCase
     /**
      * @test
      */
+    public function renderReturnsValidLinkWithField()
+    {
+        $view = GeneralUtility::makeInstance(StandaloneView::class);
+        $view->setTemplatePathAndFilename('EXT:backend/Tests/Functional/ViewHelpers/Fixtures/Link/EditRecordViewHelper/WithUidTableAndField.html');
+        $result = urldecode($view->render());
+
+        self::assertStringContainsString('route=/record/edit', $result);
+        self::assertStringContainsString('edit[c_table][43]=edit', $result);
+        self::assertStringContainsString('columnsOnly=canonical_url', $result);
+    }
+
+    /**
+     * @test
+     */
+    public function renderReturnsValidLinkWithFields()
+    {
+        $view = GeneralUtility::makeInstance(StandaloneView::class);
+        $view->setTemplatePathAndFilename('EXT:backend/Tests/Functional/ViewHelpers/Fixtures/Link/EditRecordViewHelper/WithUidTableAndFields.html');
+        $result = urldecode($view->render());
+
+        self::assertStringContainsString('route=/record/edit', $result);
+        self::assertStringContainsString('edit[c_table][43]=edit', $result);
+        self::assertStringContainsString('columnsOnly=canonical_url,title', $result);
+    }
+
+    /**
+     * @test
+     */
     public function renderThrowsExceptionForInvalidUidArgument()
     {
         $this->expectException(\InvalidArgumentException::class);
index 8183ce6..5d85ada 100644 (file)
@@ -68,6 +68,34 @@ class EditRecordViewHelperTest extends FunctionalTestCase
     /**
      * @test
      */
+    public function renderReturnsValidLinkWithField()
+    {
+        $view = GeneralUtility::makeInstance(StandaloneView::class);
+        $view->setTemplatePathAndFilename('EXT:backend/Tests/Functional/ViewHelpers/Fixtures/Uri/EditRecordViewHelper/WithUidTableAndField.html');
+        $result = urldecode($view->render());
+
+        self::assertStringContainsString('route=/record/edit', $result);
+        self::assertStringContainsString('edit[c_table][43]=edit', $result);
+        self::assertStringContainsString('columnsOnly=canonical_url', $result);
+    }
+
+    /**
+     * @test
+     */
+    public function renderReturnsValidLinkWithFields()
+    {
+        $view = GeneralUtility::makeInstance(StandaloneView::class);
+        $view->setTemplatePathAndFilename('EXT:backend/Tests/Functional/ViewHelpers/Fixtures/Uri/EditRecordViewHelper/WithUidTableAndFields.html');
+        $result = urldecode($view->render());
+
+        self::assertStringContainsString('route=/record/edit', $result);
+        self::assertStringContainsString('edit[c_table][43]=edit', $result);
+        self::assertStringContainsString('columnsOnly=canonical_url,title', $result);
+    }
+
+    /**
+     * @test
+     */
     public function renderThrowsExceptionForInvalidUidArgument()
     {
         $this->expectException(\InvalidArgumentException::class);
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-89644-AddOptionalArgumentFieldsToEditRecordViewHelpers.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-89644-AddOptionalArgumentFieldsToEditRecordViewHelpers.rst
new file mode 100644 (file)
index 0000000..9158341
--- /dev/null
@@ -0,0 +1,49 @@
+.. include:: ../../Includes.txt
+
+==========================================================================
+Feature: #89644 - Add optional argument "fields" to editRecord ViewHelpers
+==========================================================================
+
+See :issue:`89644`
+
+Description
+===========
+
+An optional argument "fields" is added to the uri.editRecord and link.editRecord ViewHelper.
+This can contain the names of one or more database fields (comma separated).
+
+If the argument "fields" is set, FormEngine creates a form to edit only these fields.
+
+
+Impact
+======
+
+This ViewHelper passes the value given in the `fields` argument to the backend route
+'/record/edit` as `columnsOnly` argument.
+
+The functionality for `columnsOnly` already existed for the backend route
+`/record/edit` before this patch.
+
+Example
+=======
+
+Create a link to edit the `tt_content.bodytext` field of record with uid 42:
+
+.. code-block:: xml
+
+   <be:link.editRecord uid="42" table="tt_content" fields="bodytext" returnUrl="foo/bar">
+        Edit record
+   </be:link.editRecord>
+
+Output:
+
+.. code-block:: html
+
+
+   <a href="/typo3/index.php?route=/record/edit&edit[tt_content][42]=edit&returnUrl=foo/bar&columnsOnly=bodytext">
+       Edit record
+   </a>
+
+
+
+.. index:: Fluid, ext:backend