Project

General

Profile

CiviCRM cheatsheet » History » Version 7

Brienne Kordis, 10/12/2022 08:57 PM

1 1 Brienne Kordis
# CiviCRM cheatsheet
2
3
### Custom Entities & Extensions
4 6 Brienne Kordis
5 1 Brienne Kordis
   * A new Entity (think: Contact, Event) can be added to CiviCRM via an extension
6 3 Brienne Kordis
       * Use the `civix` [commands](https://docs.civicrm.org/dev/en/latest/extensions/civix/) to create the extension.
7 1 Brienne Kordis
   * To create core fields on the new extension, edit the XML file of the extension. Reference this [guide](https://docs.civicrm.org/dev/en/latest/framework/database/schema-definition/) for the options.
8
    * To add custom fields on the new extension, follow this [guide](https://docs.civicrm.org/dev/en/latest/step-by-step/create-entity/#121-making-our-entity-available-for-custom-data). It is also helpful to read about [managed entities](https://docs.civicrm.org/dev/en/latest/api/v4/managed/) before tackling custom fields.
9 3 Brienne Kordis
       * If you've already created custom fields/groups *not* on the custom entity, but on another entity that you would like to use in the extension, then you can export the CustomField from the APIv4 explorer and paste the results in a filed named `OptionValue_cg_extends_objects.mgd.php` within the *managed* folder of the extension (see detailed steps below).
10 1 Brienne Kordis
   * You can include configured Search Kit searches and Form Builder forms in your extension.
11 2 Brienne Kordis
       * _Search Kit_
12 1 Brienne Kordis
         * Create, configure, and save your search and any desired displays
13
         * Go to **Support > Developer > API Explorer v4**
14
         * Set the Entity to *Saved Search* and the Action to *export* (You will need the id of the saved search, so if unknown, do a *get* first)
15
         * Fill in the id of the saved search and click **Execute**
16
         * On the upper right corner of the results box, change *View as JSON* to *View as PHP*
17
         * Copy the results
18
         * Paste the results in a new file named `SavedSearch_name-of-saved-search.mgd.php` within the *managed* folder within the particular extension's folder. Note that you might need to create the *managed* folder as `civix` does not generate it automatically, but if you created custom fields as noted above, then you will have already added it. Also make sure that you add `<?php` to the top of the file!
19 2 Brienne Kordis
       * _Form Builder_
20 1 Brienne Kordis
         * Create, configure, and save your forms
21
         * In the root folder of your extension, create an *ang* folder if it does not yet exist
22
         * Move the files (both the html and json) related to your form(s) from their default location into the ang folder of your extension
23
             * The default location, such as for WordPress sites is `/wp-content/uploads/civicrm/ang`
24 7 Brienne Kordis
25
* See an [example of a custom entity](https://github.com/briennekordis/presentationEntity)
26 4 Brienne Kordis
27
### Adding JavaScript to CiviCRM
28
  1. Create an extension
29
  2. Figure out the exact page on which you want to add JS.
30 5 Brienne Kordis
        * add a `buildForm` hook within the myextensionname.php
31 4 Brienne Kordis
        * set a breakpoint on a simple statement (`$var = 1`) within the hook, turn on the debugger, and then load the page. The breakpoint will be hit and then you can get the name of the page in question by looking at the value of the `formName` variable with your debugging environment.
32
  3. Within the main extension file, write an `if` statement. The condition should limit on what page the JS is run. Adding a second condition to specify the form's id allows for future protection in case the formName is duplicated.
33
`$formName === 'Page Name'` OR `$formName === 'Page Name' && $form->id === 1`
34 5 Brienne Kordis
  4. Within the `if` block, add `Civi::resources()->addScriptFile('foo', 'bar.js');`
35
      * the first argument ('foo') is the name of the extension
36
      * the second argument ('bar.js') is the name of the file relative to the path of the extension
37 4 Brienne Kordis
  5. In a new JS file within the extension, add the JS / JQuery
38
      * For JQuery, add the code within the `$()` of the following code block:
39
```
40
CRM.$(function ($) {
41
  $();
42
});
43
```
44 5 Brienne Kordis
  6. For additional reference, see the [Resources Reference docs](https://docs.civicrm.org/dev/en/latest/framework/resources/)