2008-04-01 (火) 03:32 時点での Upload path - コンテンツタイプ別設定の追加と Token 関連のバグフィクス のリビジョン
コンテンツタイプ毎に異なるアップロードパスを設定するための機能の追加と、Token モジュールで format_date() 処理時に発生するエラーの修正。
※ Patch suggestions for per-nodetype uploadpath redirection without default | drupal.org にある uploadpath.module.20071213b.patch の内容を一部変更しただけです。
コード改変メモ
- パスパターン入力フィールドをデフォルトパスパターン入力フィールドとするためにタイトルを変更、他の設定フォームとの表示順序調整のためにウェイトを追加
- Token モジュール関連のバグフィクスを有効化するためのチェックボックスを追加
- アップロードパスの設定を適用しない(除外する)コンテンツタイプの選択ボックスと折りたたみフィールドセットを追加
- コンテンツタイプ毎のアップロードパス設定フォームと折りたたみフィールドセット
- Token 置換パターン(構文)の表示順序調整のためにウェイトを追加
- 添付ファイル追加時のパス変更処理に関するコードを変更
Index: uploadpath.module
===================================================================
--- uploadpath.module,v 1.1.2.2 2007/06/15 22:44:39 crell
+++ uploadpath.module (patch)
@@ -32,25 +32,79 @@
*/
function uploadpath_admin_settings() {
$form = array();
-
+
+ // デフォルトのパスパターン設定用フォームへ変更
$form['uploadpath_prefix'] = array(
'#type' => 'textfield',
- '#title' => t('Pattern for the file prefix'),
+ '#title' => t('Default pattern for the file path prefix'),
'#description' => t('Specify the pattern to prefix to file names uploaded with the upload module. It will be appended after the site files directory (e.g., files) but before the file name itself. Do not include a leading or trailing slash. Spaces will be converted to underscores to avoid file system issues.'),
'#default_value' => variable_get('uploadpath_prefix', ''),
+ '#weight' => -10,
);
+ // Token のバグフィクス有効化フォーム
+ $form['token_bugfix'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Set node created and updated time, if not set'),
+ '#return_value' => 1,
+ '#default_value' => variable_get('token_bugfix', 0),
+ '#description' => t('This is a bugfix for the issue, [yyyy] etc is not evaluated properly. And this is only useful if you are using date tokens in your paths.'),
+ '#weight' => -9,
+ );
+
+ // パスの変更を除外するコンテンツタイプの選択フォーム
+ $form['node_types_exclude'] = array(
+ '#title' => t('Excluded node types'),
+ '#type' => 'fieldset',
+ '#collapsible' => TRUE,
+ '#collapsed' => TRUE,
+ '#description' => t('Select the node types to exclude from upload path processing'),
+ '#weight' => -8,
+ );
+ $node_types = node_get_types();
+ foreach ($node_types as $type) {
+ $types[$type->type] = $type->name;
+ }
+ $form['node_types_exclude']['uploadpath_excluded_node_types'] = array(
+ '#type' => 'select',
+ '#multiple' => true,
+ '#title' => t('Node types'),
+ '#default_value' => variable_get('uploadpath_excluded_node_types', array()),
+ '#options' => $types,
+ );
+
+ // コンテンツタイプ毎のパス設定フォーム
+ $form['node_types'] = array(
+ '#title' => t('Patterns for each node type'),
+ '#type' => 'fieldset',
+ '#collapsible' => TRUE,
+ '#collapsed' => TRUE,
+ '#description' => t('Patterns for node types. If empty, the default pattern will be used.'),
+ '#weight' => -7,
+ );
+ foreach ($node_types as $type) {
+ if (!in_array($type->type, variable_get('uploadpath_excluded_node_types', array()))) {
+ $form['node_types']['uploadpath_prefix_'.$type->type] = array(
+ '#type' => 'textfield',
+ '#title' => t('Path pattern for %type', array('%type' => $type->name)),
+ '#description' => t('Specify the path pattern to prefix for %type type nodes', array('%type' => $type->name)),
+ '#default_value' => variable_get('uploadpath_prefix_'.$type->type, ''),
+ );
+ }
+ }
+
$form['token_help'] = array(
'#title' => t('Replacement patterns'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('Prefer raw-text replacements for text to avoid problems with HTML entities!'),
+ '#weight' => -6, // Token 構文ヘルプのウェイト調整
);
$form['token_help']['help'] = array(
'#value' => theme('token_help', 'node'),
);
-
+
return system_settings_form($form);
}
@@ -60,21 +114,40 @@
function uploadpath_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case 'submit':
- if (isset($node->files)) {
- foreach ($node->files as $key => $file) {
- if (0 === strpos($key, 'upload_')) { // Only rewrite the name when adding the file, not when updating it
- // Get the new, prefixed file name
- $file_name = str_replace(array(' ', "\n", "\t"), '_', token_replace(variable_get('uploadpath_prefix', '') . '/', 'node', $node)) . $node->files[$key]['filename'];
-
- // Create the directory if it doesn't exist yet.
- $dirs = explode('/', dirname($file_name));
- $directory = file_directory_path();
- while (count($dirs)) {
- $directory .= '/' . array_shift($dirs);
- file_check_directory($directory, FILE_CREATE_DIRECTORY);
+ // アップロードファイル追加時のパス変更処理コードを変更
+ if (!in_array($node->type, variable_get('uploadpath_excluded_node_types', array()))) {
+ if (isset($node->files)) {
+ foreach ($node->files as $key => $file) {
+ if (0 === strpos($key, 'upload_')) { // Only rewrite the name when adding the file, not when updating it
+
+ // Token bugfix
+ if (variable_get('token_bugfix', false)) {
+ if (!$node->created) {
+ $node->created = (int)time();
+ }
+ if (!$node->changed) {
+ $node->changed = (int)$node->created;
+ }
+ }
+
+ // Get the new, prefixed file name
+ $pattern = variable_get('uploadpath_prefix_'.$node->type, false);
+ if (!$pattern) {
+ $pattern = variable_get('uploadpath_prefix', '');
+ }
+ $file_name = drupal_strtolower(str_replace(array(' ', "\n", "\t"), '_', token_replace($pattern . '/', 'node', $node))) . $node->files[$key]['filename'];
+
+ // Create the directory if it doesn't exist yet.
+ $dirs = explode('/', dirname($file_name));
+ $directory = file_directory_path();
+ while (count($dirs)) {
+ $directory .= '/' . array_shift($dirs);
+ file_check_directory($directory, FILE_CREATE_DIRECTORY);
+ }
+
+ // Change where the file will be saved to the specified directory.
+ $node->files[$key]['filename'] = $file_name;
}
- // Change where the file will be saved to the specified directory.
- $node->files[$key]['filename'] = $file_name;
}
}
}
===================================================================
--- uploadpath.module,v 1.1.2.2 2007/06/15 22:44:39 crell
+++ uploadpath.module (patch)
@@ -32,25 +32,79 @@
*/
function uploadpath_admin_settings() {
$form = array();
-
+
+ // デフォルトのパスパターン設定用フォームへ変更
$form['uploadpath_prefix'] = array(
'#type' => 'textfield',
- '#title' => t('Pattern for the file prefix'),
+ '#title' => t('Default pattern for the file path prefix'),
'#description' => t('Specify the pattern to prefix to file names uploaded with the upload module. It will be appended after the site files directory (e.g., files) but before the file name itself. Do not include a leading or trailing slash. Spaces will be converted to underscores to avoid file system issues.'),
'#default_value' => variable_get('uploadpath_prefix', ''),
+ '#weight' => -10,
);
+ // Token のバグフィクス有効化フォーム
+ $form['token_bugfix'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Set node created and updated time, if not set'),
+ '#return_value' => 1,
+ '#default_value' => variable_get('token_bugfix', 0),
+ '#description' => t('This is a bugfix for the issue, [yyyy] etc is not evaluated properly. And this is only useful if you are using date tokens in your paths.'),
+ '#weight' => -9,
+ );
+
+ // パスの変更を除外するコンテンツタイプの選択フォーム
+ $form['node_types_exclude'] = array(
+ '#title' => t('Excluded node types'),
+ '#type' => 'fieldset',
+ '#collapsible' => TRUE,
+ '#collapsed' => TRUE,
+ '#description' => t('Select the node types to exclude from upload path processing'),
+ '#weight' => -8,
+ );
+ $node_types = node_get_types();
+ foreach ($node_types as $type) {
+ $types[$type->type] = $type->name;
+ }
+ $form['node_types_exclude']['uploadpath_excluded_node_types'] = array(
+ '#type' => 'select',
+ '#multiple' => true,
+ '#title' => t('Node types'),
+ '#default_value' => variable_get('uploadpath_excluded_node_types', array()),
+ '#options' => $types,
+ );
+
+ // コンテンツタイプ毎のパス設定フォーム
+ $form['node_types'] = array(
+ '#title' => t('Patterns for each node type'),
+ '#type' => 'fieldset',
+ '#collapsible' => TRUE,
+ '#collapsed' => TRUE,
+ '#description' => t('Patterns for node types. If empty, the default pattern will be used.'),
+ '#weight' => -7,
+ );
+ foreach ($node_types as $type) {
+ if (!in_array($type->type, variable_get('uploadpath_excluded_node_types', array()))) {
+ $form['node_types']['uploadpath_prefix_'.$type->type] = array(
+ '#type' => 'textfield',
+ '#title' => t('Path pattern for %type', array('%type' => $type->name)),
+ '#description' => t('Specify the path pattern to prefix for %type type nodes', array('%type' => $type->name)),
+ '#default_value' => variable_get('uploadpath_prefix_'.$type->type, ''),
+ );
+ }
+ }
+
$form['token_help'] = array(
'#title' => t('Replacement patterns'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('Prefer raw-text replacements for text to avoid problems with HTML entities!'),
+ '#weight' => -6, // Token 構文ヘルプのウェイト調整
);
$form['token_help']['help'] = array(
'#value' => theme('token_help', 'node'),
);
-
+
return system_settings_form($form);
}
@@ -60,21 +114,40 @@
function uploadpath_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case 'submit':
- if (isset($node->files)) {
- foreach ($node->files as $key => $file) {
- if (0 === strpos($key, 'upload_')) { // Only rewrite the name when adding the file, not when updating it
- // Get the new, prefixed file name
- $file_name = str_replace(array(' ', "\n", "\t"), '_', token_replace(variable_get('uploadpath_prefix', '') . '/', 'node', $node)) . $node->files[$key]['filename'];
-
- // Create the directory if it doesn't exist yet.
- $dirs = explode('/', dirname($file_name));
- $directory = file_directory_path();
- while (count($dirs)) {
- $directory .= '/' . array_shift($dirs);
- file_check_directory($directory, FILE_CREATE_DIRECTORY);
+ // アップロードファイル追加時のパス変更処理コードを変更
+ if (!in_array($node->type, variable_get('uploadpath_excluded_node_types', array()))) {
+ if (isset($node->files)) {
+ foreach ($node->files as $key => $file) {
+ if (0 === strpos($key, 'upload_')) { // Only rewrite the name when adding the file, not when updating it
+
+ // Token bugfix
+ if (variable_get('token_bugfix', false)) {
+ if (!$node->created) {
+ $node->created = (int)time();
+ }
+ if (!$node->changed) {
+ $node->changed = (int)$node->created;
+ }
+ }
+
+ // Get the new, prefixed file name
+ $pattern = variable_get('uploadpath_prefix_'.$node->type, false);
+ if (!$pattern) {
+ $pattern = variable_get('uploadpath_prefix', '');
+ }
+ $file_name = drupal_strtolower(str_replace(array(' ', "\n", "\t"), '_', token_replace($pattern . '/', 'node', $node))) . $node->files[$key]['filename'];
+
+ // Create the directory if it doesn't exist yet.
+ $dirs = explode('/', dirname($file_name));
+ $directory = file_directory_path();
+ while (count($dirs)) {
+ $directory .= '/' . array_shift($dirs);
+ file_check_directory($directory, FILE_CREATE_DIRECTORY);
+ }
+
+ // Change where the file will be saved to the specified directory.
+ $node->files[$key]['filename'] = $file_name;
}
- // Change where the file will be saved to the specified directory.
- $node->files[$key]['filename'] = $file_name;
}
}
}
- [参考]
- Patch suggestions for per-nodetype uploadpath redirection without default | drupal.org
date() expects parameter 2 to be long, string given in | drupal.org

