Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions Neos.Media/Classes/Eel/AssetsHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

declare(strict_types=1);

namespace Neos\Media\Eel;

/*
* This file is part of the Neos.Media package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use Neos\Eel\ProtectedContextAwareInterface;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Persistence\Exception\InvalidQueryException;
use Neos\Flow\Persistence\QueryResultInterface;
use Neos\Media\Domain\Model\AssetCollection;
use Neos\Media\Domain\Model\AssetInterface;
use Neos\Media\Domain\Model\Tag;
use Neos\Media\Domain\Repository\AssetCollectionRepository;
use Neos\Media\Domain\Repository\AssetRepository;
use Neos\Media\Domain\Repository\TagRepository;

/**
* This is a helper for accessing assets from the media library
*
* @api
*/
class AssetsHelper implements ProtectedContextAwareInterface
{
/**
* @Flow\Inject
* @var AssetRepository
*/
protected $assetRepository;

/**
* @Flow\Inject
* @var TagRepository
*/
protected $tagRepository;

/**
* @Flow\Inject
* @var AssetCollectionRepository
*/
protected $assetCollectionRepository;

/**
* @return QueryResultInterface<AssetInterface> | null
*/
public function findByTag(?Tag $tag): ?QueryResultInterface
{
if (!$tag) {
return null;
}
return $this->assetRepository->findByTag($tag);
}

/**
* @return QueryResultInterface<AssetInterface> | null
*/
public function findByTagLabel(?string $tagLabel): ?QueryResultInterface
{
if (!$tagLabel) {
return null;
}
$tag = $this->tagRepository->findOneByLabel($tagLabel);
return $this->findByTag($tag);
}

/**
* @return QueryResultInterface<AssetInterface> | null
*/
public function findByCollection(?AssetCollection $collection): ?QueryResultInterface
{
if (!$collection) {
return null;
}
return $this->assetRepository->findByAssetCollection($collection);
}

/**
* @return QueryResultInterface<AssetInterface> | null
*/
public function findByCollectionTitle(?string $collectionTitle): ?QueryResultInterface
{
if (!$collectionTitle) {
return null;
}
$collection = $this->assetCollectionRepository->findOneByTitle($collectionTitle);
return $this->findByCollection($collection);
}

/**
* @param Tag[] $tags
* @return QueryResultInterface<AssetInterface> | null
*/
public function search(string $searchTerm, array $tags = [], AssetCollection $collection = null): ?QueryResultInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this one it makes a lot of sense, too, IMO:

Suggested change
/**
* @param Tag[] $tags
* @return QueryResultInterface<AssetInterface> | null
*/
public function search(string $searchTerm, array $tags = [], AssetCollection $collection = null): ?QueryResultInterface
/**
* @param Tag[]|string[] $tags
* @return QueryResultInterface<AssetInterface> | null
*/
public function search(?string $searchTerm, array $tags = [], AssetCollection|string $collection = null): ?QueryResultInterface

{
if (!$searchTerm) {
return null;
}

try {
return $this->assetRepository->findBySearchTermOrTags($searchTerm, $tags, $collection);
} catch (InvalidQueryException) {
}

return null;
}

/**
* @param string $methodName
*/
public function allowsCallOfMethod($methodName): bool
{
return true;
}
}
4 changes: 4 additions & 0 deletions Neos.Media/Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,7 @@ Neos:
options:
namespaces:
neos.media: Neos\Media\ViewHelpers

Fusion:
defaultContext:
Neos.Media.Assets: 'Neos\Media\Eel\AssetsHelper'
54 changes: 54 additions & 0 deletions Neos.Neos/Documentation/References/EelHelpersReference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,60 @@ Render a human-readable description for the passed $dimensions






.. _`Eel Helpers Reference: Neos.Media.Assets`:

Neos.Media.Assets
-----------------

Implemented in: ``Neos\Media\Eel\AssetsHelper``

Neos.Media.Assets.findByTag(tag)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

* ``tag`` (Neos\Media\Domain\Model\Tag) A ``Tag`` instance

**Return** (null|QueryResultInterface<AssetInterface>)

Neos.Media.Assets.findByTagLabel(label)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

* ``label`` (string) The label of a tag

**Return** (null|QueryResultInterface<AssetInterface>)

Neos.Media.Assets.findByCollection(collection)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

* ``collection`` (Neos\Media\Domain\Model\AssetCollection) An ``AssetCollection`` instance

**Return** (null|QueryResultInterface<AssetInterface>)

Neos.Media.Assets.findByCollectionTitle(title)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

* ``title`` (string) The title of a collection

**Return** (null|QueryResultInterface<AssetInterface>)

Neos.Media.Assets.search(searchTerm, tags, collection)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

* ``searchTerm`` (string) The search term to look for in the title, filename and caption of assets
* ``tags`` (Neos\Media\Domain\Model\Tag[], optional) A list of ``Tag`` instances
* ``collection`` (Neos\Media\Domain\Model\AssetCollection, optional) An ``AssetCollection`` instance

**Return** (null|QueryResultInterface<AssetInterface>)








.. _`Eel Helpers Reference: Neos.Seo.Image`:

Neos.Seo.Image
Expand Down