Skip to content

Commit

Permalink
Merge pull request #4303 from neos/bugfix/4300-allow-disabling-of-aut…
Browse files Browse the repository at this point in the history
…ocreated-image-variants

BUGFIX: Allow disabling of auto-created Image Variants
  • Loading branch information
mhsdesign authored Nov 1, 2023
2 parents 1e26367 + 5cc5f89 commit 28d568d
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 6 deletions.
33 changes: 33 additions & 0 deletions Neos.Media.Browser/Classes/Controller/AssetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
use Neos\Media\Domain\Repository\AssetRepository;
use Neos\Media\Domain\Repository\TagRepository;
use Neos\Media\Domain\Service\AssetService;
use Neos\Media\Domain\Service\AssetVariantGenerator;
use Neos\Media\Exception\AssetServiceException;
use Neos\Media\TypeConverter\AssetInterfaceConverter;
use Neos\Neos\Controller\BackendUserTranslationTrait;
Expand Down Expand Up @@ -140,6 +141,12 @@ class AssetController extends ActionController
*/
protected $assetSourceService;

/**
* @Flow\Inject
* @var AssetVariantGenerator
*/
protected $assetVariantGenerator;

/**
* @var AssetSourceInterface[]
*/
Expand Down Expand Up @@ -468,6 +475,32 @@ public function variantsAction(string $assetSourceIdentifier, string $assetProxy
}
}

/**
* Create missing variants for the given image
*
* @param string $assetSourceIdentifier
* @param string $assetProxyIdentifier
* @param string $overviewAction
* @throws StopActionException
* @throws UnsupportedRequestTypeException
*/
public function createVariantsAction(string $assetSourceIdentifier, string $assetProxyIdentifier, string $overviewAction): void
{
$assetSource = $this->assetSources[$assetSourceIdentifier];
$assetProxyRepository = $assetSource->getAssetProxyRepository();

$assetProxy = $assetProxyRepository->getAssetProxy($assetProxyIdentifier);
$asset = $this->persistenceManager->getObjectByIdentifier($assetProxy->getLocalAssetIdentifier(), Asset::class);

/** @var VariantSupportInterface $originalAsset */
$originalAsset = ($asset instanceof AssetVariantInterface ? $asset->getOriginalAsset() : $asset);

$this->assetVariantGenerator->createVariants($originalAsset);
$this->assetRepository->update($originalAsset);

$this->redirect('variants', null, null, ['assetSourceIdentifier' => $assetSourceIdentifier, 'assetProxyIdentifier' => $assetProxyIdentifier, 'overviewAction' => $overviewAction]);
}

/**
* @return void
* @throws NoSuchArgumentException
Expand Down
2 changes: 1 addition & 1 deletion Neos.Media.Browser/Configuration/Policy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ privilegeTargets:

'Neos.Media.Browser:ManageAssets':
label: Allowed to manage assets
matcher: 'method(Neos\Media\Browser\Controller\(Asset|Image)Controller->(index|new|show|edit|update|initializeCreate|create|replaceAssetResource|updateAssetResource|initializeUpload|upload|tagAsset|delete|createTag|editTag|updateTag|deleteTag|addAssetToCollection|relatedNodes|variants)Action()) || method(Neos\Media\Browser\Controller\ImageVariantController->(update)Action())'
matcher: 'method(Neos\Media\Browser\Controller\(Asset|Image)Controller->(index|new|show|edit|update|initializeCreate|create|replaceAssetResource|updateAssetResource|initializeUpload|upload|tagAsset|delete|createTag|editTag|updateTag|deleteTag|addAssetToCollection|relatedNodes|variants|createVariants)Action()) || method(Neos\Media\Browser\Controller\ImageVariantController->(update)Action())'

'Neos.Media.Browser:AssetUsage':
label: Allowed to calculate asset usages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
<f:form action="update" controller="ImageVariant" package="Neos.Media.Browser" format="json" id="postHelper" method="post" useParentRequest="{isSubRequest}">
<f:render partial="ConstraintsHiddenFields" arguments="{constraints: constraints}" />
</f:form>

<div class="neos-footer">
<f:form action="createVariants" addQueryString="true">
<button type="submit" class="neos-button">{neos:backend.translate(id: 'createMissingVariants', package: 'Neos.Media.Browser')}</button>
</f:form>
</div>

</f:section>

<f:section name="Scripts">
Expand Down
3 changes: 3 additions & 0 deletions Neos.Media.Browser/Resources/Private/Translations/en/Main.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,9 @@
<trans-unit id="missingDocumentNode" xml:space="preserve">
<source>No document node found for this node</source>
</trans-unit>
<trans-unit id="createMissingVariants" xml:space="preserve">
<source>Create missing variants</source>
</trans-unit>
</body>
</file>
</xliff>
5 changes: 5 additions & 0 deletions Neos.Media/Classes/Domain/Service/AssetVariantGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ public function createVariant(AssetInterface $asset, string $presetIdentifier, s
$createdVariant = null;
$preset = $this->getVariantPresets()[$presetIdentifier] ?? null;
if ($preset instanceof VariantPreset && $preset->matchesMediaType($asset->getMediaType())) {
$existingVariant = $asset->getVariant($presetIdentifier, $variantIdentifier);
if ($existingVariant !== null) {
return $existingVariant;
}

$variantConfiguration = $preset->variants()[$variantIdentifier] ?? null;

if ($variantConfiguration instanceof Configuration\Variant) {
Expand Down
9 changes: 8 additions & 1 deletion Neos.Media/Classes/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
* source code.
*/

use Neos\Flow\Configuration\ConfigurationManager;
use Neos\Flow\Core\Bootstrap;
use Neos\Flow\Package\Package as BasePackage;
use Neos\Media\Domain\Model\AssetInterface;
use Neos\Media\Domain\Model\ImportedAssetManager;
use Neos\Media\Domain\Service\AssetService;
use Neos\Media\Domain\Service\AssetVariantGenerator;
Expand All @@ -30,7 +32,12 @@ class Package extends BasePackage
public function boot(Bootstrap $bootstrap)
{
$dispatcher = $bootstrap->getSignalSlotDispatcher();
$dispatcher->connect(AssetService::class, 'assetCreated', AssetVariantGenerator::class, 'createVariants');
$dispatcher->connect(AssetService::class, 'assetCreated', function (AssetInterface $asset) use ($bootstrap) {
$configurationManager = $bootstrap->getObjectManager()->get(ConfigurationManager::class);
if ($configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, 'Neos.Media.autoCreateImageVariantPresets')) {
$bootstrap->getObjectManager()->get(AssetVariantGenerator::class)->createVariants($asset);
}
});
$dispatcher->connect(AssetService::class, 'assetCreated', ThumbnailGenerator::class, 'createThumbnails');
$dispatcher->connect(AssetService::class, 'assetCreated', ImportedAssetManager::class, 'registerCreatedAsset');
$dispatcher->connect(AssetService::class, 'assetRemoved', ImportedAssetManager::class, 'registerRemovedAsset');
Expand Down
2 changes: 1 addition & 1 deletion Neos.Media/Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Neos:
# Variant presets
variantPresets: []
# Automatically create asset variants for configured presets when assets are added
autoCreateImageVariantPresets: false
autoCreateImageVariantPresets: true

thumbnailGenerators:

Expand Down
6 changes: 3 additions & 3 deletions Neos.Media/Documentation/VariantPresets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ The following example shows the required structure and possible fields of the pr
options:
aspectRatio: '1:1'
The automatic variant generation for new assets has to be enabled via setting as
by default this feature is disabled.
The automatic variant generation for new assets is active by default.
It can be disabled via setting:

.. code-block:: yaml
Neos:
Media:
autoCreateImageVariantPresets: true
autoCreateImageVariantPresets: false
To show and edit the variants in the media module the variants tab has to be enabled.

Expand Down

0 comments on commit 28d568d

Please sign in to comment.