Conditional check before deleting media if it's in use #558
Replies: 2 comments 4 replies
-
Hey @AkashReddy-playy — this would be a great idea for a plugin. It could be applied to any relationship or upload field. It could dynamically build up a list of relationship / upload fields per collection, and then search for documents that have relationships to the document being deleted. This would all happen in a If you decide to build this, keep us posted—and we'll do the same! |
Beta Was this translation helpful? Give feedback.
-
I have code for a similar example, but it may be of limited use if you have a lot of media in hard to query places on many relationships. Instead of checking on delete, this is a way to show a count and link to the list with filters set where a relationship is used in another collection. In this example there are // post category field
{
name: 'category',
type: 'relationship',
relationTo: 'categories',
hasMany: true,
}, On the categories we have a UI field with a custom component that queries posts that have the relationship. The UI field is: // post summary field:
{
name: 'summary',
type: 'ui',
admin: {
position: 'sidebar',
components: {
Field: CategorySummary,
}
}
} Now the CategorySummary component: import React, { useEffect, useState } from 'react';
import qs from 'qs';
import { useDocumentInfo } from 'payload/dist/admin/components/utilities/DocumentInfo';
const get = (url: string, params: unknown = {}): Promise<Response> => {
const query = qs.stringify(params, { addQueryPrefix: true });
return fetch(`${url}${query}`);
};
/**
* A custom UI component for the category to display count of posts and add links
* @constructor
*/
const CategorySummary: React.FC = () => {
// access the id of a saved document from payload
const { id } = useDocumentInfo();
// getters and setters for component state variables
const [isLoading, setIsLoading] = useState(true);
const [postCount, setPostCount] = useState(null);
const [error, setError] = useState(false);
// useEffect adds a hook so that when the id is set the function is called
useEffect(() => {
if (id) {
const queryRelations = async () => {
const request = await get('/api/posts', {
where: {
// the 'in' operator is used when relations can be more than one
// on relationships that are not a `hasMany` you would use 'equals' instead of 'in'
category: { in: id },
// to add more query constraints use 'or', 'and' operator objects
},
depth: 0,
limit: 0,
});
const result = await request.json();
if (result?.docs) {
setPostCount(result?.totalDocs);
}
if (result.status >= 400) {
setError(true);
}
setIsLoading(false);
};
const ignoreResult = queryRelations();
}
}, [id]);
if (!id) {
return null;
}
if (error) {
return (<span>There was a problem fetching data</span>);
}
return (
<div>
<h4>
Summary
</h4>
<p>
{isLoading ? (
loading...
) : (
{/*
link to /posts is formatted so that it will populate the filters inputs on the listing
on relationships that are not a `hasMany` you would use [equals] instead of [in][0]
*/}
<a href={`/admin/collections/posts?where[or][0][and][0][category][in][0]=${id}`} >
{postCount}
{' '}
Posts
</a>
)}
</p>
</div>
);
};
export default CategorySummary; For your use, this might be more difficult if relationships are more complicated, such as:
|
Beta Was this translation helpful? Give feedback.
-
Hello,
Currently is it possible to check if media is in use in any of the collections?
This will be a great feature to have with an indicator to show relative links where media is used.
Any solutions or approaches to be considered would be great.
Ak
Beta Was this translation helpful? Give feedback.
All reactions