summaryrefslogtreecommitdiff
path: root/sites/all/modules/ds/tests/ds_test.module
blob: 2bde1d93416c98d26a138d6520e5be8d15556072 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php

/**
 * @file
 * Display Suite test module.
 */

/**
 * Implements hook_install().
 */
function ds_test_install() {
  variable_set('ds_extras_region_to_block', TRUE);
  variable_set('ds_extras_switch_view_mode', TRUE);
  variable_set('ds_extras_hide_page_title', TRUE);
  variable_set('ds_extras_field_template', TRUE);
  variable_set('ds_extras_fields_extra', TRUE);
  variable_set('ds_extras_hide_page_sidebars', TRUE);
  variable_set('ds_extras_fields_extra_list', "node|article|ds_extras_extra_test_field\nnode|article|ds_extras_second_field");
  variable_set('ds_extras_vd', TRUE);

  db_update('system')
    ->fields(array('weight' => 2))
    ->condition('name', 'ds_test')
    ->execute();
}

/**
 * Implements hook_theme_registry_alter().
 */
function ds_test_theme_registry_alter(&$theme_registry) {

  // Inject ds_entity_variables in all entity theming functions.
  $entity_info = entity_get_info();
  foreach ($entity_info as $entity => $info) {
    if (isset($entity_info[$entity]['fieldable']) && $entity_info[$entity]['fieldable']) {

      // User uses user_profile for theming.
      if ($entity == 'user') $entity = 'user_profile';

      // Only add preprocess functions if entity exposes theme function.
      if (isset($theme_registry[$entity])) {
        $theme_registry[$entity]['preprocess functions'][] = 'ds_test_entity_variables';
      }
    }
  }

  // Support for File Entity.
  if (isset($theme_registry['file_entity'])) {
    $theme_registry['file_entity']['preprocess functions'][] = 'ds_test_entity_variables';
  }

  // Support for Entity API.
  if (isset($theme_registry['entity'])) {
    $theme_registry['entity']['preprocess functions'][] = 'ds_test_entity_variables';
  }
}

/**
 * Theming function, added after ds_entity_variables().
 *
 * @param $variables
 */
function ds_test_entity_variables(&$variables) {
  if (isset($_GET['store'])) {
    cache_set('ds_test', $variables);
  }
}

/**
 * Implements hook_views_api().
 */
function ds_test_views_api($module, $api) {
  if ($module == 'views' && $api == 'views_default') {
    return array('version' => 3);
  }
}

/**
 * Helper function to return the tag name basid on tid.
 */
function ds_test_get_tag_name($raw_value, $object) {
  $term = taxonomy_term_load($raw_value);
  return $term->name;
}

/**
 * Helper function to return advanced view mode.
 */
function ds_views_row_adv_ds_testing($entity, $view_mode, $load_comments) {
  return 'Advanced display for id ' . $entity->nid;
}

/**
 * Implements hook_field_extra_fields().
 */
function ds_test_field_extra_fields() {
  $extra = array();

  // Register a single field to test that
  // extra fields in the hidden region are really hidden.
  $extra['node']['article']['display']['heavy_field'] = array(
   'label' => t('Heavy extra test field'),
   'weight' => 10,
  );

  return $extra;
}

/**
 * Implements hook_node_view().
 */
function ds_test_node_view($node, $view_mode, $langcode) {
  $node->content['ds_extras_extra_test_field'] = array(
    '#markup' => 'This is an extra field made available through "Extra fields" functionality.',
    '#weight' => 10,
  );

  // Check whether the heavy extra field is rendered or not.
  if ($node->type == 'article') {
    $fields = field_extra_fields_get_display('node', 'article', $view_mode);
    if (isset($fields['heavy_field']) && $fields['heavy_field']['visible']) {
      $node->content['heavy_field'] = array(
        '#markup' => 'Heavy field',
        '#weight' => $fields['heavy_field']['weight'],
      );
    }
  }
}

/**
 * Implements hook_ds_layout_info_alter().
 */
function ds_test_ds_layout_info_alter(&$layouts) {
  unset($layouts['ds_3col_stacked_equal_width']);
}

/**
 * Implements hook_ds_fields_info().
 */
function ds_test_ds_fields_info($entity_type) {
  if ($entity_type == 'node') {
    $fields['node']['ds_test_field'] = array(
      'title' => t('Test code field from hook'),
      'field_type' => DS_FIELD_TYPE_FUNCTION,
      'function' => 'dstest_render_test_field',
    );
    $fields['node']['ds_test_field_2'] = array(
      'title' => t('Test code field from hook 2'),
      'field_type' => DS_FIELD_TYPE_FUNCTION,
      'function' => 'dstest_render_test_field',
    );
    $fields['node']['ds_test_field_empty_string'] = array(
      'title' => t('Test code field that returns an empty string'),
      'field_type' => DS_FIELD_TYPE_FUNCTION,
      'function' => 'dstest_render_empty_string',
    );
    $fields['node']['ds_test_field_false'] = array(
      'title' => t('Test code field that returns FALSE'),
      'field_type' => DS_FIELD_TYPE_FUNCTION,
      'function' => 'dstest_render_false',
    );
    $fields['node']['ds_test_field_null'] = array(
      'title' => t('Test code field that returns NULL'),
      'field_type' => DS_FIELD_TYPE_FUNCTION,
      'function' => 'dstest_render_null',
    );
    $fields['node']['ds_test_field_nothing'] = array(
      'title' => t('Test code field that returns nothing'),
      'field_type' => DS_FIELD_TYPE_FUNCTION,
      'function' => 'dstest_render_nothing',
    );
    $fields['node']['ds_test_field_zero_int'] = array(
      'title' => t('Test code field that returns zero as an integer'),
      'field_type' => DS_FIELD_TYPE_FUNCTION,
      'function' => 'dstest_render_zero_int',
    );
    $fields['node']['ds_test_field_zero_string'] = array(
      'title' => t('Test code field that returns zero as a string'),
      'field_type' => DS_FIELD_TYPE_FUNCTION,
      'function' => 'dstest_render_zero_string',
    );
    $fields['node']['ds_test_field_zero_float'] = array(
      'title' => t('Test code field that returns zero as a floating point number'),
      'field_type' => DS_FIELD_TYPE_FUNCTION,
      'function' => 'dstest_render_zero_float',
    );

    return $fields;
  }
}

/**
 * Implements hook_ds_field_theme_functions_info().
 */
function ds_test_ds_field_theme_functions_info() {
  return array('ds_test_theming_function' => t('Field test function'));
}

/**
 * Theme field test function.
 */
function ds_test_theming_function($variables) {
  return 'Testing field output through custom function';
}

/**
 * Render the test code field.
 */
function dstest_render_test_field($field) {
  return 'Test code field on node ' . $field['entity']->nid;
}

/**
 * Test code field that returns an empty string.
 */
function dstest_render_empty_string() {
  return '';
}

/**
 * Test code field that returns FALSE.
 */
function dstest_render_false() {
  return FALSE;
}

/**
 * Test code field that returns NULL.
 */
function dstest_render_null() {
  return NULL;
}

/**
 * Test code field that returns nothing.
 */
function dstest_render_nothing() {
  return;
}

/**
 * Test code field that returns zero as an integer.
 */
function dstest_render_zero_int() {
  return 0;
}

/**
 * Test code field that returns zero as a string.
 */
function dstest_render_zero_string() {
  return '0';
}

/**
 * Test code field that returns zero as a floating point number.
 */
function dstest_render_zero_float() {
  return 0.0;
}

/**
 * Implements hook_ds_fields_info_alter().
 */
function ds_test_ds_fields_info_alter(&$fields, $entity_type) {
  if (isset($fields['ds_test_field_2'])) {
    $fields['ds_test_field_2']['title'] = 'Field altered';
  }
}

/**
 * Implements hook_ds_layouts_info().
 */
function ds_test_ds_layout_info() {
  $path = drupal_get_path('module', 'ds_test');
  $layouts = array(
    'dstest_1col' => array(
      'label' => t('Test One column'),
      'path' => $path . '/dstest_1col',
      'regions' => array(
        'ds_content' => t('Content'),
      ),
    ),
    'dstest_2col' => array(
      'label' => t('Test Two column'),
      'path' => $path . '/dstest_2col',
      'regions' => array(
        'left' => t('Left'),
        'right' => t('Right')
      ),
      'css' => TRUE,
    ),
  );

  return $layouts;
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function ds_test_form_field_ui_display_overview_form_alter(&$form, &$form_state) {
  foreach (element_children($form['fields']) as $key) {
    if (isset($form['fields'][$key]['settings_edit'])) {
      $settings = $form['fields'][$key]['settings_edit'];
      if (!empty($settings)) {
        $form['fields'][$key]['settings_edit']['#type'] = 'submit';
        $form['fields'][$key]['settings_edit']['#value'] = 'edit ' . $key;
      }
    }
  }
}

/**
 * Implements hook_ds_pre_render_alter().
 */
function ds_test_ds_pre_render_alter(&$layout_render_array, $context) {
  $entity = $context['entity'];
  if (isset($entity->title) && $entity->title === 'Alter me!') {
    $layout_render_array['left'][] = array('#markup' => 'cool!', '#weight' => 20);
  }
}