edit

Civix

Introduction

The civix command-line tool is the community-endorsed method for building your CiviCRM extensions.

Follow the installation instructions in the GitHub repository.

After fulfilling the Pre-Requisites, you can verify that all your configuration is correct by running the following command from within your extensions directory with:

civix civicrm:ping

This command should reply with "Ping successful". If the ping is unsuccessful, re-read the civix README.md and do the post-installation configuration.

It is also useful to examine the output of the civix help command to read about what civix can do for you.

Help is available on individual commands, e.g.,:

civix help civicrm:ping

Generate a skeletal extension

To generate a skeletal extension module, we will use civix generate:module and pass in the name for our extension. See here for details of naming conventions.

Start with:

cd $YOUR_EXTENSIONS_DIR
civix generate:module --help

Then use a command like this:

civix generate:module com.example.myextension --license=AGPL-3.0

This command will report that it has created three files, following the standard extension structure.

The command attempts to auto-detect authorship information (your name and email address) by reading your git configuration. If this fails or is otherwise incorrect, then you may pass explicit values with --author and --email.

You can now update your info.xml. This file initially contains some examples and placeholders which you need to fix. You can edit most of these fields intuitively. If you need detailed specifications, see Extension Reference.

Now that you have created your extension, you need to activate it by navigating to:

Administer » System Settings » Manage Extensions

or

Administer » Customize Data and Screens » Manage Extensions.

For more detailed instructions, see Extensions.

Add features

There are many different features that you can add to a module-extension at your discretion. A few possibilities:

Add a basic web page

CiviCRM uses a typical web-MVC architecture. To implement a basic web page, you must create a PHP controller class, create a Smarty template file, and create a routing rule. You can create the appropriate files by calling civix generate:page.

Once again you can review the output of this command to see the available options:

civix generate:page --help

Generally you will only need to supply the PHP class name and web-path, for example:

civix generate:page MyPage civicrm/my-page

This creates three files:

  • xml/Menu/myextension.xml defines request-routing rules and associates the controller ("CRM_Myextension_Page_Greeter") with the web path civicrm/my-page.
  • CRM/Myextension/Page/MyPage.php is the controller which coordinates any parsing, validation, business-logic, or database operations.
  • templates/CRM/Myextension/Page/MyPage.tpl is loaded automatically after the controller executes. It defines the markup that is eventually displayed. For more information on the syntax of this file, see the smarty guide.

The auto-generated code for the controller and view demonstrate a few basic operations, such as passing data from the controller to the view.

Note

After adding or modifying a route in the XML file, you must reset CiviCRMs "menu cache". Do this in a web browser by visiting /civicrm/menu/rebuild?reset=1 or, if using Drupal & Drush, by running drush cc civicrm.

Edit In Place

If the data on the page is read and updated through the API, then you may want to consider using the in-place editing API.

Add a basic web form

Caution

The form system is not well documented and may undergo significant revision after the CiviCRM 4.x series. In general, migrating basic pages will be easier than migrating basic forms, so you may want to consider building your data-input interface using basic pages, the AJAX API, and the in-place editing API.

CiviCRM uses a typical web-MVC architecture. To implement a basic web form, you must create a PHP controller class, create a Smarty template file, and create a routing rule. You can create the appropriate files by calling civix generate:form.

The form generation command has similar arguments to civix generate:page, requiring a class name and a web route:

civix generate:form FavoriteColor civicrm/favcolor

This creates three files:

  • xml/Menu/myextension.xml defines request-routing rules and associates the controller CRM_Myextension_Form_FavoriteColor with the web path civicrm/favcolor.
  • CRM/Myextension/Form/FavoriteColor.php is the controller which coordinates any parsing, validation, business-logic, or database operations. For more details on how this class works, see QuickForm Reference.
  • templates/CRM/Myextension/Form/FavoriteColor.tpl is loaded automatically after the controller executes. It defines the markup that is eventually displayed. For more information on the syntax of this file, see the smarty guide.

The auto-generated code for the controller and view demonstrate a few basic operations, such as adding a <select> element to the form.

Note

After adding or modifying a route in the XML file, you must reset CiviCRMs "menu cache". This can be done in a web browser by visiting /civicrm/menu/rebuild?reset=1.

Add a new entity

If you want your extension to store data in the database, then you will need to create a new entity.

  1. Pick a name for your entity.

    • In some places, CiviCRM expects a CamelCaseName, in others, an snake_case_name. Be absolutely consistent in your naming, because CiviCRM needs to translate between those two naming conventions.

    • Also consider that all entity names (including yours) should be unique across all core entities as well as all extension entities (for all installed extensions). Thus in many cases it's best to prefix your entity name with the short name of your extension.

    • For the remainder of this tutorial, we will use MyEntity as the name of the entity.

  2. Generate the skeletal files

    $ civix generate:entity MyEntity
    

    Make sure to use CamelCase here.

    This creates a skeletal file for your XML schema, your BAO, and your API.

  3. Edit the XML schema definitions that you just generated (in the xml folder). Define your desired fields.

  4. Generate your DAO and SQL files.

    $ civix generate:entity-boilerplate
    

    You can safely re-run this command after you make changes to your XML schema definition. But if your schema changes require database migrations for existing installations, then you'll need to write a migration manually in addition to re-generating your boilerplate.

  5. Generate a database upgrader.

    $ civix generate:upgrader
    

    Even though you're not yet creating any upgrades for your extension, you need to do this step now so that CiviCRM will pick up auto_install.sql and auto_uninstall.sql later on.

  6. Re-install your extension.

    $ cv ext:uninstall org.example.myextension
    $ cv ext:enable org.example.myextension
    

Now your entity should be ready to use. Try it out with cv api MyEntity.create and cv api MyEntity.get. Then add some tests.

Troubleshooting

If you've generated an entity within an extension that you created with civix v18.01.0 or earlier, then you'll need to add this hook to your myextension.php file (changing myextension to your extension's short name).

/**
 * Implements hook_civicrm_entityTypes().
 *
 * Declare entity types provided by this module.
 *
 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_entityTypes
 */
function myextension_civicrm_entityTypes(&$entityTypes) {
  _myextension_civix_civicrm_entityTypes($entityTypes);
}

(Starting from civix v18.02.0, this hook is automatically added when you generate a new extension.)

Add a database upgrader, installer and uninstaller

If your module requires creating or maintaining SQL tables, then you should create a class for managing database upgrades. The upgrader adds a class for managing installs and upgrades but you then need to edit the file comment out the various upgrade and uninstall functions to make it work. Generally your install script belongs in an sql/ folder in the root of your extension with a name like 'install'.

This civix command does not require arguments:

civix generate:upgrader

This creates two files and one directory:

  • CRM/Myextension/Upgrader.php stores a series of upgrade functions based on a function naming pattern similar to Drupal's hook_update_N. After examining the file's comments for example upgrade functions you can then write your own.
  • CRM/Myextension/Upgrader/Base.php contains helper functions and adapters which make it easier to write the upgrader. This file may be overwritten from time-to-time to provide new helpers or adapters.
  • sql

After reviewing the examples and creating your own upgrade functions, you can execute the upgrades through the web interface by visiting the "Manage Extensions" screen. This screen will display an alert with an action-link to perform the upgrades.

Note

The "upgrader" class is a wrapper for hook_civicrm_upgrade which aims to be easy-to-use for developers with Drupal experience. If you need to organize the upgrade logic differently, then consider providing your own implementation of hook_civicrm_upgrade.

Caution

Only use the upgrade system to manage new SQL tables. Do not manipulate core schema.

If you need to create triggers on core SQL tables, use hook_civicrm_triggerInfo. This allows your triggers to coexist with triggers from other modules.

Add a case type

(from CiviCRM v4.4+)

If you want to develop a custom case-type for CiviCase, then you can generate a skeletal CiviCase XML file.

Once again civix will give the details of options and arguments with this command:

civix help generate:case-type

This reports that civix expects a label argument and an optional name:

civix generate:case-type "Volunteer Training" Training

This creates two files:

  • xml/case/Training.xml defines the roles, activity types, and timelines associated with the new case type. For more in depth discussion of CiviCase XML, see CiviCase Configuration.
  • alltypes.civix.php(which may already exist) defines implementations of various hooks (notably hook_civicrm_caseTypes).

Add custom fields

(from CiviCRM v4.4+)

Your extension can create one or more sets of custom fields at installation. There are two methods depending on whether the custom data set extends an entity (e.g. "Individual" – without any specific subtype) or extends a specific subtype of an entity, (e.g. Activities of type 'Volunteer').

Extending a base entity

This is the simplest scenario. Start by creating a custom fields using the web interface and then export them for use with the extension:

Note

Before you begin verify that civix is connected to your instance of CiviCRM by running civix civicrm:ping.

  • On your development instance of CiviCRM, create the new custom fields using the web interface. Note the unique ID of the custom data group (also known as the "Custom Fieldset", "CustomGroup" or "civicrm_custom_group").
  • Create an XML file with civix generate:custom-xml specifying the custom-data group ID.
  • Create an upgrader file with civix generate:upgrader to load the XML file during installation.

Check the full range of options with the civix help command:

civix help generate:custom-xml

So to created your XML for a custom data group ID of 7:

civix generate:custom-xml --data=7

Most of the CiviHR modules rely on the first approach (e.g.: Upgrader/Base.php and auto_install.xml).

Extending a subtype

The automatic export does not work too well when the custom-data group extends a specific subtype. The "HR Emergency Contact" extension provides and example that creates a custom-data group that describes Relationships with type "Emergency Contact". Internally, Civi uses "relationship type IDs", but those are not portable. As a quick work-around, this extension uses Smarty: (HREmerg/Upgrader.php and hremerg-customdata.xml.tpl).

To create this extension, the author used civix generate:custom-xml and then:

  1. Renamed the xml/auto_install.xml to templates/hremerg-customdata.xml.tpl
  2. Changed the value of <extends_entity_column_value> in the .tpl file using a variable instead of a hard-coded type ID.
  3. Added logic in the upgrader to create the relationship type.
  4. Added logic in the upgrader to evaluate the Smarty template.

Accessing Custom Fields

For setting and getting custom field values in hooks, you need to know the field ID of the custom field(s) you want to work with. You'll then access these fields as "custom_". So if you have a field holding a custom value whose ID in the civicrm_custom_field table is 34, you'll use "custom_34" to access it.

To get a custom field ID given the custom field name and custom group name, you can use the following code:

require_once 'CRM/Core/BAO/CustomField.php';
$customFieldID = CRM_Core_BAO_CustomField::getCustomFieldID($field, $group);

Once you have the ID(s), you'll want to use the setValues and getValues functions in the CRM/Core/BAO/CustomValueTable.php file. Here are a couple of examples of their use:

Setting values:

require_once 'CRM/Core/BAO/CustomValueTable.php';
$params = array('entityID' => $contribution_id, 'custom_34' => 'new val');
CRM_Core_BAO_CustomValueTable::setValues($params);

Getting values:

$params = array( 'entityID' => 1327, 'custom_13' => 1, 'custom_43' => 1);
require_once 'CRM/Core/BAO/CustomValueTable.php';
$values = CRM_Core_BAO_CustomValueTable::getValues($params);

Caution

Note that custom field values may not always be available when you might expect. For instance, you can't retrieve custom field values in the 'create' operation in the _pre and _post hooks, because the custom field values haven't been stored yet. However, you can retrieve values in the 'edit' operation.

Add a hook function

CiviCRM hook functions allow extensions to run extra logic as part of the normal CiviCRM processing. For example, hook_civicrm_buildForm() allows a module to run logic whenever a web-form is displayed, and hook_civicrm_post() allows a module to run logic after any entity is saved.

Hook function names follow the Drupal convention of being the module's short-name concatenated to the hook name. This strict but simple naming convention is what allows the CiviCRM core to locate your hook functions and call them at the appropriate times. For example, if our module's main file is myextension.php and we want to use hook_civicrm_post() to write to a log file every time a contribution is saved, then our function must be called myextension_civicrm_post().

To implement a hook, add a function to the module's main .php file created earlier with civix generate:module:

/**
 * Implementation of hook_civicrm_post
 */
function myextension_civicrm_post($op, $objectName, $objectId, &$objectRef) {
  switch ($objectName) {
    case 'Contribution':
      $file = '/tmp/contributions.log';
      $message = strtr("Performed \"@op\" at @time on contribution #@id\n", array(
        '@op' => $op,
        '@time' => date('Y-m-d H:i:s'),
        '@id' => $objectId,
      ));
      file_put_contents($file, $message, FILE_APPEND);
      break;
    default:
      // nothing to do
  }
}

Note

When you first created the skeletal project, several hook functions were auto-generated in myextension.php. These functions are usually about one line long – they simply delegate the work to another function. For example myextension_civicrm_config() delegates work to _myextension_civix_civicrm_config(). You should feel free to add more code to myextension_civicrm_config(), but you should preserve the call to _myextension_civix_civicrm_config().

Add a resource file

To include static resources such as stylesheets, Javascript files, or images place them in your extension directory. To load the files at runtime, see the examples in the Resource Reference.

Add a report

CiviReport enables developers to define new business reports using customizable SQL logic and form layouts. If another existing report is close to your needs you may copy and modify it.

To to see the available report generation options activate the civix help:

civix generate:report --help

To create a new report specify the report PHP class name and the CiviCRM component, for example:

civix generate:report MyReport CiviContribute

This creates three files:

  • CRM/Myextension/Form/Report/MyReport.mgd.php stores metadata about the report in a format based on hook_civicrm_managed and the API.
  • CRM/Myextension/Form/Report/MyReport.php contains the form-builder and query-builder for the report. For details about its structure, see the CiviReport Reference.
  • templates/CRM/Myextension/Form/Report/MyReport.tpl contains the report's HTML template. This template usually delegates responsibility to a core template and does not need to be edited.

If one of the existing reports is close to meeting your needs, but requires further PHP or SQL customization, you may simply make a new report based on that report. To copy a report, find the class-name of the original report within the civicrm/CRM/Report/Form/ directory in the CiviCRM repository. Then run the civix generate:report command using the copy option from within your extension directory.

For example, this command will copy the activity report in the class CRM_Report_Form_Activity to a new report within your extension:

civix generate:report --copy CRM_Report_Form_Activity MyActivity Contact

CiviCRM enables developers to define new search forms using customizable SQL logic and form layouts. Use this command to get started:

civix help generate:search

Then you could generate your basic search code for a MySearch class with:

civix generate:search MySearch

This command will create two files:

  • CRM/Myextension/Form/Search/MySearch.mgd.php stores metadata about the custom search. The format of the file is based on hook_civicrm_managed and the API.
  • CRM/Myextension/Form/Search/MySearch.php contains the form-builder and query-builder for the custom search.

If one of the existing searches is close to meeting your needs you may copy it instead and then customise the PHP, SQL and templates.

To make a new search based on an existing search first determine the name of the original search class within the civicrm/CRM/Contact/Form/Search/Custom directory of CiviCRM source tree. Then run the generate:search command from within your module directory.

For example, the zipcode search is in the class CRM_Contact_Form_Search_Custom_ZipCodeRange, so you can copy it with:

civix generate:search --copy CRM_Contact_Form_Search_Custom_ZipCodeRange MySearch

The "copy" option will create either two or three files depending on whether the original search screen defines its own Smarty template.

  1. Disable and re-enable your extension.
  2. Go to Search > Custom Searches... and find your new custom search listed at the bottom.

Now you have a working custom search, but how do you make it do something custom?

See this (somewhat outdated) wiki page for more information.

Add an API function

The CiviCRM API provides a way to expose functions for use by other developers. API functions allow implementing AJAX interfaces (using the CRM.$().crmAPI() helper), and they can also be called via REST, PHP, Smarty, Drush CLI, and more. Each API requires a two-part name: an entity name (such as "Contact", "Event", or "MyEntity") and an action name (such as "create" or "myaction").

Get started by accessing the civix help:

civix help generate:api

Note

Action names must be lowercase. The javascript helpers CRM.api() and CRM.api3() force actions to be lowercase. This issue does not present itself in the API Explorer or when the api action is called via PHP, REST, or SMARTY.

Note

The API loader is very sensitive to case on some platforms. Be careful with capitalization, underscores and multiword action names. API calls can work in one context (Mac OSX filesystem, case-insensitive) and fail in another (Linux filesystem, case-sensitive).

You can make your API code with a command in this pattern:

civix generate:api NewEntity newaction

This creates one file:

  • api/v3/NewEntity/Newaction.php provides the API function civicrm_api3_new_entity_newaction() and the specification function _civicrm_api3_new_entity_newaction_spec(). Note that the parameters and return values must be processed in a particular way (as demonstrated by the auto-generated file).

For use with CiviCRM 4.3 and later, you can also add the --schedule option (e.g., --schedule Hourly). This will create another file:

  • api/v3/NewEntity/Newaction.mgd.php provides the scheduling record that will appear in the CiviCRM's job-manager.

When calling the API, follow these rules:

  • Entity-names are UpperCamelCase
  • Action-names are lowersmashedcase
  • Other variations may work when you call them. Some docs/explorers may show these in cases which work. However, if you try to do the same in new code, you may get a headache.

For example: cv api NewEntity.newaction civicrm_api3('NewEntity', 'newaction')

Tip

Read more about the API architecture for help writing your custom API.

Add a unit-test class

Unit-testing is essential to maintain quality-control over your extension. When developing a test case for a CiviCRM extension, it is useful to run the test case within an active, clean CiviCRM environment. The combined CiviCRM civix testing tools will automate this as long as you follow a few basic conventions.

The following steps will create and run a test in your extension.

Note

Before preparing unit-tests with extensions, you must first configure you personal testing sandbox and enable your extension on the sandbox.

Explore the full options with:

civix help generate:test

To create a skeletal test-class choose a class name in your extension's namespace (CRM_Myextension) that ends with the word Test:

civix generate:test CRM_Myextension_MyTest

This creates a new directory and a new PHP file:

  • tests/phpunit is the base directory for all test classes.
  • tests/phpunit/CRM/Myextension/MyTest.php is the actual test class. It should be written according to the conventions of PHPUnit.

To make sure you can run the test civix needs to know where the CiviCRM base install is located.

The skeletal test class does not do anything useful. For more details on how to write a test class:

To run the tests see the instructions for running PHPUnit tests.

Upgrade civix

What happens when a new version of civix comes out? Your extension should continue to work as-is -- after all, civix is just a code-generator.

However, if you generate an extension with one version of civix (e.g. v16.03), and if you upgrade (e.g. v18.03), and if you run a new generator, then you could encounter problems. This is because generators and templates evolve over time -- in particular, new templates may rely on new helpers and stubs in <mymodule>.php and <mymodule>.civix.php.

Fortunately, these changes are rare; they tend to have limited impact; and there's documentation for them. From time-to-time, you should check UPGRADE.md for suggestions on updating your code to match the current templates.