Skip to content

Instantly share code, notes, and snippets.

@Sebobo
Last active May 15, 2019 07:49
Show Gist options
  • Save Sebobo/212f41e10c2fd80761e83ec40ac52eeb to your computer and use it in GitHub Desktop.
Save Sebobo/212f41e10c2fd80761e83ec40ac52eeb to your computer and use it in GitHub Desktop.
Use asset title as filename in Neos CMS

Add "cocur/slugify" to your composer.json

Run flow resource:publish after applying this change.

<?php
namespace My\Package\ResourceManagement\Target;
use Neos\Flow\Annotations as Flow;
use Cocur\Slugify\Slugify;
use Neos\Flow\ResourceManagement\ResourceMetaDataInterface;
use Neos\Media\Domain\Repository\AssetRepository;
/**
* A target which publishes resources by creating symlinks with the filename based on the files title.
*/
class FileSystemSymlinkTarget extends \Neos\Flow\ResourceManagement\Target\FileSystemSymlinkTarget
{
/**
* @Flow\Inject
* @var AssetRepository
*/
protected $assetRepository;
/**
* @inheritDoc
*/
protected function getRelativePublicationPathAndFilename(ResourceMetaDataInterface $object)
{
$filename = $object->getFilename();
$asset = $this->assetRepository->findOneByResourceSha1($object->getSha1());
if ($asset) {
$slugify = new Slugify();
if (!empty($asset->getTitle())) {
$filename = $slugify->slugify($asset->getTitle()) . '.' . $asset->getFileExtension();
}
}
if ($object->getRelativePublicationPath() !== '') {
$pathAndFilename = $object->getRelativePublicationPath() . $filename;
} else {
if ($this->subdivideHashPathSegment) {
$sha1Hash = $object->getSha1();
$pathAndFilename = $sha1Hash[0] . '/' . $sha1Hash[1] . '/' . $sha1Hash[2] . '/' . $sha1Hash[3] . '/' . $sha1Hash . '/' . $filename;
} else {
$pathAndFilename = $object->getSha1() . '/' . $filename;
}
}
return $pathAndFilename;
}
}
Neos
Flow:
resource:
targets:
localWebDirectoryPersistentResourcesTarget:
target: 'My\Package\ResourceManagement\Target\FileSystemSymlinkTarget'
@Sebobo
Copy link
Author

Sebobo commented May 15, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment