file_module_test.module 2.22 KB
Newer Older
1
2
3
4
5
6
7
8
<?php

/**
 * @file
 * Provides File module pages for testing purposes.
 */

/**
9
 * Implements hook_menu().
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 */
function file_module_test_menu() {
  $items = array();

  $items['file/test'] = array(
    'title' => 'Managed file test',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('file_module_test_form'),
    'access arguments' => array('access content'),
  );

  return $items;
}

24
/**
25
26
27
28
29
30
31
 * Form constructor for testing a 'managed_file' element.
 *
 * Path: file/test
 *
 * @see file_module_test_menu()
 * @see file_module_test_form_submit()
 * @ingroup forms
32
33
34
 */
function file_module_test_form($form, &$form_state, $tree = TRUE, $extended = FALSE, $default_fid = NULL) {
  $form['#tree'] = (bool) $tree;
35

36
  $form['nested']['file'] = array(
37
38
39
40
    '#type' => 'managed_file',
    '#title' => t('Managed file'),
    '#upload_location' => 'public://test',
    '#progress_message' => t('Please wait...'),
41
    '#extended' => (bool) $extended,
42
  );
43
44
45
  if ($default_fid) {
    $form['nested']['file']['#default_value'] = $extended ? array('fid' => $default_fid) : $default_fid;
  }
46
47
48
49
50
51
52
53
54
55
56
57
58

  $form['textfield'] = array(
    '#type' => 'textfield',
    '#title' => t('Type a value and ensure it stays'),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );

  return $form;
}
59
60
61
62
63
64
65
66
67
68
69
70
71

/**
 * Form submission handler for file_module_test_form().
 */
function file_module_test_form_submit($form, &$form_state) {
  if ($form['#tree']) {
    $fid = $form['nested']['file']['#extended'] ? $form_state['values']['nested']['file']['fid'] : $form_state['values']['nested']['file'];
  }
  else {
    $fid = $form['nested']['file']['#extended'] ? $form_state['values']['file']['fid'] : $form_state['values']['file'];
  }
  drupal_set_message(t('The file id is %fid.', array('%fid' => $fid)));
}
72
73
74
75
76
77
78
79
80
81
82
83
84

/**
 * Implements hook_file_download_access().
 */
function file_module_test_file_download_access($field, $entity_type, $entity) {
  list(,, $bundle) = entity_extract_ids($entity_type, $entity);
  $instance = field_info_instance($entity_type, $field['field_name'], $bundle);
  // Allow the file to be downloaded only if the given arguments are correct.
  // If any are wrong, $instance will be NULL.
  if (empty($instance)) {
    return FALSE;
  }
}