DrupalBin
Submit Code
About
Recent Posts
Code
Fix for Fix for Code
Fix for Code
Code
Node type whitelist
an axe coach accessories
coach purses married
Fix for de las ghd alturas
de las ghd alturas
Code
more
User login
Log in using OpenID:
What is OpenID?
Username:
*
Password:
*
Log in using OpenID
Cancel OpenID login
Create new account
Request new password
Tags
CCK
drupal
fapi
jquery
menu
module
php
simpletest
taxonomy
test
theme
views
more tags
Home
Fix for Creating a node with a FileField from file
View
Fix
July 22, 2009 - 8:33am —
mongolito404
/**
* FileField creation code from http://drupal.org/node/292904#comment-956155
*/
function
import_document
(
&
$document
)
{
$node
=
new
stdClass
(
)
;
$node
-
>
type
=
'folder'
;
node_object_prepare
(
$node
)
;
$node
-
>
title
=
$document
-
>
desc
;
$node
-
>
created
= _import_get_time
(
$document
-
>
date
)
;
$node
-
>
changed
=
$node
-
>
created
;
$node
-
>
status
=
1
;
$node
-
>
promote
=
0
;
$node
-
>
sticky
=
0
;
$node
-
>
field_category
[
]
[
'value'
]
=
58
;
$node
-
>
field_folderfiles
[
]
= _import_file
(
$node
,
'field_folderfiles'
,
'/tmp/cdm/'
.
$document
-
>
filename
)
;
var_export
(
$node
)
;
print
(
"
\n
"
)
;
//print("Content created for '$node->title'.");
}
function
_import_file
(
$node
,
$field
,
$filename
)
{
if
(
is_string
(
$field
)
)
$field
= content_fields
(
$field
,
$node
-
>
type
)
;
$validators
=
array_merge
(
filefield_widget_upload_validators
(
$field
)
, imagefield_widget_upload_validators
(
$field
)
)
;
$save_path
= _import_widget_files_directory
(
$field
,
$node
)
;
return
field_file_save_file
(
$filename
,
$validators
,
$save_path
)
;
}
function
_import_widget_files_directory
(
$field
,
$node
)
{
$widget_file_path
=
$field
[
'widget'
]
[
'file_path'
]
;
if
(
module_exists
(
'token'
)
)
{
global
$user
;
$widget_file_path
= token_replace
(
$widget_file_path
,
'user'
,
$user
)
;
if
(
$node
)
{
$widget_file_path
= token_replace
(
$widget_file_path
,
'node'
,
$node
)
;
}
}
return
file_directory_path
(
)
.
'/'
.
$widget_file_path
;
}
CCK
filefield
Submit Fix
Summary:
Tags:
Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Show summary in full view
/** * FileField creation code from http://drupal.org/node/292904#comment-956155 */ function import_document(&$document){ $node = new stdClass(); $node->type = 'folder'; node_object_prepare($node); $node->title = $document->desc; $node->created = _import_get_time($document->date); $node->changed = $node->created; $node->status = 1; $node->promote = 0; $node->sticky = 0; $node->field_category[]['value'] = 58; $node->field_folderfiles[] = _import_file($node, 'field_folderfiles', '/tmp/cdm/'.$document->filename); var_export($node); print("\n"); //print("Content created for '$node->title'."); } function _import_file($node, $field, $filename) { if(is_string($field)) $field = content_fields($field, $node->type); $validators = array_merge(filefield_widget_upload_validators($field), imagefield_widget_upload_validators($field)); $save_path = _import_widget_files_directory($field, $node); return field_file_save_file($filename, $validators, $save_path); } function _import_widget_files_directory($field, $node) { $widget_file_path = $field['widget']['file_path']; if (module_exists('token')) { global $user; $widget_file_path = token_replace($widget_file_path, 'user', $user); if($node) { $widget_file_path = token_replace($widget_file_path, 'node', $node); } } return file_directory_path() .'/'. $widget_file_path; }
Syntax highlighting mode:
ActionScript
ColdFusion
Diff
Drupal 5
Drupal 6
HTML
INI
Javascript
MySQL
PHP
Python
robots.txt
SQL
Text
Select the syntax highlighting mode to use.
See Also:
Order
Title:
URL:
-1
0
1
Title:
URL:
-1
0
1
Any links you'd like to have associated with the post (Drupal.org issue, Wikipedia article, etc).
File attachments
Changes made to the attachments are not permanent until you save this post. The first "listed" file will be included in RSS feeds.
Attach new file:
The maximum upload size is
1 MB
. Only files with the following extensions may be uploaded:
jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp phps
.