[FEATURE] Add Example how to resolve file relations for items (#3)

This commit is contained in:
Daniel Siepmann 2022-09-28 12:08:22 +02:00 committed by GitHub
parent d38af239ee
commit 85b132e759
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 61 additions and 14 deletions

View file

@ -24,6 +24,8 @@ declare(strict_types=1);
namespace WerkraumMedia\Watchlist\Domain\Items\Page; namespace WerkraumMedia\Watchlist\Domain\Items\Page;
use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Resource\FileReference;
use TYPO3\CMS\Core\Resource\FileRepository;
use WerkraumMedia\Watchlist\Domain\ItemHandlerInterface; use WerkraumMedia\Watchlist\Domain\ItemHandlerInterface;
use WerkraumMedia\Watchlist\Domain\Model\Item; use WerkraumMedia\Watchlist\Domain\Model\Item;
@ -31,10 +33,14 @@ class ItemHandler implements ItemHandlerInterface
{ {
private ConnectionPool $connectionPool; private ConnectionPool $connectionPool;
private FileRepository $fileRepository;
public function __construct( public function __construct(
ConnectionPool $connectionPool ConnectionPool $connectionPool,
FileRepository $fileRepository
) { ) {
$this->connectionPool = $connectionPool; $this->connectionPool = $connectionPool;
$this->fileRepository = $fileRepository;
} }
public function return(string $identifier): ?Item public function return(string $identifier): ?Item
@ -44,7 +50,8 @@ class ItemHandler implements ItemHandlerInterface
return new Page( return new Page(
$pageUid, $pageUid,
(string)$pageRecord['title'] (string)$pageRecord['title'],
$this->getImage($pageUid)
); );
} }
@ -62,4 +69,9 @@ class ItemHandler implements ItemHandlerInterface
$qb->setMaxResults(1); $qb->setMaxResults(1);
return $qb->execute()->fetchAssociative() ?: []; return $qb->execute()->fetchAssociative() ?: [];
} }
private function getImage(int $uid): ?FileReference
{
return $this->fileRepository->findByRelation('pages', 'media', $uid)[0] ?? null;
}
} }

View file

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace WerkraumMedia\Watchlist\Domain\Items\Page; namespace WerkraumMedia\Watchlist\Domain\Items\Page;
use TYPO3\CMS\Core\Resource\FileInterface;
use WerkraumMedia\Watchlist\Domain\Model\Item; use WerkraumMedia\Watchlist\Domain\Model\Item;
class Page implements Item class Page implements Item
@ -31,12 +32,16 @@ class Page implements Item
private string $title; private string $title;
private ?FileInterface $image;
public function __construct( public function __construct(
int $pageUid, int $pageUid,
string $title string $title,
?FileInterface $image
) { ) {
$this->pageUid = $pageUid; $this->pageUid = $pageUid;
$this->title = $title; $this->title = $title;
$this->image = $image;
} }
public function getUniqueIdentifier(): string public function getUniqueIdentifier(): string
@ -48,4 +53,9 @@ class Page implements Item
{ {
return $this->title; return $this->title;
} }
public function getImage(): ?FileInterface
{
return $this->image;
}
} }

View file

@ -61,6 +61,18 @@ The Handler needs to be registered via Symfony Tags, e.g. via ``Services.yaml``:
The class needs to implement the ``WerkraumMedia\Watchlist\Domain\Model\Item``. The class needs to implement the ``WerkraumMedia\Watchlist\Domain\Model\Item``.
Example
-------
The extension delivers an example implementation for testing purposes, check out:
- ``Classes/Domain/Items/Page/ItemHandler.php``
- ``Classes/Domain/Items/Page/Page.php``
The example demonstrates how to fetch information from database,
including file references.
JavaScript JavaScript
========== ==========

View file

@ -10,7 +10,12 @@
<f:else> <f:else>
<ol> <ol>
<f:for each="{watchlist.items}" as="item"> <f:for each="{watchlist.items}" as="item">
<li>{item.title}</li> <li>
{item.title}
<f:if condition="{item.image}">
<f:image image="{item.image}" />
</f:if>
</li>
</f:for> </f:for>
</ol> </ol>
</f:else> </f:else>

View file

@ -1,8 +1,14 @@
"pages",,,,,, "pages",,,,,,,,,,,,,,
,"uid","pid","slug","title",, ,"uid","pid","slug","title",,,,,,,,,,
,1,0,"/","Page Title",, ,1,0,"/","Page Title",,,,,,,,,,
,2,1,"/page-2","Page 2 Title",, ,2,1,"/page-2","Page 2 Title",,,,,,,,,,
"sys_template",,,,,, "sys_template",,,,,,,,,,,,,,
,"uid","pid","root","clear","constants","config" ,"uid","pid","root","clear","constants","config",,,,,,,,
,1,1,1,3,"databasePlatform = mysql","<INCLUDE_TYPOSCRIPT: source=""FILE:EXT:watchlist/Tests/Fixtures/FrontendRendering.typoscript""> ,1,1,1,3,"databasePlatform = mysql","<INCLUDE_TYPOSCRIPT: source=""FILE:EXT:watchlist/Tests/Fixtures/FrontendRendering.typoscript"">
<INCLUDE_TYPOSCRIPT: source=""FILE:EXT:fluid_styled_content/Configuration/TypoScript/setup.typoscript"">" <INCLUDE_TYPOSCRIPT: source=""FILE:EXT:fluid_styled_content/Configuration/TypoScript/setup.typoscript"">",,,,,,,,
"sys_file",,,,,,,,,,,,,,
,"uid","pid","missing","storage","type","metadata","identifier","identifier_hash","folder_hash","extension","mime_type","name","sha1","size"
,1,0,0,1,2,0,"/Files/FirstResult.png","29b827d0daa29658d8a0d952dfd20f559bbe3bcf","86d12d536195df2100a5ec04ab80c08f9bed3d31","png","image/png","FirstResult.png","b13f2bbf275d592534eab659c1430c2702ce31fc",42383
"sys_file_reference",,,,,,,,,,,,,,
,"uid","pid","uid_local","uid_foreign","tablenames","fieldname","sorting_foreign","table_local",,,,,,
,1,1,1,1,"pages","media",1,"sys_file",,,,,,

1 pages
2 uid pid slug title
3 1 0 / Page Title
4 2 1 /page-2 Page 2 Title
5 sys_template
6 uid pid root clear constants config
7 1 1 1 3 databasePlatform = mysql <INCLUDE_TYPOSCRIPT: source="FILE:EXT:watchlist/Tests/Fixtures/FrontendRendering.typoscript"> <INCLUDE_TYPOSCRIPT: source="FILE:EXT:fluid_styled_content/Configuration/TypoScript/setup.typoscript">
8 sys_file
9 uid pid missing storage type metadata identifier identifier_hash folder_hash extension mime_type name sha1 size
10 1 0 0 1 2 0 /Files/FirstResult.png 29b827d0daa29658d8a0d952dfd20f559bbe3bcf 86d12d536195df2100a5ec04ab80c08f9bed3d31 png image/png FirstResult.png b13f2bbf275d592534eab659c1430c2702ce31fc 42383
11 sys_file_reference
12 uid pid uid_local uid_foreign tablenames fieldname sorting_foreign table_local
13 1 1 1 1 pages media 1 sys_file
14

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

View file

@ -43,6 +43,7 @@ class BasicsTest extends FunctionalTestCase
protected $pathsToLinkInTestInstance = [ protected $pathsToLinkInTestInstance = [
'typo3conf/ext/watchlist/Tests/Fixtures/Sites' => 'typo3conf/sites', 'typo3conf/ext/watchlist/Tests/Fixtures/Sites' => 'typo3conf/sites',
'typo3conf/ext/watchlist/Tests/Fixtures/Fileadmin/Files' => 'fileadmin/Files',
]; ];
protected function setUp(): void protected function setUp(): void
@ -85,7 +86,8 @@ class BasicsTest extends FunctionalTestCase
$request = $request->withHeader('Cookie', 'watchlist=page-1'); $request = $request->withHeader('Cookie', 'watchlist=page-1');
$result = $this->executeFrontendRequest($request); $result = $this->executeFrontendRequest($request);
self::assertStringContainsString('<li>Page Title</li>', $result->getBody()->__toString()); self::assertMatchesRegularExpression('#<li>\s*Page Title#', $result->getBody()->__toString());
self::assertStringContainsString('<img src="/fileadmin/Files/FirstResult.png" width="" height="" alt="" />', $result->getBody()->__toString());
} }
/** /**

View file

@ -63,7 +63,7 @@ class WatchlistTest extends TestCase
{ {
$subject = new Watchlist(); $subject = new Watchlist();
$thing = new Page(1, 'test'); $thing = new Page(1, 'test', null);
$subject->addItem($thing); $subject->addItem($thing);
self::assertCount(1, $subject->getItems()); self::assertCount(1, $subject->getItems());
@ -77,7 +77,7 @@ class WatchlistTest extends TestCase
{ {
$subject = new Watchlist(); $subject = new Watchlist();
$thing = new Page(1, 'test'); $thing = new Page(1, 'test', null);
$subject->addItem($thing); $subject->addItem($thing);
$subject->removeItem($thing); $subject->removeItem($thing);