ds_test.module 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. /**
  3. * @file
  4. * Display Suite test module.
  5. */
  6. /**
  7. * Implements hook_install().
  8. */
  9. function ds_test_install() {
  10. variable_set('ds_extras_region_to_block', TRUE);
  11. variable_set('ds_extras_switch_view_mode', TRUE);
  12. variable_set('ds_extras_hide_page_title', TRUE);
  13. variable_set('ds_extras_field_template', TRUE);
  14. variable_set('ds_extras_fields_extra', TRUE);
  15. variable_set('ds_extras_hide_page_sidebars', TRUE);
  16. variable_set('ds_extras_fields_extra_list', "node|article|ds_extras_extra_test_field\nnode|article|ds_extras_second_field");
  17. variable_set('ds_extras_vd', TRUE);
  18. db_update('system')
  19. ->fields(array('weight' => 2))
  20. ->condition('name', 'ds_test')
  21. ->execute();
  22. }
  23. /**
  24. * Implements hook_theme_registry_alter().
  25. */
  26. function ds_test_theme_registry_alter(&$theme_registry) {
  27. // Inject ds_entity_variables in all entity theming functions.
  28. $entity_info = entity_get_info();
  29. foreach ($entity_info as $entity => $info) {
  30. if (isset($entity_info[$entity]['fieldable']) && $entity_info[$entity]['fieldable']) {
  31. // User uses user_profile for theming.
  32. if ($entity == 'user') $entity = 'user_profile';
  33. // Only add preprocess functions if entity exposes theme function.
  34. if (isset($theme_registry[$entity])) {
  35. $theme_registry[$entity]['preprocess functions'][] = 'ds_test_entity_variables';
  36. }
  37. }
  38. }
  39. // Support for File Entity.
  40. if (isset($theme_registry['file_entity'])) {
  41. $theme_registry['file_entity']['preprocess functions'][] = 'ds_test_entity_variables';
  42. }
  43. // Support for Entity API.
  44. if (isset($theme_registry['entity'])) {
  45. $theme_registry['entity']['preprocess functions'][] = 'ds_test_entity_variables';
  46. }
  47. }
  48. /**
  49. * Theming function, added after ds_entity_variables().
  50. *
  51. * @param $variables
  52. */
  53. function ds_test_entity_variables(&$variables) {
  54. if (isset($_GET['store'])) {
  55. cache_set('ds_test', $variables);
  56. }
  57. }
  58. /**
  59. * Implements hook_views_api().
  60. */
  61. function ds_test_views_api($module, $api) {
  62. if ($module == 'views' && $api == 'views_default') {
  63. return array('version' => 3);
  64. }
  65. }
  66. /**
  67. * Helper function to return the tag name basid on tid.
  68. */
  69. function ds_test_get_tag_name($raw_value, $object) {
  70. $term = taxonomy_term_load($raw_value);
  71. return $term->name;
  72. }
  73. /**
  74. * Helper function to return advanced view mode.
  75. */
  76. function ds_views_row_adv_ds_testing($entity, $view_mode, $load_comments) {
  77. return 'Advanced display for id ' . $entity->nid;
  78. }
  79. /**
  80. * Implements hook_field_extra_fields().
  81. */
  82. function ds_test_field_extra_fields() {
  83. $extra = array();
  84. // Register a single field to test that
  85. // extra fields in the hidden region are really hidden.
  86. $extra['node']['article']['display']['heavy_field'] = array(
  87. 'label' => t('Heavy extra test field'),
  88. 'weight' => 10,
  89. );
  90. return $extra;
  91. }
  92. /**
  93. * Implements hook_node_view().
  94. */
  95. function ds_test_node_view($node, $view_mode, $langcode) {
  96. $node->content['ds_extras_extra_test_field'] = array(
  97. '#markup' => 'This is an extra field made available through "Extra fields" functionality.',
  98. '#weight' => 10,
  99. );
  100. // Check whether the heavy extra field is rendered or not.
  101. if ($node->type == 'article') {
  102. $fields = field_extra_fields_get_display('node', 'article', $view_mode);
  103. if (isset($fields['heavy_field']) && $fields['heavy_field']['visible']) {
  104. $node->content['heavy_field'] = array(
  105. '#markup' => 'Heavy field',
  106. '#weight' => $fields['heavy_field']['weight'],
  107. );
  108. }
  109. }
  110. }
  111. /**
  112. * Implements hook_ds_layout_info_alter().
  113. */
  114. function ds_test_ds_layout_info_alter(&$layouts) {
  115. unset($layouts['ds_3col_stacked_equal_width']);
  116. }
  117. /**
  118. * Implements hook_ds_fields_info().
  119. */
  120. function ds_test_ds_fields_info($entity_type) {
  121. if ($entity_type == 'node') {
  122. $fields['node']['ds_test_field'] = array(
  123. 'title' => t('Test code field from hook'),
  124. 'field_type' => DS_FIELD_TYPE_FUNCTION,
  125. 'function' => 'dstest_render_test_field',
  126. );
  127. $fields['node']['ds_test_field_2'] = array(
  128. 'title' => t('Test code field from hook 2'),
  129. 'field_type' => DS_FIELD_TYPE_FUNCTION,
  130. 'function' => 'dstest_render_test_field',
  131. );
  132. $fields['node']['ds_test_field_empty_string'] = array(
  133. 'title' => t('Test code field that returns an empty string'),
  134. 'field_type' => DS_FIELD_TYPE_FUNCTION,
  135. 'function' => 'dstest_render_empty_string',
  136. );
  137. $fields['node']['ds_test_field_false'] = array(
  138. 'title' => t('Test code field that returns FALSE'),
  139. 'field_type' => DS_FIELD_TYPE_FUNCTION,
  140. 'function' => 'dstest_render_false',
  141. );
  142. $fields['node']['ds_test_field_null'] = array(
  143. 'title' => t('Test code field that returns NULL'),
  144. 'field_type' => DS_FIELD_TYPE_FUNCTION,
  145. 'function' => 'dstest_render_null',
  146. );
  147. $fields['node']['ds_test_field_nothing'] = array(
  148. 'title' => t('Test code field that returns nothing'),
  149. 'field_type' => DS_FIELD_TYPE_FUNCTION,
  150. 'function' => 'dstest_render_nothing',
  151. );
  152. $fields['node']['ds_test_field_zero_int'] = array(
  153. 'title' => t('Test code field that returns zero as an integer'),
  154. 'field_type' => DS_FIELD_TYPE_FUNCTION,
  155. 'function' => 'dstest_render_zero_int',
  156. );
  157. $fields['node']['ds_test_field_zero_string'] = array(
  158. 'title' => t('Test code field that returns zero as a string'),
  159. 'field_type' => DS_FIELD_TYPE_FUNCTION,
  160. 'function' => 'dstest_render_zero_string',
  161. );
  162. $fields['node']['ds_test_field_zero_float'] = array(
  163. 'title' => t('Test code field that returns zero as a floating point number'),
  164. 'field_type' => DS_FIELD_TYPE_FUNCTION,
  165. 'function' => 'dstest_render_zero_float',
  166. );
  167. return $fields;
  168. }
  169. }
  170. /**
  171. * Implements hook_ds_field_theme_functions_info().
  172. */
  173. function ds_test_ds_field_theme_functions_info() {
  174. return array('ds_test_theming_function' => t('Field test function'));
  175. }
  176. /**
  177. * Theme field test function.
  178. */
  179. function ds_test_theming_function($variables) {
  180. return 'Testing field output through custom function';
  181. }
  182. /**
  183. * Render the test code field.
  184. */
  185. function dstest_render_test_field($field) {
  186. return 'Test code field on node ' . $field['entity']->nid;
  187. }
  188. /**
  189. * Test code field that returns an empty string.
  190. */
  191. function dstest_render_empty_string() {
  192. return '';
  193. }
  194. /**
  195. * Test code field that returns FALSE.
  196. */
  197. function dstest_render_false() {
  198. return FALSE;
  199. }
  200. /**
  201. * Test code field that returns NULL.
  202. */
  203. function dstest_render_null() {
  204. return NULL;
  205. }
  206. /**
  207. * Test code field that returns nothing.
  208. */
  209. function dstest_render_nothing() {
  210. return;
  211. }
  212. /**
  213. * Test code field that returns zero as an integer.
  214. */
  215. function dstest_render_zero_int() {
  216. return 0;
  217. }
  218. /**
  219. * Test code field that returns zero as a string.
  220. */
  221. function dstest_render_zero_string() {
  222. return '0';
  223. }
  224. /**
  225. * Test code field that returns zero as a floating point number.
  226. */
  227. function dstest_render_zero_float() {
  228. return 0.0;
  229. }
  230. /**
  231. * Implements hook_ds_fields_info_alter().
  232. */
  233. function ds_test_ds_fields_info_alter(&$fields, $entity_type) {
  234. if (isset($fields['ds_test_field_2'])) {
  235. $fields['ds_test_field_2']['title'] = 'Field altered';
  236. }
  237. }
  238. /**
  239. * Implements hook_ds_layouts_info().
  240. */
  241. function ds_test_ds_layout_info() {
  242. $path = drupal_get_path('module', 'ds_test');
  243. $layouts = array(
  244. 'dstest_1col' => array(
  245. 'label' => t('Test One column'),
  246. 'path' => $path . '/dstest_1col',
  247. 'regions' => array(
  248. 'ds_content' => t('Content'),
  249. ),
  250. ),
  251. 'dstest_2col' => array(
  252. 'label' => t('Test Two column'),
  253. 'path' => $path . '/dstest_2col',
  254. 'regions' => array(
  255. 'left' => t('Left'),
  256. 'right' => t('Right')
  257. ),
  258. 'css' => TRUE,
  259. ),
  260. );
  261. return $layouts;
  262. }
  263. /**
  264. * Implements hook_form_FORM_ID_alter().
  265. */
  266. function ds_test_form_field_ui_display_overview_form_alter(&$form, &$form_state) {
  267. foreach (element_children($form['fields']) as $key) {
  268. if (isset($form['fields'][$key]['settings_edit'])) {
  269. $settings = $form['fields'][$key]['settings_edit'];
  270. if (!empty($settings)) {
  271. $form['fields'][$key]['settings_edit']['#type'] = 'submit';
  272. $form['fields'][$key]['settings_edit']['#value'] = 'edit ' . $key;
  273. }
  274. }
  275. }
  276. }
  277. /**
  278. * Implements hook_ds_pre_render_alter().
  279. */
  280. function ds_test_ds_pre_render_alter(&$layout_render_array, $context) {
  281. $entity = $context['entity'];
  282. if (isset($entity->title) && $entity->title === 'Alter me!') {
  283. $layout_render_array['left'][] = array('#markup' => 'cool!', '#weight' => 20);
  284. }
  285. }