Skip to content

Add a Saved Search in Your Own Extension

Introduction

You can add a saved search created with Search Kit in your own extension. If you for example created a saved search like the Soft Credit Example and want to include it in your extension to provide an overview of soft credits and related contributions.

You need 2 simple steps to accomplish this

  1. Create the saved search and display(s).
  2. Export them to a .mgd.php file in your extension.

Automatic Export (civix)

The 2nd step can be most easily done via the civix export command.

Manual Export

If civix is not available, you can manually export from the overview of Saved Searches or directly in the API4 Explorer. This guide details the Saved Search overview and will explain how you to do this directly in the API4 Explorer afterwards.

Once you are on the list of saved searches you can click on the action menu behind each row and select the export option: Saved Searches export

If you select this option you will get the option to save the search to the clipboard but also to package for distribution: Package for distribution using the API4 Explorer Export.

If you click on the link you will be presented with a prefilled API Explorer for your Saved Search: Prefilled API Explorer.

Now press the Execute button and copy the results array, then save this results array into the managed folder in your extension (using the copy button is a good idea). You'll also need to add use CRM_Yourextension_ExtensionUtil as E; to the top of the file (not shown here).

Result into Managed

If you now install your extension in a new environment your defined search will appear in the Saved Search list under the heading Packaged Searches. You'll need to ensure that you have the mgd-php in your mixins in info.xml for your Saved Search to show up, which you can do by running civix mixin --enable=mgd-php@1.