Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
50 changes: 50 additions & 0 deletions Classes/Aspects/AssetUsageAspect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Mireo\RepeatableFields\Aspects;

use Mireo\RepeatableFields\Service\RepeatableAssetUsageHelper;
use Neos\ContentRepository\Domain\Model\NodeData;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Aop\JoinPointInterface;
use Neos\Media\Domain\Model\AssetInterface;

/**
* Aspect to extend asset usage detection to support repeatable fields
*
* @Flow\Scope("singleton")
* @Flow\Aspect
*/
class AssetUsageAspect
{
/**
* @Flow\Inject
* @var RepeatableAssetUsageHelper
*/
protected $repeatableAssetUsageHelper;

/**
* Extend the getRelatedNodes method to also find assets in repeatable fields
*
* @Flow\Around("method(Neos\Neos\Domain\Strategy\AssetUsageInNodePropertiesStrategy->getRelatedNodes())")
* @param JoinPointInterface $joinPoint
* @return array<NodeData>
*/
public function extendGetRelatedNodes(JoinPointInterface $joinPoint): array
{
$originalNodes = $joinPoint->getAdviceChain()->proceed($joinPoint);
/** @var AssetInterface $asset */
$asset = $joinPoint->getMethodArgument('asset');
$repeatableFieldNodes = $this->repeatableAssetUsageHelper->findNodesWithAssetInRepeatableFields($asset);

$allNodes = $originalNodes;
$existingIdentifiers = array_map(fn($node) => $node->getIdentifier(), $originalNodes);

foreach ($repeatableFieldNodes as $node) {
if (!in_array($node->getIdentifier(), $existingIdentifiers, true)) {
$allNodes[] = $node;
}
}

return $allNodes;
}
}
82 changes: 82 additions & 0 deletions Classes/Service/RepeatableAssetUsageHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
namespace Mireo\RepeatableFields\Service;

use Doctrine\ORM\EntityManagerInterface;
use Neos\ContentRepository\Domain\Model\NodeData;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Persistence\PersistenceManagerInterface;
use Neos\Media\Domain\Model\AssetInterface;
use Neos\Media\Domain\Model\Image;
use Neos\Neos\Domain\Service\SiteService;

/**
* Helper class for finding asset usage in repeatable fields
*
* @Flow\Scope("singleton")
*/
class RepeatableAssetUsageHelper
{
/**
* @Flow\Inject
* @var PersistenceManagerInterface
*/
protected $persistenceManager;

/**
* @Flow\Inject
* @var EntityManagerInterface
*/
protected $entityManager;

/**
* Find nodes that use the asset in repeatable field properties
*
* Repeatable fields store assets in two formats:
* - As objects: "__identity": "uuid" (images)
* - As plain strings: "uuid" (videos, PDFs, other assets)
*
* @param AssetInterface $asset
* @return array<NodeData>
*/
public function findNodesWithAssetInRepeatableFields(AssetInterface $asset): array
{
$assetIdentifier = $this->persistenceManager->getIdentifierByObject($asset);
$identifiers = [$assetIdentifier];

if ($asset instanceof Image) {
foreach ($asset->getVariants() as $variant) {
$identifiers[] = $this->persistenceManager->getIdentifierByObject($variant);
}
}

$queryBuilder = $this->entityManager->createQueryBuilder();
$queryBuilder->select('n')
->from(NodeData::class, 'n')
->where('n.path LIKE :pathPrefix');

$queryBuilder->setParameter('pathPrefix', SiteService::SITES_ROOT_PATH . '%');

$constraints = [];
$parameters = ['pathPrefix' => SiteService::SITES_ROOT_PATH . '%'];
$identifierIndex = 0;

foreach ($identifiers as $identifier) {
$lowerIdentifier = strtolower($identifier);

$constraints[] = '(LOWER(NEOSCR_TOSTRING(n.properties)) LIKE :identity' . $identifierIndex . ')';
$parameters['identity' . $identifierIndex] = '%"__identity": "' . $lowerIdentifier . '"%';
$identifierIndex++;

$constraints[] = '(LOWER(NEOSCR_TOSTRING(n.properties)) LIKE :plain' . $identifierIndex . ')';
$parameters['plain' . $identifierIndex] = '%": "' . $lowerIdentifier . '"%';
$identifierIndex++;
}

if (!empty($constraints)) {
$queryBuilder->andWhere(implode(' OR ', $constraints));
$queryBuilder->setParameters($parameters);
}

return $queryBuilder->getQuery()->getResult();
}
}