Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Auto assign new assets to default collection and prevent removing last collection depending on config #251

Merged
merged 2 commits into from
Nov 1, 2024
Merged
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
9 changes: 6 additions & 3 deletions Classes/GraphQL/Resolver/Type/MutationResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,12 @@ public function uploadFile($_, array $variables): array
if ($assetCollectionId) {
/** @var AssetCollection $assetCollection */
$assetCollection = $this->assetCollectionRepository->findByIdentifier($assetCollectionId);
if ($assetCollection) {
$asset->setAssetCollections(new ArrayCollection([$assetCollection]));
}
} else {
// Assign the asset to the asset collection of the site it has been uploaded to
$assetCollection = $this->assetCollectionService->getDefaultCollectionForCurrentSite();
}
if ($assetCollection) {
$asset->setAssetCollections(new ArrayCollection([$assetCollection]));
}

$this->assetRepository->add($asset);
Expand Down
10 changes: 10 additions & 0 deletions Classes/GraphQL/Resolver/Type/QueryResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Flowpack\Media\Ui\GraphQL\Context\AssetSourceContext;
use Flowpack\Media\Ui\Infrastructure\Neos\Media\AssetProxyIteratorBuilder;
use Flowpack\Media\Ui\Service\AssetChangeLog;
use Flowpack\Media\Ui\Service\AssetCollectionService;
use Flowpack\Media\Ui\Service\SimilarityService;
use Flowpack\Media\Ui\Service\UsageDetailsService;
use Neos\Flow\Annotations as Flow;
Expand Down Expand Up @@ -114,6 +115,12 @@ class QueryResolver implements ResolverInterface
*/
protected $privilegeManager;

/**
* @Flow\Inject
* @var AssetCollectionService
*/
protected $assetCollectionService;

/**
* Returns total count of asset proxies in the given asset source
* @noinspection PhpUnusedParameterInspection
Expand Down Expand Up @@ -185,10 +192,13 @@ public function assetUsageCount($_, array $variables, AssetSourceContext $assetS
*/
public function config($_): array
{
$defaultAssetCollection = $this->assetCollectionService->getDefaultCollectionForCurrentSite();

return [
'uploadMaxFileSize' => $this->getMaximumFileUploadSize(),
'uploadMaxFileUploadLimit' => $this->getMaximumFileUploadLimit(),
'currentServerTime' => (new \DateTime())->format(DATE_W3C),
'defaultAssetCollectionId' => $defaultAssetCollection ? $this->persistenceManager->getIdentifierByObject($defaultAssetCollection) : null,
'canManageTags' => $this->privilegeManager->isPrivilegeTargetGranted('Flowpack.Media.Ui:ManageTags'),
'canManageAssetCollections' => $this->privilegeManager->isPrivilegeTargetGranted('Flowpack.Media.Ui:ManageAssetCollections'),
'canManageAssets' => $this->privilegeManager->isPrivilegeTargetGranted('Flowpack.Media.Ui:ManageAssets'),
Expand Down
22 changes: 22 additions & 0 deletions Classes/Service/AssetCollectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
use Doctrine\ORM\Query\ResultSetMapping;
use Flowpack\Media\Ui\Domain\Model\HierarchicalAssetCollectionInterface;
use Flowpack\Media\Ui\Utility\AssetCollectionUtility;
use Neos\ContentRepository\Domain\Service\ContextFactoryInterface;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
use Neos\Media\Domain\Model\AssetCollection;
use Neos\Media\Domain\Repository\AssetCollectionRepository;
use Neos\Neos\Domain\Service\ContentContext;

/**
* @Flow\Scope("singleton")
Expand All @@ -37,6 +40,12 @@ class AssetCollectionService
*/
protected $assetCollectionRepository;

/**
* @Flow\Inject
* @var ContextFactoryInterface
*/
protected $contextFactory;

/**
* Queries the asset count for all asset collections once and caches the result.
* This helps to avoid a lot of slow queries when rendering the asset collection list.
Expand Down Expand Up @@ -85,4 +94,17 @@ public function updatePathForNestedAssetCollections(HierarchicalAssetCollectionI
$this->updatePathForNestedAssetCollections($childCollection);
}
}

/**
* Returns the default asset collection for the current site if available
*/
public function getDefaultCollectionForCurrentSite(): ?AssetCollection
{
/** @var ContentContext $context */
$context = $this->contextFactory->create([
'workspaceName' => 'live',
]);

return $context->getCurrentSite()?->getAssetCollection();
}
}
1 change: 1 addition & 0 deletions Resources/Private/GraphQL/schema.root.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ type Config {
uploadMaxFileSize: FileSize!
uploadMaxFileUploadLimit: Int!
currentServerTime: DateTime!
defaultAssetCollectionId: AssetCollectionId
canManageAssetCollections: Boolean!
canManageTags: Boolean!
canManageAssets: Boolean!
Expand Down
2 changes: 2 additions & 0 deletions Resources/Private/JavaScript/core/src/hooks/useConfigQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface ConfigQueryResult {
uploadMaxFileSize: number;
uploadMaxFileUploadLimit: number;
currentServerTime: Date;
defaultAssetCollectionId: AssetCollectionId | null;
canManageAssetCollections: boolean;
canManageTags: boolean;
canManageAssets: boolean;
Expand All @@ -18,6 +19,7 @@ const DEFAULT_CONFIG: ConfigQueryResult = {
uploadMaxFileSize: 0,
uploadMaxFileUploadLimit: 0,
currentServerTime: new Date(),
defaultAssetCollectionId: null,
canManageAssetCollections: false,
canManageTags: false,
canManageAssets: false,
Expand Down
1 change: 1 addition & 0 deletions Resources/Private/JavaScript/core/src/queries/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const CONFIG = gql`
uploadMaxFileSize
uploadMaxFileUploadLimit
currentServerTime
defaultAssetCollectionId
canManageAssetCollections
canManageTags
canManageAssets
Expand Down
1 change: 1 addition & 0 deletions Resources/Private/JavaScript/dev-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
uploadMaxFileSize: 1024 * 1024,
uploadMaxFileUploadLimit: 2,
currentServerTime: new Date(),
defaultAssetCollectionId: null,
canManageAssetCollections: true,
canManageTags: true,
canManageAssets: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useRecoilValue } from 'recoil';
import { Headline, MultiSelectBox, SelectBox } from '@neos-project/react-ui-components';

import { useIntl, useNotify, useMediaUi } from '@media-ui/core';
import { useSelectedAsset, useSetAssetCollections } from '@media-ui/core/src/hooks';
import { useConfigQuery, useSelectedAsset, useSetAssetCollections } from '@media-ui/core/src/hooks';
import { IconLabel } from '@media-ui/core/src/components';
import { featureFlagsState } from '@media-ui/core/src/state';
import { collectionPath, useAssetCollectionsQuery } from '@media-ui/feature-asset-collections';
Expand All @@ -19,6 +19,7 @@ const collectionsMatchAsset = (assetCollectionIds: string[], asset: Asset) => {
const CollectionSelectBox: React.FC = () => {
const Notify = useNotify();
const { translate } = useIntl();
const { config } = useConfigQuery();
const {
approvalAttainmentStrategy: { obtainApprovalToSetAssetCollections },
} = useMediaUi();
Expand Down Expand Up @@ -136,7 +137,7 @@ const CollectionSelectBox: React.FC = () => {
onSearchTermChange={handleSearchTermChange}
ListPreviewElement={AssetCollectionOptionPreviewElement}
displaySearchBox
allowEmpty
allowEmpty={false}
threshold={0}
/>
</>
Expand All @@ -158,7 +159,7 @@ const CollectionSelectBox: React.FC = () => {
onSearchTermChange={handleSearchTermChange}
ListPreviewElement={AssetCollectionOptionPreviewElement}
displaySearchBox
allowEmpty
allowEmpty={!config.defaultAssetCollectionId}
threshold={0}
/>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Headline, SelectBox } from '@neos-project/react-ui-components';

import { useIntl, useMediaUi, useNotify } from '@media-ui/core';
import { IconLabel } from '@media-ui/core/src/components';
import { useConfigQuery } from '@media-ui/core/src/hooks';
import {
collectionPath,
useAssetCollectionsQuery,
Expand All @@ -18,7 +17,6 @@ import * as classes from './ParentCollectionSelectBox.module.css';

const ParentCollectionSelectBox = () => {
const Notify = useNotify();
const { config } = useConfigQuery();
const { translate } = useIntl();
const { approvalAttainmentStrategy } = useMediaUi();
const { assetCollections } = useAssetCollectionsQuery();
Expand Down Expand Up @@ -98,7 +96,7 @@ const ParentCollectionSelectBox = () => {
</Headline>
<SelectBox
className={classes.collectionSelectBox}
disabled={!config.canManageAssetCollections || loading}
disabled={loading || filteredSelectBoxOptions.length === 0}
placeholder={translate('inspector.collections.placeholder', 'Select a collection')}
value={selectedAssetCollection.parent?.id}
optionValueField="id"
Expand Down
Loading