Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Important: This page is a tour of the code for the submodule patterns_export and the components. If you are looking for information on how to write a an export function for a component please read this section before.
Exporting patterns: Flow of Execution
In this section we will analyze the flow of execution provided by the submodule patterns_export, that allows us to extract Patterns files automatically. For this example we will be using the color component.
- A new entry in the menu is set by hook_menu_alter(). The function called when accessing to the url is patterns_export(), which will check if there are any parsers available and call the function patterns_export_page1() after loading all the components.
- The function patterns_export_page1() will take care of:
- Invoke all the modules implementing patterns, and filter those which provide any export function.
- Prepare the form with the granularity options to export for each of these components (i.e.: the taxonomy component allows to export terms and/or vocabularies).
- The function patterns_export_validate() will be executed afterwards, and if the form presents all the expected values patterns_export_submit() is run:
- Depending on the option, either it will take the whole array or it will filter according to our preferences. In our case we have decided to export all the settings of the component color, therefore the $exports array looks as follows after being processed:
$exports Array [1] color Array [1] color Array [2] export Array [1] 0 color_patterns_export_all_color files Array [2] 0 modules/system/system.module 1 modules/system/system.admin.inc
- Depending on the option, either it will take the whole array or it will filter according to our preferences. In our case we have decided to export all the settings of the component color, therefore the $exports array looks as follows after being processed:
- Once the form has been processed, the function patterns_export_start_engine() is executed. This function checks the format of the file and the execution mode, and calls the function patterns_export_php() or patterns_export_batch() depending on the selected mode.
Exporting configuration in PHP mode
In our example, we will describe the flow choosing the php mode, therefore the function patterns_export_php() (in patterns_export/core.inc) is executed:
- It calls patterns_export_core_php() (in patterns_export/core.inc), making use of the PHP function list to assign several values ($sections and $modules) returned by this function in once
- Internally this function will iterate through the received array to add all the files required by the component and invoke the export functions defined by the component.
- Following our example, the function color_patterns_export_all_color() is invoked, and after processing the values the function will return the following values:
$result Array [3] color Array [0] 0 Array [1] color Array [2] 0 Array [1] modify Array [4] tag color theme bartik palette Array [0] scheme default 1 Array [1] modify Array [4] tag color theme garland palette Array [0] scheme default 1 Array [1] 0 color
-
Then the function patterns_export_finalize_export() (in finalized.inc) is executed. This function checks if any of the sections is empty to skip it, and then it calls a different function to return the result as a file, zip or importing it directly in the Database. All these functions performed a similar first step that consists in building a Pattern compatible object and then call a function from the main module to return the expected format.
Following our example in which we kept the default option "Import into the Database", the function patterns_export_to_db() is executed:- Then _patterns_export_merge_exported_patterns() is called, returning a Patterns object such as:
$pattern Array [3] info Array [8] title Untitled Pattern category General description No description version 1.0 core x.y author root author_email david.rozas@gmail.com author_website http://www.davidrozas.com modules Array [1] 0 color color Array [2] 0 Array [1] modify Array [4] tag color theme bartik palette Array [0] scheme default 1 Array [1] modify Array [4] tag color theme garland palette Array [0] scheme default
- Then the function patterns_io_save_pattern() (in includes/io/io.inc is called, following the same flow discussed in previous sections.
- Then _patterns_export_merge_exported_patterns() is called, returning a Patterns object such as: