Projet

Général

Profil

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

root / drupal7 / sites / all / modules / ds / ds.api.php @ 6e9292aa

1
<?php
2

    
3
/**
4
 * @file
5
 * Hooks provided by Display Suite module.
6
 */
7

    
8
/**
9
 * @addtogroup hooks
10
 * @{
11
 */
12

    
13
/**
14
 * Implements hook_ctools_plugin_api().
15
 */
16
function hook_test_ctools_plugin_api($module, $api) {
17
  if (($module == 'ds' && $api == 'ds') || ($module == 'ds_extras' && $api == 'ds_extras')) {
18
    return array('version' => 1);
19
  }
20
}
21

    
22
/**
23
 * Expose Display Suite field settings.
24
 *
25
 * This hook is called by CTools. For this hook to work, you need
26
 * hook_ctools_plugin_api(). The values of this hook can be overridden
27
 * and reverted through the UI.
28
 */
29
function hook_ds_field_settings_info() {
30
  $dsfieldsettings = array();
31

    
32
  $dsfieldsetting = new stdClass;
33
  $dsfieldsetting->disabled = FALSE; /* Edit this to true to make a default dsfieldsetting disabled initially */
34
  $dsfieldsetting->api_version = 1;
35
  $dsfieldsetting->id = 'node|article|default';
36
  $dsfieldsetting->entity_type = 'node';
37
  $dsfieldsetting->bundle = 'article';
38
  $dsfieldsetting->view_mode = 'default';
39
  $dsfieldsetting->settings = array(
40
    'title' => array(
41
      'weight' => '0',
42
      'label' => 'hidden',
43
      'format' => 'default',
44
      'formatter_settings' => array(
45
        'link' => '1',
46
        'wrapper' => 'h3',
47
        'class' => '',
48
      ),
49
    ),
50
    'node_link' => array(
51
      'weight' => '1',
52
      'label' => 'hidden',
53
      'format' => 'default',
54
    ),
55
  );
56
  $dsfieldsettings['node|article|default'] = $dsfieldsetting;
57

    
58
  return $dsfieldsettings;
59
}
60

    
61
/**
62
 * Expose default layout settings info.
63
 *
64
 * This hook is called by CTools. For this hook to work, you need
65
 * hook_ctools_plugin_api(). The values of this hook can be overridden
66
 * and reverted through the UI.
67
 */
68
function hook_ds_layout_settings_info() {
69
  $dslayouts = array();
70

    
71
  $dslayout = new stdClass;
72
  $dslayout->disabled = FALSE; /* Edit this to true to make a default dslayout disabled initially */
73
  $dslayout->api_version = 1;
74
  $dslayout->id = 'node|article|default';
75
  $dslayout->entity_type = 'node';
76
  $dslayout->bundle = 'article';
77
  $dslayout->view_mode = 'default';
78
  $dslayout->layout = 'ds_2col';
79
  $dslayout->settings = array(
80
    'hide_empty_regions' => 0,
81
    'regions' => array(
82
      'left' => array(
83
        0 => 'title',
84
        1 => 'node_link',
85
      ),
86
      'right' => array(
87
        0 => 'body',
88
      ),
89
    ),
90
    'fields' => array(
91
      'title' => 'left',
92
      'node_link' => 'left',
93
      'body' => 'right',
94
    ),
95
    'classes' => array(),
96
  );
97
  $dslayouts['node|article|default'] = $dslayout;
98

    
99
  return $dslayouts;
100
}
101

    
102
/**
103
 * Expose default view modes.
104
 *
105
 * This hook is called by CTools. For this hook to work, you need
106
 * hook_ctools_plugin_api(). The values of this hook can be overridden
107
 * and reverted through the UI.
108
 */
109
function hook_ds_view_modes_info() {
110
  $ds_view_modes = array();
111

    
112
  $ds_view_mode = new stdClass;
113
  $ds_view_mode->disabled = FALSE; /* Edit this to true to make a default ds_view_mode disabled initially */
114
  $ds_view_mode->api_version = 1;
115
  $ds_view_mode->view_mode = 'test_exportables';
116
  $ds_view_mode->label = 'Test exportables';
117
  $ds_view_mode->entities = array(
118
    'node' => 'node',
119
  );
120
  $ds_view_modes['test_exportables'] = $ds_view_mode;
121

    
122
  return $ds_view_modes;
123
}
124

    
125
/**
126
 * Define fields. These fields are not overridable through the interface.
127
 * If you want those, look at hook_ds_custom_fields_info().
128
 *
129
 * @param $entity_type
130
 *   The name of the entity which we are requesting fields for, e.g. 'node'.
131
 *
132
 * @return $fields
133
 *   A collection of fields which keys are the entity type name and values
134
 *   a collection fields.
135
 *
136
 * @see ds_get_fields()
137
 */
138
function hook_ds_fields_info($entity_type) {
139
  $fields = array();
140

    
141
  $fields['title'] = array(
142

    
143
    // title: title of the field
144
    'title' => t('Title'),
145

    
146
    // type: type of field
147
    // - DS_FIELD_TYPE_THEME      : calls a theming function.
148
    // - DS_FIELD_TYPE_FUNCTION   : calls a custom function.
149
    // - DS_FIELD_TYPE_CODE       : calls ds_render_code_field().
150
    // - DS_FIELD_TYPE_BLOCK      : calls ds_render_block_field().
151
    // - DS_FIELD_TYPE_PREPROCESS : calls nothing, just takes a key from the
152
    //                              variable field that is passed on.
153
    // - DS_FIELD_TYPE_IGNORE     : calls nothing, use this if you simple want
154
    //                              to drag and drop. The field itself will have
155
    //                              a theme function.
156
    'field_type' => DS_FIELD_TYPE_FUNCTION,
157

    
158
    // ui_limit : only used for the manage display screen so
159
    // you can limit fields to show based on bundles or view modes
160
    // the values are always in the form of $bundle|$view_mode
161
    // You may use * to select all.
162
    // Make sure you use the machine name.
163
    'ui_limit' => array('article|full', '*|search_index'),
164

    
165
    // file: an optional file in which the function resides.
166
    // Only for DS_FIELD_TYPE_FUNCTION.
167
    'file' => 'optional_filename',
168

    
169
    // function: only for DS_FIELD_TYPE_FUNCTION.
170
    'function' => 'theme_ds_title_field',
171

    
172
    // properties: can have different keys.
173
    'properties' => array(
174

    
175
      // formatters: optional if a function is used.
176
      // In case the field_type is DS_FIELD_TYPE_THEME, you also
177
      // need to register these formatters as a theming function
178
      // since the key will be called with theme('function').
179
      // The value is the caption used in the selection config on Field UI.
180
      'formatters' => array(
181
        'node_title_nolink_h1' => t('H1 title'),
182
        'node_title_link_h1' => t('H1 title, linked to node'),
183
      ),
184

    
185
      // settings & default: optional if you have a settings form for your field.
186
      'settings' => array(
187
        'wrapper' => array('type' => 'textfield', 'description' => t('Eg: h1, h2, p')),
188
        'link' => array('type' => 'select', 'options' => array('yes', 'no')),
189
      ),
190
      'default' => array('wrapper' => 'h2', 'link' => 0),
191

    
192
      // code: optional, only for code field.
193
      'code' => 'my code here',
194

    
195
      // use_token: optional, only for code field.
196
      'use_token' => TRUE, // or FALSE,
197

    
198
      // block: the module and delta of the block, only for block fields.
199
      'block' => 'user-menu',
200

    
201
      // block_render: block render type, only for block fields.
202
      // - DS_BLOCK_CONTENT       : render through block template file.
203
      // - DS_BLOCK_TITLE_CONTENT : render only title and content.
204
      // - DS_BLOCK_CONTENT       : render only content.
205
      'block_render' => DS_BLOCK_CONTENT,
206
    )
207
  );
208

    
209
  return array('node' => $fields);
210

    
211
}
212

    
213
/**
214
 * Define custom fields which can be overridden through the UI and which
215
 * are exportable. The keys are almost the same as in hook_ds_fields_info()
216
 * except that field_type is limited and you need an entities key.
217
 *
218
 * This hook is called by CTools. For this hook to work, you need
219
 * hook_ctools_plugin_api(). The values of this hook can be overridden
220
 * and reverted through the UI.
221
 */
222
function hook_ds_custom_fields_info() {
223
  $ds_fields = array();
224

    
225
  $ds_field = new stdClass;
226
  $ds_field->api_version = 1;
227
  $ds_field->field = 'custom_field';
228
  $ds_field->label = 'Custom field';
229

    
230
  // Field type: either block or code
231
  // DS_FIELD_TYPE_CODE: 5
232
  // DS_FIELD_TYPE_BLOCK: 6
233
  $ds_field->field_type = 5;
234

    
235
  // Collection of entities on which this custom field can work on.
236
  $ds_field->entities = array(
237
    'node' => 'node',
238
  );
239
  $ds_field->properties = array(
240
    'code' => array(
241
      'value' => '<?php print "this is a custom field"; ?>',
242
      'format' => 'ds_code',
243
    ),
244
    'use_token' => 0,
245
  );
246
  $ds_fields['custom_field'] = $ds_field;
247

    
248
  return $ds_fields;
249
}
250

    
251
/**
252
 * Expose Views layouts definitions.
253
 *
254
 * This hook is called by CTools. For this hook to work, you need
255
 * hook_ctools_plugin_api(). The values of this hook can be overridden
256
 * and reverted through the UI.
257
 */
258
function hook_ds_vd_info() {
259
  $ds_vds = array();
260

    
261
  $ds_vd = new stdClass;
262
  $ds_vd->api_version = 1;
263
  $ds_vd->vd = 'frontpage-page';
264
  $ds_vd->label = 'Frontpage: Views displays';
265
  $ds_vds['frontpage-page'] = $ds_vd;
266

    
267
  return $ds_vds;
268
}
269

    
270
/**
271
 * Alter fields defined by Display Suite.
272
 *
273
 * This function is called for each entity type.
274
 *
275
 * @param $fields
276
 *   An array with fields which can be altered just before they get cached.
277
 * @param $entity_type
278
 *   The name of the entity type.
279
 */
280
function hook_ds_fields_info_alter(&$fields, $entity_type) {
281
  if (isset($fields['title'])) {
282
    $fields['title']['title'] = t('My title');
283
  }
284
}
285

    
286
/**
287
 * Alter fields defined by Display Suite just before they get
288
 * rendered on the Field UI. Use this hook to inject fields
289
 * which you can't alter with hook_ds_fields_info_alter().
290
 *
291
 * Use this in edge cases, see ds_extras_ds_fields_ui_alter()
292
 * which adds fields chosen in Views UI. This also runs
293
 * when a layout has been chosen.
294
 *
295
 * @param $fields
296
 *   An array with fields which can be altered just before they get cached.
297
 * @param $entity_type
298
 *   The name of the entity type.
299
 */
300
function hook_ds_fields_ui_alter(&$fields, $context) {
301
  $fields['title'] = t('Extra title');
302
}
303

    
304
/**
305
 * Define theme functions for fields.
306
 *
307
 * This only is necessary when you're using the field settings
308
 * plugin which comes with the DS extras module and you want to
309
 * expose a special field theming function to the interface.
310
 *
311
 * The theme function gets $variables as the only parameter.
312
 * The optional configuration through the UI is in $variables['ds-config'].
313
 *
314
 * Note that 'theme_ds_field_' is always needed, so the suggestions can work.
315
 *
316
 * @return $field_theme_functions
317
 *   A collection of field theming functions.
318
 */
319
function hook_ds_field_theme_functions_info() {
320
  return array('theme_ds_field_mine' => t('Theme field'));
321
}
322

    
323
/**
324
 * Return configuration summary for the field format.
325
 *
326
 * As soon as you have hook_ds_fields and one of the fields
327
 * has a settings key, Display Suite will call this hook for the summary.
328
 *
329
 * @param $field
330
 *   The configuration of the field.
331
 *
332
 * @return $summary
333
 *   The summary to show on the Field UI.
334
 */
335
function hook_ds_field_format_summary($field) {
336
  return 'Field summary';
337
}
338

    
339
/**
340
 * Return a settings form for a Display Suite field.
341
 *
342
 * As soon as you have hook_ds_fields and one of the fields
343
 * has a settings key, Display Suite will call this hook for field form.
344
 *
345
 * @param $field
346
 *   The configuration of the field.
347
 *
348
 * @return $form
349
 *   A form definition.
350
 */
351
function hook_ds_field_settings_form($field) {
352

    
353
  // Saved formatter settings are on $field['formatter_settings'];
354
  $settings = isset($field['formatter_settings']) ? $field['formatter_settings'] : $field['properties']['default'];
355

    
356
  $form['label'] = array(
357
    '#type' => 'textfield',
358
    '#title' => t('Label'),
359
    '#default_value' => $settings['label'],
360
  );
361
}
362

    
363
/**
364
 * Modify the layout settings just before they get saved.
365
 *
366
 * @param $record
367
 *   The record just before it gets saved into the database.
368
 * @param $form_state
369
 *   The form_state values.
370
 */
371
function hook_ds_layout_settings_alter($record, $form_state) {
372
  $record->settings['hide_page_title'] = TRUE;
373
}
374

    
375
/**
376
 * Modify the field settings before they get saved.
377
 *
378
 * @param $field_settings
379
 *   A collection of field settings which keys are fields.
380
 * @param $form
381
 *   The current form which is submitted.
382
 * @param $form_state
383
 *   The form state with all its values.
384
 */
385
function hook_ds_field_settings_alter(&$field_settings, $form, $form_state) {
386
  $field_settings['title']['region'] = 'left';
387
}
388

    
389
/**
390
 * Define layouts from code.
391
 *
392
 * @return $layouts
393
 *   A collection of layouts.
394
 */
395
function hook_ds_layout_info() {
396
  $path = drupal_get_path('module', 'foo');
397

    
398
  $layouts = array(
399
    'foo_1col' => array(
400
      'label' => t('Foo one column'),
401
      'path' => $path . '/layouts/foo_1col',
402
      'regions' => array(
403
        'foo_content' => t('Content'),
404
      ),
405
      'css' => TRUE,
406
      // optional, form only applies to node form at this point.
407
      'form' => TRUE,
408
    ),
409
  );
410

    
411
  return $layouts;
412
}
413

    
414
/**
415
 * Alter the layout render array.
416
 *
417
 * @param $layout_render_array
418
 *   The render array
419
 * @param $context
420
 *   An array with the context that is being rendered. Available keys are
421
 *   - entity
422
 *   - entity_type
423
 *   - bundle
424
 *   - view_mode
425
 * @param array $vars
426
 *   All variables available for render. You can use this to add css classes.
427
 */
428
function hook_ds_pre_render_alter(&$layout_render_array, $context, &$vars) {
429
  $layout_render_array['left'][] = array('#markup' => 'cool!', '#weight' => 20);
430
  $vars['attributes_array']['class'][] = 'custom';
431
}
432

    
433
/**
434
 * Alter layouts found by Display Suite.
435
 *
436
 * @param $layouts
437
 *   A array of layouts which keys are the layout and which values are
438
 *   properties of that layout (label, path, regions and css).
439
 */
440
function hook_ds_layout_info_alter(&$layouts) {
441
  unset($layouts['ds_2col']);
442
}
443

    
444
/**
445
 * Alter the region options in the field UI screen.
446
 *
447
 * This function is only called when a layout has been chosen.
448
 *
449
 * @param $context
450
 *   A collection of keys for the context. The keys are 'entity_type',
451
 *   'bundle' and 'view_mode'.
452
 * @param $region_info
453
 *   A collection of info for regions. The keys are 'region_options'
454
 *   and 'table_regions'.
455
 */
456
function hook_ds_layout_region_alter($context, &$region_info) {
457
  $region_info['region_options'][$block_key] = $block['title'];
458
  $region_info['table_regions'][$block_key] = array(
459
    'title' => check_plain($block['title']),
460
    'message' => t('No fields are displayed in this region'),
461
  );
462
}
463

    
464
/**
465
 * Alter the field label options. Note that you will either
466
 * update the preprocess functions or the field.tpl.php file when
467
 * adding new options.
468
 *
469
 * @param $field_label_options
470
 *   A collection of field label options.
471
 */
472
function hook_ds_label_options_alter(&$field_label_options) {
473
  $field_label_options['label_after'] = t('Label after field');
474
}
475

    
476
/**
477
 * Themes can also define extra layouts.
478
 *
479
 * Create a ds_layouts folder and then a folder name that will
480
 * be used as key for the layout. The folder should at least have 2 files:
481
 *
482
 * - key.inc
483
 * - key.tpl.php
484
 *
485
 * The css file is optional.
486
 * - key.css
487
 *
488
 * e.g.
489
 * bartik/ds_layouts/bartik_ds/bartik_ds.inc
490
 *                            /bartik-ds.tpl.php
491
 *                            /bartik_ds.css
492
 *
493
 * bartik_ds.inc must look like this:
494
 *
495

496
  // Fuction name is ds_LAYOUT_KEY
497
  function ds_bartik_ds() {
498
    return array(
499
      'label' => t('Bartik DS'),
500
      'regions' => array(
501
        // The key of this region name is also the variable used in
502
        // the template to print the content of that region.
503
        'bartik' => t('Bartik DS'),
504
      ),
505
      // Add this if there is a default css file.
506
      'css' => TRUE,
507
      // Add this if there is a default preview image
508
      'image' => TRUE,
509
    );
510
  }
511

512
 */
513

    
514
/**
515
 * Alter the view mode just before it's rendered by the DS views entity plugin.
516
 *
517
 * @param $view_mode
518
 *   The name of the view mode.
519
 * @param $context
520
 *   A collection of items which can be used to identify in what
521
 *   context an entity is being rendered. The variable contains 3 keys:
522
 *     - entity: The entity being rendered.
523
 *     - view_name: the name of the view.
524
 *     - display: the name of the display of the view.
525
 */
526
function hook_ds_views_view_mode_alter(&$view_mode, $context) {
527
  if ($context['view_name'] == 'my_view_name') {
528
    $view_mode = 'new_view_mode';
529
  }
530
}
531

    
532
/**
533
 * Theme an entity coming from the views entity plugin.
534
 *
535
 * @param $entity
536
 *   The complete entity.
537
 * @param $view_mode
538
 *   The name of the view mode.
539
 */
540
function ds_views_row_ENTITY_NAME($entity, $view_mode) {
541
  $nid = $vars['row']->{$vars['field_alias']};
542
  $node = node_load($nid);
543
  $element = node_view($node, $view_mode);
544
  return drupal_render($element);
545
}
546

    
547
/**
548
 * Theme an entity through an advanced function coming from the views entity plugin.
549
 *
550
 * @param $vars
551
 *   An array of variables from the views preprocess functions.
552
 * @param $view_mode
553
 *   The name of the view mode.
554
 */
555
function ds_views_row_adv_VIEWS_NAME(&$vars, $view_mode) {
556
  // You can do whatever you want to here.
557
  $vars['object'] = 'This is what I want for christmas.';
558
}
559

    
560
/**
561
 * Modify the entity render array in the context of a view.
562
 *
563
 * @param array $content
564
 *   By reference. An entity view render array.
565
 * @param array $context
566
 *   By reference. An associative array containing:
567
 *   - row: The current active row object being rendered.
568
 *   - view: By reference. The current view object.
569
 *   - view_mode: The view mode which is set in the Views' options.
570
 *   - load_comments: The same param passed to each row function.
571
 *
572
 * @see ds_views_row_render_entity()
573
 */
574
function hook_ds_views_row_render_entity_alter(&$content, &$context) {
575
  if ($context['view_mode'] == 'my_mode') {
576
    // Modify the view, or the content render array in the context of a view.
577
    $view = &$context['view'];
578
    $element = &drupal_array_get_nested_value($content, array('field_example', 0));
579
  }
580
}
581

    
582
/**
583
 * Alter the strings used to separate taxonomy terms.
584
 */
585
function hook_ds_taxonomy_term_separators(&$separators) {
586
  // Remove the option to use a hyphen.
587
  unset($separators[' - ']);
588
  // Add the option to use a pipe.
589
  $separators[' | '] = t('pipe');
590
}
591

    
592
/**
593
 * Allow modules to provide additional classes for regions and layouts.
594
 */
595
function hook_ds_classes_alter(&$classes, $name) {
596
  if ('ds_classes_regions' === $name) {
597
    $classes['css-class-name'] = t('Custom Styling');
598
  }
599
}
600

    
601
/**
602
 * Alter the field template settings form
603
 *
604
 * @param array $form
605
 *   The form containing the field settings
606
 * @param array $field_settings
607
 *   The settings of the field
608
 */
609
function hook_ds_field_theme_functions_settings_alter(&$form, $field_settings) {
610
  $form['something'] = array(
611
    '#type' => 'textfield',
612
    '#title' => 'test',
613
  );
614
}
615

    
616
/*
617
 * @} End of "addtogroup hooks".
618
 */