export.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <p>Exportable objects are objects that can live either in the database or in code, or in both. If they live in both, then the object in code is considered to be "overridden", meaning that the version in code is ignored in favor of the version in the database.</p>
  2. <p>The main benefit to this is that you can move objects that are intended to be structure or feature-related into code, thus removing them entirely from the database. This is a very important part of the deployment path, since in an ideal world, the database is primarily user generated content, whereas site structure and site features should be in code. However, many many features in Drupal rely on objects being in the database and provide UIs to create them.</p>
  3. <p>Using this system, you can give your objects dual life. They can be created in the UI, exported into code and put in revision control. Views and Panels both use this system heavily. Plus, any object that properly implements this system can be utilized by the Features module to be used as part of a bundle of objects that can be turned into feature modules.</p>
  4. <p>Typically, exportable objects have two identifiers. One identifier is a simple serial used for database identification. It is a primary key in the database and can be used locally. It also has a name which is an easy way to uniquely identify it. This makes it much less likely that importing and exporting these objects across systems will have collisions. They still can, of course, but with good name selection, these problems can be worked around.</p>
  5. <h2>Making your objects exportable</h2>
  6. <p>To make your objects exportable, you do have to do a medium amount of work.</p>
  7. <ol>
  8. <li>Create a chunk of code in your object's schema definition in the .install file to introduce the object to CTools' export system.</li>
  9. <li>Create a load function for your object that utilizes ctools_export_load_object().</li>
  10. <li>Create a save function for your object that utilizes drupal_write_record() or any method you desire.</li>
  11. <li>Create an import and export mechanism from the UI.</li>
  12. </ol>
  13. <h2>The export section of the schema file</h2>
  14. <p>Exportable objects are created by adding definition to the schema in an 'export' section. For example:</p>
  15. <pre>
  16. function mymodule_schema() {
  17. $schema['mymodule_myobj'] = array(
  18. 'description' => t('Table storing myobj definitions.'),
  19. 'export' => array(
  20. 'key' => 'name',
  21. 'key name' => 'Name',
  22. 'primary key' => 'oid',
  23. 'identifier' => 'myobj', // Exports will be as $myobj
  24. 'default hook' => 'default_mymodule_myobj', // Function hook name.
  25. 'api' => array(
  26. 'owner' => 'mymodule',
  27. 'api' => 'default_mymodule_myobjs', // Base name for api include files.
  28. 'minimum_version' => 1,
  29. 'current_version' => 1,
  30. ),
  31. // If the key is stored in a table that is joined in, specify it:
  32. 'key in table' => 'my_join_table',
  33. ),
  34. // If your object's data is split up across multiple tables, you can
  35. // specify additional tables to join. This is very useful when working
  36. // with modules like exportables.module that has a special table for
  37. // translating keys to local database IDs.
  38. //
  39. // The joined table must have its own schema definition.
  40. //
  41. // If using joins, you should implement a 'delete callback' (see below)
  42. // to ensure that deletes happen properly. export.inc does not do this
  43. // automatically!
  44. 'join' => array(
  45. 'exportables' => array(
  46. // The following parameters will be used in this way:
  47. // SELECT ... FROM {mymodule_myobj} t__0 INNER JOIN {my_join_table} t__1 ON t__0.id = t__1.id AND extras
  48. 'table' => 'my_join_table',
  49. 'left_key' => 'format',
  50. 'right_key' => 'id',
  51. // Optionally you can define a callback to add custom conditions or
  52. // alter the query as necessary. The callback function takes 3 args:
  53. //
  54. // myjoincallback(&$query, $schema, $join_schema);
  55. //
  56. // where $query is the database query object, $schema is the schema for
  57. // the export base table and $join_schema is the schema for the current
  58. // join table.
  59. 'callback' => 'myjoincallback',
  60. // You must specify which fields will be loaded. These fields must
  61. // exist in the schema definition of the joined table.
  62. 'load' => array(
  63. 'machine',
  64. ),
  65. // And finally you can define other tables to perform INNER JOINS
  66. //'other_joins' => array(
  67. // 'table' => ...
  68. //),
  69. ),
  70. )
  71. 'fields' => array(
  72. 'name' => array(
  73. 'type' => 'varchar',
  74. 'length' => '255',
  75. 'description' => 'Unique ID for this object. Used to identify it programmatically.',
  76. ),
  77. 'oid' => array(
  78. 'type' => 'serial',
  79. 'unsigned' => TRUE,
  80. 'not null' => TRUE,
  81. 'description' => 'Primary ID field for the table. Not used for anything except internal lookups.',
  82. 'no export' => TRUE, // Do not export database-only keys.
  83. ),
  84. // ......
  85. 'primary key' => array('oid'),
  86. 'unique keys' => array(
  87. 'name' => array('name'),
  88. ),
  89. );
  90. return $schema;
  91. }
  92. </pre>
  93. <dl>
  94. <dt>key</dt>
  95. <dd>This is the primary key of the exportable object and should be a string as names are more portable across systems. It is possible to use numbers here, but be aware that export collisions are very likely. Defaults to 'name'.</dd>
  96. <dt>key name</dt>
  97. <dd>Human readable title of the export key. Defaults to 'Name'. Because the schema is cached, do not translate this. It must instead be translated when used.</dd>
  98. <dt>primary key</dt>
  99. <dd>A single field within the table that is to be used as the main identifier to discern whether or not the object has been written. As the schema definition's primary key value will be used by default, it is not usually necessary to define this.</dd>
  100. <dt>object</dt>
  101. <dd>The class the object should be created as, if 'object factory' is not set. If this is not set either, defaults as stdClass.</dd>
  102. <dt>object factory</dt>
  103. <dd>Function used to create the object. The function receives the schema and the loaded data as a parameters: your_factory_function($schema, $data). If this is set, 'object' has no effect since you can use your function to create whatever class you wish.</dd>
  104. <dt>admin_title</dt>
  105. <dd>A convenience field that may contain the field which represents the human readable administrative title for use in export UI. If a field "admin_title" exists, it will automatically be used.</dd>
  106. <dt>admin_description</dt>
  107. <dd>A convenience field that may contain the field which represents the human readable administrative title for use in export UI. If a field "admin_title" exists, it will automatically be used.</dd>
  108. <dt>can disable</dt>
  109. <dd>Control whether or not the exportable objects can be disabled. All this does is cause the 'disabled' field on the object to always be set appropriately, and a variable is kept to record the state. Changes made to this state must be handled by the owner of the object. Defaults to TRUE.</dd>
  110. <dt>status</dt>
  111. <dd>Exportable objects can be enabled or disabled, and this status is stored in a variable. This defines what variable that is. Defaults to: 'default_' . $table.</dd>
  112. <dt>default hook</dt>
  113. <dd>What hook to invoke to find exportable objects that are currently defined. These will all be gathered into a giant array. Defaults to 'default_' . $table.</dd>
  114. <dt>cache defaults</dt>
  115. <dd>If true, default objects will be cached so that the processing of the hook does not need to be called often. Defaults to FALSE. Recommended if you will potentially have a lot of objects in code. Not recommended if code will be the exception.</dd>
  116. <dt>default cache bin</dt>
  117. <dd>If default object caching is enabled, what cache bin to use. This defaults to the basic "cache". It is highly recommended that you use a different cache bin if possible.</dd>
  118. <dt>identifier</dt>
  119. <dd>When exporting the object, the identifier is the variable that the exported object will be placed in. Defaults to $table.</dd>
  120. <dt>bulk export</dt>
  121. <dd>Declares whether or not the exportable will be available for bulk exporting.</dd>
  122. <dt>export type string</dt>
  123. <dd>The export type string (Local, Overridden, Database) is normally stored as $item-&gt;type. Since type is a very common keyword, it's possible to specify what key to actually use. </dd>
  124. <dt>list callback</dt>
  125. <dd>Bulk export callback to provide a list of exportable objects to be chosen for bulk exporting. Defaults to $module . '_' . $table . '_list' if the function exists. If it is not, a default listing function will be provided that will make a best effort to list the titles. See ctools_export_default_list().</dd>
  126. <dt>to hook code callback</dt>
  127. <dd>Function used to generate an export for the bulk export process. This is only necessary if the export is more complicated than simply listing the fields. Defaults to $module . '_' . $table . '_to_hook_code'.</dd>
  128. <dt>boolean</dt>
  129. <dd>Explicitly indicate if a table field contains a boolean or not. The Schema API does not model the
  130. difference between a tinyint and a boolean type. Boolean values are stored in tinyint fields. This may cause mismatch errors when exporting a non-boolean value from a tinyint field. Add this to a tinyint field if it contains boolean data and can be exported. Defaults to TRUE.
  131. <dt>create callback</dt>
  132. <dd>CRUD callback to use to create a new exportable item in memory. If not provided, the default function will be used. The single argument is a boolean used to determine if defaults should be set on the object. This object will not be written to the database by this callback.</dd>
  133. <dt>load callback</dt>
  134. <dd>CRUD callback to use to load a single item. If not provided, the default load function will be used. The callback will accept a single argument which should be an identifier of the export key.</dd>
  135. <dt>load multiple callback</dt>
  136. <dd>CRUD callback to use to load multiple items. If not provided, the default multiple load function will be used. The callback will accept an array which should be the identifiers of the export key.</dd>
  137. <dt>load all callback</dt>
  138. <dd>CRUD callback to use to load all items, usually for administrative purposes. If not provided, the default load function will be used. The callback will accept a single argument to determine if the load cache should be reset or not.</dd>
  139. <dt>save callback</dt>
  140. <dd>CRUD callback to use to save a single item. If not provided, the default save function will be used. The callback will accept a single argument which should be the complete exportable object to save.</dd>
  141. <dt>delete callback</dt>
  142. <dd>CRUD callback to use to delete a single item. If not provided, the default delete function will be used. The callback will accept a single argument which can be *either* the object or just the export key to delete. The callback MUST be able to accept either.</dd>
  143. <dt>export callback</dt>
  144. <dd>CRUD callback to use for exporting. If not provided, the default export function will be used. The callback will accept two arguments, the first is the item to export, the second is the indent to place on the export, if any.</dd>
  145. <dt>import callback</dt>
  146. <dd>CRUD callback to use for importing. If not provided, the default export function will be used. This function will accept the code as a single argument and, if the code evaluates, return an object represented by that code. In the case of failure, this will return a string with human readable errors.</dd>
  147. <dt>status callback</dt>
  148. <dd>CRUD callback to use for updating the status of an object. If the status is TRUE the object will be disabled. If the status is FALSE the object will be enabled.</dd>
  149. <dt>api</dt>
  150. <dd>The 'api' key can optionally contain some information for the plugin API definition. This means that the imports can be tied to an API name which is used to have automatic inclusion of files, and can be used to prevent dangerous objects from older versions from being loaded, causing a loss of functionality rather than site crashes or security loopholes.
  151. <p>If not present, no additional files will be loaded and the default hook will always be a simple hook that must be either part of the .module file or loaded during normal operations.</p>
  152. <p>api supports these subkeys:</p>
  153. <dl>
  154. <dt>owner</dt>
  155. <dd>The module that owns the API. Typically this is the name of the module that owns the schema. This will be one of the two keys used by hook_ctools_plugin_api() to determine version compatibility. Note that the name of this hook can be tailored via the use of hook_ctools_plugin_api_hook_name(). See ctools_plugin_api_get_hook() for details.</dd>
  156. <dt>api</dt>
  157. <dd>This is the name of the API, and will be the second parameter to the above mentioned hook. It will also be used as part of the name of the file that the hook containing default objects will be in, which comes in the form of MODULENAME.API.inc.</dd>
  158. <dt>minimum_version</dt>
  159. <dd>The minimum version supported. Any module reporting an API less than this will not have its default objects used. This should be updated only when API changes can cause older objects to crash or otherwise break badly.</dd>
  160. <dt>current_version</dt>
  161. <dd>The current version of the API. Any module reporting a required API higher than this will not have its default objects used.</dd>
  162. </dl>
  163. </dd>
  164. </dl>
  165. <p>In addition, each field can contain the following:</p>
  166. <dl>
  167. <dt>no export</dt>
  168. <dd>Set to TRUE to prevent that field from being exported.</dd>
  169. <dt>export callback</dt>
  170. <dd>A function to override the export behavior. It will receive ($myobject, $field, $value, $indent) as arguments. By default, fields are exported through ctools_var_export().</dd>
  171. </dl>
  172. <h2>Reserved keys on exportable objects</h2>
  173. <p>Exportable objects have several reserved keys that are used by the CTools export API. Each key can be found at <code>$myobj-&gt;{$key}</code> on an object loaded through <code>ctools_export_load_object()</code>. Implementing modules should not use these keys as they will be overwritten by the CTools export API.</p>
  174. <dl>
  175. <dt>api_version</dt>
  176. <dd>The API version that this object implements.</dd>
  177. <dt>disabled</dt>
  178. <dd>A boolean for whether the object is disabled.</dd>
  179. <dt>export_module</dt>
  180. <dd>For objects that live in code, the module which provides the default object.</dd>
  181. <dt>export_type</dt>
  182. <dd>A bitmask representation of an object current storage. You can use this bitmask in combination with the <code>EXPORT_IN_CODE</code> and <code>EXPORT_IN_DATABASE</code> constants to test for an object's storage in your code.
  183. </dd>
  184. <dt>in_code_only</dt>
  185. <dd>A boolean for whether the object lives only in code.</dd>
  186. <dt>table</dt>
  187. <dd>The schema API table that this object belongs to.</dd>
  188. <dt>type</dt>
  189. <dd>A string representing the storage type of this object. Can be one of the following:
  190. <ul>
  191. <li><em>Normal</em> is an object that lives only in the database.</li>
  192. <li><em>Overridden</em> is an object that lives in the database and is overriding the exported configuration of a corresponding object in code.</li>
  193. <li><em>Default</em> is an object that lives only in code.</li>
  194. </ul>
  195. <i>Note: This key can be changed by setting 'export type string' to something else, to try and prevent "type" from conflicting.</i>
  196. </dd>
  197. </dl>
  198. <h2>The load callback</h2>
  199. <p>Calling ctools_export_crud_load($table, $name) will invoke your load callback, calling ctools_export_crud_load_multiple($table, $names) will invoke your load multiple callback, and calling ctools_export_crud_load_all($table, $reset) will invoke your load all callback. The default handlers should be sufficient for most uses.</p>
  200. <p>Typically, there will be three load functions. A 'single' load, to load just one object, a 'multiple' load to load multiple objects, and an 'all' load, to load all of the objects for use in administrating the objects or utilizing the objects when you need all of them. Using ctools_export_load_object() you can easily do both, as well as quite a bit in between. This example duplicates the default functionality for loading one myobj.</p>
  201. <pre>
  202. /**
  203. * Implements 'load callback' for myobj exportables.
  204. */
  205. function mymodule_myobj_load($name) {
  206. ctools_include('export');
  207. $result = ctools_export_load_object('mymodule_myobjs', 'names', array($name));
  208. if (isset($result[$name])) {
  209. return $result[$name];
  210. }
  211. }
  212. /**
  213. * Implements 'load multiple callback' for myobj exportables.
  214. */
  215. function mymodule_myobj_load_multiple(array $names) {
  216. ctools_include('export')
  217. $results = ctools_export_load_object('mymodule_myobjs', 'names', $names);
  218. return array_filter($results);
  219. }
  220. </pre>
  221. <h2>The save callback</h2>
  222. Calling ctools_export_crud_save($table, $object) will invoke your save callback. The default handlers should be sufficient for most uses. For the default save mechanism to work, you <b>must</b> define a 'primary key' in the 'export' section of your schema. The following example duplicates the default functionality for the myobj.
  223. <pre>
  224. /**
  225. * Save a single myobj.
  226. */
  227. function mymodule_myobj_save(&$myobj) {
  228. $update = (isset($myobj->oid) && is_numeric($myobj->oid)) ? array('oid') : array();
  229. return drupal_write_record('myobj', $myobj, $update);
  230. }
  231. </pre>
  232. <h2>Default hooks for your exports</h2>
  233. <p>All exportables come with a 'default' hook, which can be used to put your exportable into code. The easiest way to actually use this hook is to set up your exportable for bulk exporting, enable the bulk export module and export an object.</p>