Projet

Général

Profil

Paste
Télécharger (17,3 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / help / export.html @ 7e72b748

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

    
3
<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>
4

    
5
<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>
6

    
7
<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>
8

    
9
<h2>Making your objects exportable</h2>
10

    
11
<p>To make your objects exportable, you do have to do a medium amount of work.</p>
12

    
13
<ol>
14
  <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>
15
  <li>Create a load function for your object that utilizes ctools_export_load_object().</li>
16
  <li>Create a save function for your object that utilizes drupal_write_record() or any method you desire.</li>
17
  <li>Create an import and export mechanism from the UI.</li>
18
</ol>
19

    
20
<h2>The export section of the schema file</h2>
21

    
22
<p>Exportable objects are created by adding definition to the schema in an 'export' section. For example:</p>
23

    
24
<pre>
25
function mymodule_schema() {
26
  $schema['mymodule_myobj'] = array(
27
    'description' => t('Table storing myobj definitions.'),
28
    'export' => array(
29
      'key' => 'name',
30
      'key name' => 'Name',
31
      'primary key' => 'oid',
32
      'identifier' => 'myobj', // Exports will be as $myobj
33
      'default hook' => 'default_mymodule_myobj',  // Function hook name.
34
      'api' => array(
35
        'owner' => 'mymodule',
36
        'api' => 'default_mymodule_myobjs',  // Base name for api include files.
37
        'minimum_version' => 1,
38
        'current_version' => 1,
39
      ),
40
      // If the key is stored in a table that is joined in, specify it:
41
      'key in table' => 'my_join_table',
42

    
43
    ),
44

    
45
    // If your object's data is split up across multiple tables, you can
46
    // specify additional tables to join. This is very useful when working
47
    // with modules like exportables.module that has a special table for
48
    // translating keys to local database IDs.
49
    //
50
    // The joined table must have its own schema definition.
51
    //
52
    // If using joins, you should implement a 'delete callback' (see below)
53
    // to ensure that deletes happen properly. export.inc does not do this
54
    // automatically!
55
    'join' => array(
56
      'exportables' => array(
57
        // The following parameters will be used in this way:
58
        // SELECT ... FROM {mymodule_myobj} t__0 INNER JOIN {my_join_table} t__1 ON t__0.id = t__1.id AND extras
59
        'table' => 'my_join_table',
60
        'left_key' => 'format',
61
        'right_key' => 'id',
62
        // Optionally you can define a callback to add custom conditions or
63
        // alter the query as necessary. The callback function takes 3 args:
64
        //
65
        //   myjoincallback(&$query, $schema, $join_schema);
66
        //
67
        // where $query is the database query object, $schema is the schema for
68
        // the export base table and $join_schema is the schema for the current
69
        // join table.
70
        'callback' => 'myjoincallback',
71

    
72
        // You must specify which fields will be loaded. These fields must
73
        // exist in the schema definition of the joined table.
74
        'load' => array(
75
          'machine',
76
        ),
77

    
78
        // And finally you can define other tables to perform INNER JOINS
79
        //'other_joins' => array(
80
        //   'table' => ...
81
        //),
82
      ),
83
    )
84
    'fields' => array(
85
      'name' => array(
86
        'type' => 'varchar',
87
        'length' => '255',
88
        'description' => 'Unique ID for this object. Used to identify it programmatically.',
89
      ),
90
      'oid' => array(
91
        'type' => 'serial',
92
        'unsigned' => TRUE,
93
        'not null' => TRUE,
94
        'description' => 'Primary ID field for the table. Not used for anything except internal lookups.',
95
        'no export' => TRUE, // Do not export database-only keys.
96
      ),
97
    // ......
98
    'primary key' => array('oid'),
99
    'unique keys' => array(
100
      'name' => array('name'),
101
    ),
102
  );
103
  return $schema;
104
}
105
</pre>
106

    
107
<dl>
108
<dt>key</dt>
109
<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>
110

    
111
<dt>key name</dt>
112
<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>
113

    
114
<dt>primary key</dt>
115
<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>
116

    
117
<dt>object</dt>
118
<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>
119

    
120
<dt>object factory</dt>
121
<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>
122

    
123
<dt>admin_title</dt>
124
<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>
125

    
126
<dt>admin_description</dt>
127
<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>
128

    
129
<dt>can disable</dt>
130
<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>
131

    
132
<dt>status</dt>
133
<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>
134

    
135
<dt>default hook</dt>
136
<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>
137

    
138
<dt>cache defaults</dt>
139
<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>
140

    
141
<dt>default cache bin</dt>
142
<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>
143

    
144
<dt>identifier</dt>
145
<dd>When exporting the object, the identifier is the variable that the exported object will be placed in. Defaults to $table.</dd>
146

    
147
<dt>bulk export</dt>
148
<dd>Declares whether or not the exportable will be available for bulk exporting.</dd>
149

    
150
<dt>export type string</dt>
151
<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>
152

    
153
<dt>list callback</dt>
154
<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>
155

    
156
<dt>to hook code callback</dt>
157
<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>
158

    
159
<dt>boolean</dt>
160
<dd>Explicitly indicate if a table field contains a boolean or not. The Schema API does not model the
161
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.
162

    
163
<dt>create callback</dt>
164
<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>
165

    
166
<dt>load callback</dt>
167
<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>
168

    
169
<dt>load multiple callback</dt>
170
<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>
171

    
172
<dt>load all callback</dt>
173
<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>
174

    
175
<dt>save callback</dt>
176
<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>
177

    
178
<dt>delete callback</dt>
179
<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>
180

    
181
<dt>export callback</dt>
182
<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>
183

    
184
<dt>import callback</dt>
185
<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>
186

    
187
<dt>status callback</dt>
188
<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>
189

    
190
<dt>api</dt>
191
<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.
192

    
193
<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>
194

    
195
<p>api supports these subkeys:</p>
196

    
197
<dl>
198
<dt>owner</dt>
199
<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>
200
<dt>api</dt>
201
<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>
202
<dt>minimum_version</dt>
203
<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>
204
<dt>current_version</dt>
205
<dd>The current version of the API. Any module reporting a required API higher than this will not have its default objects used.</dd>
206
</dl>
207
</dd>
208

    
209
</dl>
210
<p>In addition, each field can contain the following:</p>
211
<dl>
212
<dt>no export</dt>
213
<dd>Set to TRUE to prevent that field from being exported.</dd>
214

    
215
<dt>export callback</dt>
216
<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>
217
</dl>
218

    
219
<h2>Reserved keys on exportable objects</h2>
220

    
221
<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>
222
<dl>
223
<dt>api_version</dt>
224
<dd>The API version that this object implements.</dd>
225

    
226
<dt>disabled</dt>
227
<dd>A boolean for whether the object is disabled.</dd>
228

    
229
<dt>export_module</dt>
230
<dd>For objects that live in code, the module which provides the default object.</dd>
231

    
232
<dt>export_type</dt>
233
<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.
234
</dd>
235

    
236
<dt>in_code_only</dt>
237
<dd>A boolean for whether the object lives only in code.</dd>
238

    
239
<dt>table</dt>
240
<dd>The schema API table that this object belongs to.</dd>
241

    
242
<dt>type</dt>
243
<dd>A string representing the storage type of this object. Can be one of the following:
244
<ul>
245
<li><em>Normal</em> is an object that lives only in the database.</li>
246
<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>
247
<li><em>Default</em> is an object that lives only in code.</li>
248
</ul>
249
<i>Note: This key can be changed by setting 'export type string' to something else, to try and prevent "type" from conflicting.</i>
250
</dd>
251
</dl>
252

    
253
<h2>The load callback</h2>
254
<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>
255

    
256
<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>
257

    
258
<pre>
259
/**
260
 * Implements 'load callback' for myobj exportables.
261
 */
262
function mymodule_myobj_load($name) {
263
  ctools_include('export');
264
  $result = ctools_export_load_object('mymodule_myobjs', 'names', array($name));
265
  if (isset($result[$name])) {
266
    return $result[$name];
267
  }
268
}
269

    
270
/**
271
 * Implements 'load multiple callback' for myobj exportables.
272
 */
273
function mymodule_myobj_load_multiple(array $names) {
274
  ctools_include('export')
275
  $results = ctools_export_load_object('mymodule_myobjs', 'names', $names);
276
  return array_filter($results);
277
}
278
</pre>
279

    
280
<h2>The save callback</h2>
281
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.
282

    
283
<pre>
284
/**
285
* Save a single myobj.
286
*/
287
function mymodule_myobj_save(&$myobj) {
288
  $update = (isset($myobj->oid) && is_numeric($myobj->oid)) ? array('oid') : array();
289
  return drupal_write_record('myobj', $myobj, $update);
290
}
291
</pre>
292

    
293
<h2>Default hooks for your exports</h2>
294
<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>