Community Documentation

Writing an export function for a Patterns component

Last updated November 22, 2012. Created by shakty on April 24, 2012.
Edited by drozas. Log in to edit this page.

This page deals with an experimental feature of the Patterns module: Automatic exporting the configuration of a Drupal web site.

In the alpha release of Patterns 7.x automatic exporting was achieved by implementing hook_get_arguments. While still valid, such hook is now deprecated in favor of using hook_patterns and specifying one or more export functions as a return value in the associative array. The following code snippet explains how:

function mymodule_patterns($data = NULL) {

  $actions['mytag'] = array(
    // ...
    PATTERNS_EXPORT => array(PATTERNS_EXPORT_ALL => 'mymodule_export_all_mytag'),
   );
}

function mymodule_export_all_mytag(){
  // ...
  //return $all_mytag_collection;
}

For each tag (here only 'mytag') it is possible to specify one ore more export function inside the array indexed by PATTERNS_EXPORT. The key of such array will be displayed to the users for selection in the Export page, while the values must contain the name of a valid function.

The export function, must return an associative array of Patterns actions (create, modify, delete) specific for the tag of interest. An example of such function taken from the Taxonomy component follow:

function taxonomy_patterns_export_all_vocs($args = NULL, &$result = NULL) {
$info = taxonomy_patterns();
$form_id = current($info['vocabulary'][PATTERNS_MODIFY]);
$vocs = taxonomy_get_vocabularies();
$result = array();
foreach ($vocs as $voc) {
// It is important to user array merge. Pushing is not enough
  $result = array_merge($result, patterns_api_extract_actions($form_id, $voc, 'vocabulary', PATTERNS_MODIFY));
  }
  return $result;
}

The above listed function makes use of a very important pattern API: patterns_api_extract_actions. Such api can call a form, retrieve the current values, and return them incapsulated inside a valid pattern action. Extensive documentation for developers about patterns_api_extract_actions is available as comments in the code of the of patterns/includes/api/api.inc file.

Further details about the internal flow of execution while exporting configuration can be found in the following section.
A very simple "patterns_hello_world" module implementing an export function can also be checkout from GitHub at: git://github.com/QScience/patterns_hello_world.git

Page status

About this page

Drupal version
Drupal 7.x
Audience
Programmers
Level
Intermediate, Advanced

Administration & Security Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here