ds.api.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by Display Suite module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Implements hook_ctools_plugin_api().
  12. */
  13. function hook_test_ctools_plugin_api($module, $api) {
  14. if (($module == 'ds' && $api == 'ds') || ($module == 'ds_extras' && $api == 'ds_extras')) {
  15. return array('version' => 1);
  16. }
  17. }
  18. /**
  19. * Expose Display Suite field settings.
  20. *
  21. * This hook is called by CTools. For this hook to work, you need
  22. * hook_ctools_plugin_api(). The values of this hook can be overridden
  23. * and reverted through the UI.
  24. */
  25. function hook_ds_field_settings_info() {
  26. $dsfieldsettings = array();
  27. $dsfieldsetting = new stdClass;
  28. $dsfieldsetting->disabled = FALSE; /* Edit this to true to make a default dsfieldsetting disabled initially */
  29. $dsfieldsetting->api_version = 1;
  30. $dsfieldsetting->id = 'node|article|default';
  31. $dsfieldsetting->entity_type = 'node';
  32. $dsfieldsetting->bundle = 'article';
  33. $dsfieldsetting->view_mode = 'default';
  34. $dsfieldsetting->settings = array(
  35. 'title' => array(
  36. 'weight' => '0',
  37. 'label' => 'hidden',
  38. 'format' => 'default',
  39. 'formatter_settings' => array(
  40. 'link' => '1',
  41. 'wrapper' => 'h3',
  42. 'class' => '',
  43. ),
  44. ),
  45. 'node_link' => array(
  46. 'weight' => '1',
  47. 'label' => 'hidden',
  48. 'format' => 'default',
  49. ),
  50. );
  51. $dsfieldsettings['node|article|default'] = $dsfieldsetting;
  52. return $dsfieldsettings;
  53. }
  54. /**
  55. * Expose default layout settings info.
  56. *
  57. * This hook is called by CTools. For this hook to work, you need
  58. * hook_ctools_plugin_api(). The values of this hook can be overridden
  59. * and reverted through the UI.
  60. */
  61. function hook_ds_layout_settings_info() {
  62. $dslayouts = array();
  63. $dslayout = new stdClass;
  64. $dslayout->disabled = FALSE; /* Edit this to true to make a default dslayout disabled initially */
  65. $dslayout->api_version = 1;
  66. $dslayout->id = 'node|article|default';
  67. $dslayout->entity_type = 'node';
  68. $dslayout->bundle = 'article';
  69. $dslayout->view_mode = 'default';
  70. $dslayout->layout = 'ds_2col';
  71. $dslayout->settings = array(
  72. 'hide_empty_regions' => 0,
  73. 'regions' => array(
  74. 'left' => array(
  75. 0 => 'title',
  76. 1 => 'node_link',
  77. ),
  78. 'right' => array(
  79. 0 => 'body',
  80. ),
  81. ),
  82. 'fields' => array(
  83. 'title' => 'left',
  84. 'node_link' => 'left',
  85. 'body' => 'right',
  86. ),
  87. 'classes' => array(),
  88. );
  89. $dslayouts['node|article|default'] = $dslayout;
  90. return $dslayouts;
  91. }
  92. /**
  93. * Expose default view modes.
  94. *
  95. * This hook is called by CTools. For this hook to work, you need
  96. * hook_ctools_plugin_api(). The values of this hook can be overridden
  97. * and reverted through the UI.
  98. */
  99. function hook_ds_view_modes_info() {
  100. $ds_view_modes = array();
  101. $ds_view_mode = new stdClass;
  102. $ds_view_mode->disabled = FALSE; /* Edit this to true to make a default ds_view_mode disabled initially */
  103. $ds_view_mode->api_version = 1;
  104. $ds_view_mode->view_mode = 'test_exportables';
  105. $ds_view_mode->label = 'Test exportables';
  106. $ds_view_mode->entities = array(
  107. 'node' => 'node',
  108. );
  109. $ds_view_modes['test_exportables'] = $ds_view_mode;
  110. return $ds_view_modes;
  111. }
  112. /**
  113. * Define fields. These fields are not overridable through the interface.
  114. * If you want those, look at hook_ds_custom_fields_info().
  115. *
  116. * @param $entity_type
  117. * The name of the entity which we are requesting fields for, e.g. 'node'.
  118. *
  119. * @return $fields
  120. * A collection of fields which keys are the entity type name and values
  121. * a collection fields.
  122. *
  123. * @see ds_get_fields()
  124. */
  125. function hook_ds_fields_info($entity_type) {
  126. $fields = array();
  127. $fields['title'] = array(
  128. // title: title of the field
  129. 'title' => t('Title'),
  130. // type: type of field
  131. // - DS_FIELD_TYPE_THEME : calls a theming function.
  132. // - DS_FIELD_TYPE_FUNCTION : calls a custom function.
  133. // - DS_FIELD_TYPE_CODE : calls ds_render_code_field().
  134. // - DS_FIELD_TYPE_BLOCK : calls ds_render_block_field().
  135. // - DS_FIELD_TYPE_PREPROCESS : calls nothing, just takes a key from the
  136. // variable field that is passed on.
  137. // - DS_FIELD_TYPE_IGNORE : calls nothing, use this if you simple want
  138. // to drag and drop. The field itself will have
  139. // a theme function.
  140. 'field_type' => DS_FIELD_TYPE_FUNCTION,
  141. // ui_limit : only used for the manage display screen so
  142. // you can limit fields to show based on bundles or view modes
  143. // the values are always in the form of $bundle|$view_mode
  144. // You may use * to select all.
  145. // Make sure you use the machine name.
  146. 'ui_limit' => array('article|full', '*|search_index'),
  147. // file: an optional file in which the function resides.
  148. // Only for DS_FIELD_TYPE_FUNCTION.
  149. 'file' => 'optional_filename',
  150. // function: only for DS_FIELD_TYPE_FUNCTION.
  151. 'function' => 'theme_ds_title_field',
  152. // properties: can have different keys.
  153. 'properties' => array(
  154. // formatters: optional if a function is used.
  155. // In case the field_type is DS_FIELD_TYPE_THEME, you also
  156. // need to register these formatters as a theming function
  157. // since the key will be called with theme('function').
  158. // The value is the caption used in the selection config on Field UI.
  159. 'formatters' => array(
  160. 'node_title_nolink_h1' => t('H1 title'),
  161. 'node_title_link_h1' => t('H1 title, linked to node'),
  162. ),
  163. // settings & default: optional if you have a settings form for your field.
  164. 'settings' => array(
  165. 'wrapper' => array('type' => 'textfield', 'description' => t('Eg: h1, h2, p')),
  166. 'link' => array('type' => 'select', 'options' => array('yes', 'no')),
  167. ),
  168. 'default' => array('wrapper' => 'h2', 'link' => 0),
  169. // code: optional, only for code field.
  170. 'code' => 'my code here',
  171. // use_token: optional, only for code field.
  172. 'use_token' => TRUE, // or FALSE,
  173. // block: the module and delta of the block, only for block fields.
  174. //
  175. // @note: Display Suite uses a "|" token to split the module from
  176. // the delta.
  177. 'block' => 'user|menu',
  178. // block_render: block render type, only for block fields.
  179. // - DS_BLOCK_TEMPLATE : render through block template file.
  180. // - DS_BLOCK_TITLE_CONTENT : render only title and content.
  181. // - DS_BLOCK_CONTENT : render only content.
  182. 'block_render' => DS_BLOCK_CONTENT,
  183. )
  184. );
  185. return array('node' => $fields);
  186. }
  187. /**
  188. * Define custom fields which can be overridden through the UI and which
  189. * are exportable. The keys are almost the same as in hook_ds_fields_info()
  190. * except that field_type is limited and you need an entities key.
  191. *
  192. * This hook is called by CTools. For this hook to work, you need
  193. * hook_ctools_plugin_api(). The values of this hook can be overridden
  194. * and reverted through the UI.
  195. */
  196. function hook_ds_custom_fields_info() {
  197. $ds_fields = array();
  198. $ds_field = new stdClass;
  199. $ds_field->api_version = 1;
  200. $ds_field->field = 'custom_field';
  201. $ds_field->label = 'Custom field';
  202. // Field type: either block or code
  203. // DS_FIELD_TYPE_CODE: 5
  204. // DS_FIELD_TYPE_BLOCK: 6
  205. $ds_field->field_type = 5;
  206. // Collection of entities on which this custom field can work on.
  207. $ds_field->entities = array(
  208. 'node' => 'node',
  209. );
  210. $ds_field->properties = array(
  211. 'code' => array(
  212. 'value' => '<?php print "this is a custom field"; ?>',
  213. 'format' => 'ds_code',
  214. ),
  215. 'use_token' => 0,
  216. );
  217. $ds_fields['custom_field'] = $ds_field;
  218. return $ds_fields;
  219. }
  220. /**
  221. * Expose Views layouts definitions.
  222. *
  223. * This hook is called by CTools. For this hook to work, you need
  224. * hook_ctools_plugin_api(). The values of this hook can be overridden
  225. * and reverted through the UI.
  226. */
  227. function hook_ds_vd_info() {
  228. $ds_vds = array();
  229. $ds_vd = new stdClass;
  230. $ds_vd->api_version = 1;
  231. $ds_vd->vd = 'frontpage-page';
  232. $ds_vd->label = 'Frontpage: Views displays';
  233. $ds_vds['frontpage-page'] = $ds_vd;
  234. return $ds_vds;
  235. }
  236. /**
  237. * Alter fields defined by Display Suite.
  238. *
  239. * This function is called for each entity type.
  240. *
  241. * @param $fields
  242. * An array with fields which can be altered just before they get cached.
  243. * @param $entity_type
  244. * The name of the entity type.
  245. */
  246. function hook_ds_fields_info_alter(&$fields, $entity_type) {
  247. if (isset($fields['title'])) {
  248. $fields['title']['title'] = t('My title');
  249. }
  250. }
  251. /**
  252. * Alter fields defined by Display Suite just before they get
  253. * rendered on the Field UI. Use this hook to inject fields
  254. * which you can't alter with hook_ds_fields_info_alter().
  255. *
  256. * Use this in edge cases, see ds_extras_ds_fields_ui_alter()
  257. * which adds fields chosen in Views UI. This also runs
  258. * when a layout has been chosen.
  259. *
  260. * @param $fields
  261. * An array with fields which can be altered just before they get cached.
  262. * @param $entity_type
  263. * The name of the entity type.
  264. */
  265. function hook_ds_fields_ui_alter(&$fields, $context) {
  266. $fields['title'] = t('Extra title');
  267. }
  268. /**
  269. * Define theme functions for fields.
  270. *
  271. * This only is necessary when you're using the field settings
  272. * plugin which comes with the DS extras module and you want to
  273. * expose a special field theming function to the interface.
  274. *
  275. * The theme function gets $variables as the only parameter.
  276. * The optional configuration through the UI is in $variables['ds-config'].
  277. *
  278. * Note that 'theme_ds_field_' is always needed, so the suggestions can work.
  279. *
  280. * @return $field_theme_functions
  281. * A collection of field theming functions.
  282. */
  283. function hook_ds_field_theme_functions_info() {
  284. return array('theme_ds_field_mine' => t('Theme field'));
  285. }
  286. /**
  287. * Return configuration summary for the field format.
  288. *
  289. * As soon as you have hook_ds_fields and one of the fields
  290. * has a settings key, Display Suite will call this hook for the summary.
  291. *
  292. * @param $field
  293. * The configuration of the field.
  294. *
  295. * @return $summary
  296. * The summary to show on the Field UI.
  297. */
  298. function hook_ds_field_format_summary($field) {
  299. return 'Field summary';
  300. }
  301. /**
  302. * Return a settings form for a Display Suite field.
  303. *
  304. * As soon as you have hook_ds_fields and one of the fields
  305. * has a settings key, Display Suite will call this hook for field form.
  306. *
  307. * @param $field
  308. * The configuration of the field.
  309. *
  310. * @return $form
  311. * A form definition.
  312. */
  313. function hook_ds_field_settings_form($field) {
  314. // Saved formatter settings are on $field['formatter_settings'];
  315. $settings = isset($field['formatter_settings']) ? $field['formatter_settings'] : $field['properties']['default'];
  316. $form['label'] = array(
  317. '#type' => 'textfield',
  318. '#title' => t('Label'),
  319. '#default_value' => $settings['label'],
  320. );
  321. }
  322. /**
  323. * Modify the layout settings just before they get saved.
  324. *
  325. * @param $record
  326. * The record just before it gets saved into the database.
  327. * @param $form_state
  328. * The form_state values.
  329. */
  330. function hook_ds_layout_settings_alter($record, $form_state) {
  331. $record->settings['hide_page_title'] = TRUE;
  332. }
  333. /**
  334. * Modify the field settings before they get saved.
  335. *
  336. * @param $field_settings
  337. * A collection of field settings which keys are fields.
  338. * @param $form
  339. * The current form which is submitted.
  340. * @param $form_state
  341. * The form state with all its values.
  342. */
  343. function hook_ds_field_settings_alter(&$field_settings, $form, $form_state) {
  344. $field_settings['title']['region'] = 'left';
  345. }
  346. /**
  347. * Define layouts from code.
  348. *
  349. * @return $layouts
  350. * A collection of layouts.
  351. */
  352. function hook_ds_layout_info() {
  353. $path = drupal_get_path('module', 'foo');
  354. $layouts = array(
  355. 'foo_1col' => array(
  356. 'label' => t('Foo one column'),
  357. 'path' => $path . '/layouts/foo_1col',
  358. 'regions' => array(
  359. 'foo_content' => t('Content'),
  360. ),
  361. 'css' => TRUE,
  362. // optional, form only applies to node form at this point.
  363. 'form' => TRUE,
  364. ),
  365. );
  366. return $layouts;
  367. }
  368. /**
  369. * Alter the layout render array.
  370. *
  371. * @param $layout_render_array
  372. * The render array
  373. * @param $context
  374. * An array with the context that is being rendered. Available keys are
  375. * - entity
  376. * - entity_type
  377. * - bundle
  378. * - view_mode
  379. * @param array $vars
  380. * All variables available for render. You can use this to add css classes.
  381. */
  382. function hook_ds_pre_render_alter(&$layout_render_array, $context, &$vars) {
  383. $layout_render_array['left'][] = array('#markup' => 'cool!', '#weight' => 20);
  384. $vars['attributes_array']['class'][] = 'custom';
  385. }
  386. /**
  387. * Alter layouts found by Display Suite.
  388. *
  389. * @param $layouts
  390. * A array of layouts which keys are the layout and which values are
  391. * properties of that layout (label, path, regions and css).
  392. */
  393. function hook_ds_layout_info_alter(&$layouts) {
  394. unset($layouts['ds_2col']);
  395. }
  396. /**
  397. * Alter the region options in the field UI screen.
  398. *
  399. * This function is only called when a layout has been chosen.
  400. *
  401. * @param $context
  402. * A collection of keys for the context. The keys are 'entity_type',
  403. * 'bundle' and 'view_mode'.
  404. * @param $region_info
  405. * A collection of info for regions. The keys are 'region_options'
  406. * and 'table_regions'.
  407. */
  408. function hook_ds_layout_region_alter($context, &$region_info) {
  409. $region_info['region_options'][$block_key] = $block['title'];
  410. $region_info['table_regions'][$block_key] = array(
  411. 'title' => check_plain($block['title']),
  412. 'message' => t('No fields are displayed in this region'),
  413. );
  414. }
  415. /**
  416. * Alter the field label options. Note that you will either
  417. * update the preprocess functions or the field.tpl.php file when
  418. * adding new options.
  419. *
  420. * @param $field_label_options
  421. * A collection of field label options.
  422. */
  423. function hook_ds_label_options_alter(&$field_label_options) {
  424. $field_label_options['label_after'] = t('Label after field');
  425. }
  426. /**
  427. * Themes can also define extra layouts.
  428. *
  429. * Create a ds_layouts folder and then a folder name that will
  430. * be used as key for the layout. The folder should at least have 2 files:
  431. *
  432. * - key.inc
  433. * - key.tpl.php
  434. *
  435. * The css file is optional.
  436. * - key.css
  437. *
  438. * e.g.
  439. * bartik/ds_layouts/bartik_ds/bartik_ds.inc
  440. * /bartik-ds.tpl.php
  441. * /bartik_ds.css
  442. *
  443. * bartik_ds.inc must look like this:
  444. *
  445. // Fuction name is ds_LAYOUT_KEY
  446. function ds_bartik_ds() {
  447. return array(
  448. 'label' => t('Bartik DS'),
  449. 'regions' => array(
  450. // The key of this region name is also the variable used in
  451. // the template to print the content of that region.
  452. 'bartik' => t('Bartik DS'),
  453. ),
  454. // Add this if there is a default css file.
  455. 'css' => TRUE,
  456. // Add this if there is a default preview image
  457. 'image' => TRUE,
  458. );
  459. }
  460. */
  461. /**
  462. * Alter the view mode just before it's rendered by the DS views entity plugin.
  463. *
  464. * @param $view_mode
  465. * The name of the view mode.
  466. * @param $context
  467. * A collection of items which can be used to identify in what
  468. * context an entity is being rendered. The variable contains 3 keys:
  469. * - entity: The entity being rendered.
  470. * - view_name: the name of the view.
  471. * - display: the name of the display of the view.
  472. */
  473. function hook_ds_views_view_mode_alter(&$view_mode, $context) {
  474. if ($context['view_name'] == 'my_view_name') {
  475. $view_mode = 'new_view_mode';
  476. }
  477. }
  478. /**
  479. * Theme an entity coming from the views entity plugin.
  480. *
  481. * @param $entity
  482. * The complete entity.
  483. * @param $view_mode
  484. * The name of the view mode.
  485. */
  486. function ds_views_row_ENTITY_NAME($entity, $view_mode) {
  487. $nid = $vars['row']->{$vars['field_alias']};
  488. $node = node_load($nid);
  489. $element = node_view($node, $view_mode);
  490. return drupal_render($element);
  491. }
  492. /**
  493. * Theme an entity through an advanced function coming from the views entity plugin.
  494. *
  495. * @param $vars
  496. * An array of variables from the views preprocess functions.
  497. * @param $view_mode
  498. * The name of the view mode.
  499. */
  500. function ds_views_row_adv_VIEWS_NAME(&$vars, $view_mode) {
  501. // You can do whatever you want to here.
  502. $vars['object'] = 'This is what I want for christmas.';
  503. }
  504. /**
  505. * Modify the entity render array in the context of a view.
  506. *
  507. * @param array $content
  508. * By reference. An entity view render array.
  509. * @param array $context
  510. * By reference. An associative array containing:
  511. * - row: The current active row object being rendered.
  512. * - view: By reference. The current view object.
  513. * - view_mode: The view mode which is set in the Views' options.
  514. * - load_comments: The same param passed to each row function.
  515. *
  516. * @see ds_views_row_render_entity()
  517. */
  518. function hook_ds_views_row_render_entity_alter(&$content, &$context) {
  519. if ($context['view_mode'] == 'my_mode') {
  520. // Modify the view, or the content render array in the context of a view.
  521. $view = &$context['view'];
  522. $element = &drupal_array_get_nested_value($content, array('field_example', 0));
  523. }
  524. }
  525. /**
  526. * Alter the strings used to separate taxonomy terms.
  527. */
  528. function hook_ds_taxonomy_term_separators(&$separators) {
  529. // Remove the option to use a hyphen.
  530. unset($separators[' - ']);
  531. // Add the option to use a pipe.
  532. $separators[' | '] = t('pipe');
  533. }
  534. /**
  535. * Allow modules to provide additional classes for regions and layouts.
  536. */
  537. function hook_ds_classes_alter(&$classes, $name) {
  538. if ('ds_classes_regions' === $name) {
  539. $classes['css-class-name'] = t('Custom Styling');
  540. }
  541. }
  542. /**
  543. * Alter the field template settings form
  544. *
  545. * @param array $form
  546. * The form containing the field settings
  547. * @param array $field_settings
  548. * The settings of the field
  549. */
  550. function hook_ds_field_theme_functions_settings_alter(&$form, $field_settings) {
  551. $form['something'] = array(
  552. '#type' => 'textfield',
  553. '#title' => 'test',
  554. );
  555. }
  556. /*
  557. * @} End of "addtogroup hooks".
  558. */