-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_path.module
More file actions
91 lines (79 loc) · 2.01 KB
/
file_path.module
File metadata and controls
91 lines (79 loc) · 2.01 KB
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
<?php
/**
* @file
* file_path.module
*/
/**
* Implements hook_menu().
*/
function file_path_menu() {
$items = array();
$items['file_path/autocomplete'] = array(
'title' => 'File path auto complete',
'access arguments' => array('access file path auto complete'),
'page callback' => 'file_path_autocomplete',
);
return $items;
}
/**
* Implements hook_permissions().
*/
function file_path_permissions() {
return array(
'access file path auto complete' => array(
'title' => t('Grant access to file path auto complete'),
),
);
}
/**
* Implements hook_element_info().
*/
function file_path_element_info() {
$types['file_path'] = array(
'#input' => TRUE,
'#theme' => array('textfield'),
'#size' => 50,
'#autocomplete_path' => 'file_path/autocomplete',
'#attached' => array(
'js' => array(
drupal_get_path('module', 'file_path') . '/js/file_path.js',
array(
'type' => 'setting',
'data' => array('filePath' => array('url' => url('file_path/autocomplete', array('absolute' => TRUE)))),
),
),
),
'#theme_wrappers' => array('form_element'),
);
return $types;
}
/**
* Return the auto complete path.
*/
function file_path_autocomplete($string = '') {
$matches = array();
$string = str_replace("*", "/", $string);
$explode = explode('/', $string);
if (!file_Exists($string)) {
if (count($explode)) {
// The user is the root. Supply the root folder path.
$string = DRUPAL_ROOT;
}
}
try {
foreach (new DirectoryIterator($string) as $fileInfo) {
if ($fileInfo->isDot()) {
continue;
}
$last = end($explode);
if (!empty($last) && strpos($fileInfo->getFilename(), $last) === FALSE) {
continue;
}
$path = count($explode) == 1 ? $fileInfo->getFilename() : $fileInfo->getPathname();
$matches[$path] = $fileInfo->getFilename();
}
drupal_json_output($matches);
} catch (Exception $e) {
drupal_set_message($e->getMessage());
}
}