Addin custom button with content id in edit page

System Information
  • Strapi Version: 4.13.2
  • Operating System: Arch Linux
  • Database: Postgres 15
  • Node Version: 18.17
  • Yarn Version: 3.6.3

Hi everyone!

I’m adding a clone-like functionality to one of my content types. To allow users to trigger it, I am trying to add a button on those “edit” pages that call a custom route I already have created. So far, I have managed to add a custom button to all edit pages, but I am missing two bits of info to complete my feature:

  • I am adding said custom button using the injectContentManagerComponent method, which adds the button on all content types. Is there a way to limit the injection to a specific content type only?
  • When clicked, my button (which lives on the edit page) should call a POST /my-content-type/:id/clone-like-action. How can I get the id of the content I’m currently editing?

Thanks in advance!

Hey,

You can achieve that using the useCMEditViewDataManager() method which is provided by strapi under your src/plugins/pluginName/admin/src/components/folder/index.js. You can add it to your mentioned file.

This will help you in fetching the Edit record detail and UID of current previewing collection. Below will answer both of your question.

import { useCMEditViewDataManager } from '@strapi/helper-plugin'; // FOR IMPORT

--------------------------
const { allLayoutData, modifiedData } = useCMEditViewDataManager();
const { uid } = allLayoutData.contentType;

// CHECK IF UID CONTROLLER IS ALLOWED OR NOT
const allowedUID = ['api::blog.blog','api::page.page'];
if(isCreatingEntry || !allowedUID.includes(uid)) {
  return null;
}

console.log(modifiedData) // FETCH YOU RECORD DETAIL

// YOUR CODE BLOCK
return (
 <>
   <Button />
 </>
)

Hope this helps you :slight_smile:

Thanks

Hey, this does indeed help! Thank you for taking the time to help a random person on the internet!

1 Like

I am glad that this worked out for you and will help others too.

Happy to help :slight_smile:

Thanks.