Skip to content

Commit

Permalink
[TASK] Update and Improve Backend UriBuilder examples (#276)
Browse files Browse the repository at this point in the history
Move link examples to Partials so they can be included individually
  • Loading branch information
linawolf authored Jun 17, 2024
1 parent 77794ba commit 6f5c001
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 60 deletions.
110 changes: 64 additions & 46 deletions Classes/Controller/ModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
use Psr\Log\LoggerInterface;
use T3docs\Examples\Service\TableInformationService;
use TYPO3\CMS\Backend\Clipboard\Clipboard;
Expand Down Expand Up @@ -52,7 +53,7 @@
class ModuleController extends ActionController
{
private int $pageUid;
/** @var array<string, mixed> */
/** @var array<string, mixed> */
private array $exampleConfig;

public function __construct(
Expand All @@ -64,7 +65,8 @@ public function __construct(
protected readonly FileRepository $fileRepository,
protected readonly ConnectionPool $connectionPool,
protected readonly TableInformationService $tableInformationService,
private readonly LoggerInterface $logger,
protected readonly LoggerInterface $logger,
protected readonly UriBuilder $backendUriBuilder,
) {}

/**
Expand Down Expand Up @@ -296,72 +298,88 @@ protected function debugClipboard(): void
*/
public function linksAction(): ResponseInterface
{
$backendUriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
$uriParameters = ['edit' => ['pages' => [1 => 'edit']]];
$editPage1Link = $backendUriBuilder->buildUriFromRoute(
'record_edit',
$uriParameters,
);
$pageUid = (int)($this->request->getQueryParams()['id'] ?? 0);
$returnUrl = (string)$backendUriBuilder->buildUriFromRoute(
$returnUrl = (string)$this->backendUriBuilder->buildUriFromRoute(
'web_examples',
['id' => $pageUid, 'action' => 'links'],
);

$uriParameters =
$editPage1Link = $this->getEditPageLink(1, $returnUrl);
$editPagesDoktypeLink = $this->getEditDoktypeLink($returnUrl);
$createHaikuLink = $this->getCreateHaikuLink($returnUrl);

$view = $this->initializeModuleTemplate($this->request);
$view->assignMultiple(
[
'edit' =>
[
'pages' =>
[
1 => 'edit',
2 => 'edit',
],
'tx_examples_haiku' =>
[
1 => 'edit',
],
],
'columnsOnly' => 'title,doktype',
'editPage1Link' => $editPage1Link,
'editPagesDoktypeLink' => $editPagesDoktypeLink,
'createHaikuLink' => $createHaikuLink,
'returnUrl' => $returnUrl,
];
$editPagesDoktypeLink = $backendUriBuilder->buildUriFromRoute(
],
);
return $view->renderResponse();
}

private function getEditPageLink(int $uid, string $returnUrl): UriInterface
{
$uriParameters = [
'edit' => [
'pages' => [
$uid => 'edit',
],
],
'returnUrl' => $returnUrl,
];
return $this->backendUriBuilder->buildUriFromRoute(
'record_edit',
$uriParameters,
);
}

protected function getCreateHaikuLink(string $returnUrl): UriInterface
{
$uriParameters =
[
'edit' =>
[
'tx_examples_haiku' =>
[
1 => 'new',
],
'edit' => [
'tx_examples_haiku' => [
1 => 'new',
],
'defVals' =>
[
'tx_examples_haiku' =>
[
'title' => 'New Haiku?',
'season' => 'Spring',
],
],
'defVals' => [
'tx_examples_haiku' => [
'title' => 'New Haiku?',
'season' => 'Spring',
],
],
'columnsOnly' => 'title,season,color',
'returnUrl' => $returnUrl,
];
$createHaikuLink = $backendUriBuilder->buildUriFromRoute(
return $this->backendUriBuilder->buildUriFromRoute(
'record_edit',
$uriParameters,
);
}

$view = $this->initializeModuleTemplate($this->request);
$view->assignMultiple(
protected function getEditDoktypeLink(string $returnUrl): UriInterface
{
$uriParameters =
[
'editPage1Link' => $editPage1Link,
'editPagesDoktypeLink' => $editPagesDoktypeLink,
'createHaikuLink' => $createHaikuLink,
],
'edit' => [
'pages' => [
1 => 'edit',
2 => 'edit',
],
'tx_examples_haiku' => [
1 => 'edit',
],
],
'columnsOnly' => 'title,doktype',
'returnUrl' => $returnUrl,
];
return $this->backendUriBuilder->buildUriFromRoute(
'record_edit',
$uriParameters,
);
return $view->renderResponse();
}

/**
Expand Down
10 changes: 10 additions & 0 deletions Resources/Private/Partials/Module/CreateHaikuBlankLink.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html data-namespace-typo3-fluid="true"
xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
xmlns:core="http://typo3.org/ns/TYPO3/CMS/Core/ViewHelpers"
xmlns:be="http://typo3.org/ns/TYPO3/CMS/Backend/ViewHelpers"
>
<a href="{be:uri.newRecord(pid:1, table:'tx_examples_haiku', returnUrl:returnUrl)}">
<core:icon identifier="actions-document-new"/>
<f:translate key="function_links_new_haiku"/>
</a>
</html>
9 changes: 9 additions & 0 deletions Resources/Private/Partials/Module/CreateHaikuLink.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html data-namespace-typo3-fluid="true"
xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
xmlns:core="http://typo3.org/ns/TYPO3/CMS/Core/ViewHelpers"
>
<a href="{createHaikuLink}">
<core:icon identifier="actions-document-new"/>
<f:translate key="function_links_new_spring_haiku"/>
</a>
</html>
9 changes: 9 additions & 0 deletions Resources/Private/Partials/Module/EditPage1Link.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html data-namespace-typo3-fluid="true"
xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
xmlns:core="http://typo3.org/ns/TYPO3/CMS/Core/ViewHelpers"
>
<a href="{editPage1Link}">
<core:icon identifier="actions-document-open"/>
Edit page 1
</a>
</html>
10 changes: 10 additions & 0 deletions Resources/Private/Partials/Module/EditPage2Link.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html data-namespace-typo3-fluid="true"
xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
xmlns:core="http://typo3.org/ns/TYPO3/CMS/Core/ViewHelpers"
xmlns:be="http://typo3.org/ns/TYPO3/CMS/Backend/ViewHelpers"
>
<a href="{be:uri.editRecord(uid:2, table:'pages', returnUrl: returnUrl)}">
<core:icon identifier="actions-document-open"/>
Edit page 2
</a>
</html>
17 changes: 3 additions & 14 deletions Resources/Private/Templates/Module/Links.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,10 @@ <h1>
<f:translate key="function_links_intro"/>
</p>
<p>
<a href="{editPage1Link}">
<core:icon identifier="actions-document-open"/>
<f:translate key="function_links_edit_page"/> 1
</a>
<f:render partial="Module/EditPage1Link.html" arguments="{_all}"/>
</p>
<p>
<f:comment>Would only output the uri: <be:uri.editRecord uid="2" table="pages" returnUrl="foo/bar" /></f:comment>
<a href="{be:uri.editRecord(uid:2, table:'pages', returnUrl:'foo/bar')}">
<core:icon identifier="actions-document-open"/>
<f:translate key="function_links_edit_page"/> 2
</a>
<f:render partial="Module/EditPage2Link.html" arguments="{_all}"/>
</p>
<p>
<a href="{editPagesDoktypeLink}">
Expand All @@ -32,17 +25,13 @@ <h1>
</a>
</p>
<p>
<f:comment>Would only output the uri: <be:uri.newRecord pid="1" table="tx_examples_haiku" returnUrl="foo/bar" /></f:comment>
<a href="{be:uri.newRecord(pid:1, table:'tx_examples_haiku', returnUrl:'foo/bar')}">
<core:icon identifier="actions-document-new"/>
<f:translate key="function_links_new_haiku"/>
</a>
</p>
<p>
<a href="{createHaikuLink}">
<core:icon identifier="actions-document-new"/>
<f:translate key="function_links_new_spring_haiku"/>
</a>
<f:render partial="Module/CreateHaikuLink.html" arguments="{_all}"/>
</p>
</f:section>
</html>

0 comments on commit 6f5c001

Please sign in to comment.