How can I associate many existing files with drupal filefield? - drupal

I have many mp3 files stored on my server already from a static website, and we're now moving to drupal. I'm going to create a node for each audio file, but I don't want to have to upload each file again. I'd rather copy the files into the drupal files directory where I want them, and then associate the nodes with the appropriate file.
Any ideas on how to accomplish that?
Thanks!

I am not sure if I am going to propose a different approach or if I am about to tell with different words what you already meant with your original question, but as you want the nodes to be the files, I would rather generate the nodes starting from the files, rather than associating existing nodes with existing files.
In generic terms I would do it programmatically: for each existing files in your import directory I would build the $node object and then invoke node_save($node) to store it in Drupal.
Of course in building the $node object you will need to invoke the API function of the module you are using to manage the files. Here's some sample code I wrote to do a similar task. In this scenario I was attaching a product sheet to a product (a node with additional fields), so...
field_sheet was the CCK field for the product sheet in the product node
product was the node type
$sheet_file was the complete reference (path + filename) to the product sheet file.
So the example:
// Load the CCK field
$field = content_fields('field_sheet', 'product');
// Load the appropriate validators
$validators = array_merge(filefield_widget_upload_validators($field));
// Where do we store the files?
$files_path = filefield_widget_file_path($field);
// Create the file object
$file = field_file_save_file($sheet_file, $validators, $files_path);
// Apply the file to the field, this sets the first file only, could be looped
// if there were more files
$node->field_scheda = array(0 => $file);
// The file has been copied in the appropriate directory, so it can be
// removed from the import directory
unlink($sheet_file);
BTW: if you use a library to read MP3 metadata, you could set $node->title and other attributes in a sensible way.
Hope this helps!

The file_import module doesn't do exactly what you want (it creates node attachments instead of nodes), but it would be relatively simple to use that module as guidance along with the Drapal API to do what you want.

Related

Is there a way to add more information to a node, except the mandatory ones?

I want to add more node information to a network node. Is it possible to share more data besides what's in the node configuration file? Maybe some custom fields, like an encoded logo image or stuff like that.
Thanks
Yes you can.
Inside your module under src folder add a file called config.conf.
Add your values inside of it in the following format:
key1="string_value"
key2=number_value
Inside build.gradle go to the part where you define your nodes, let's say your module name is "my_module"; do this:
cordapp (project(':my_module')) {
config project.file("src/config.conf")
}
Now when you run deployNodes, gradle will generate a file called my_module.conf under build\nodes\my_node\cordapps\config.
To access those values inside your flow:
getServiceHub().getAppContext().getConfig().getString("key1");
As for testing flows; to mimic the custom config file you need to do the following:
Map<String, String> customConfig = new HashMap<>();
customConfig.put("key1", "string_value");
customConfig.put("key2", "int_value");
// Setup network.
network = new MockNetwork(new MockNetworkParameters().withCordappsForAllNodes(ImmutableList.of(
TestCordapp.findCordapp("my_package").withConfig(customConfig))));

how to add node in through Drupal API

In order to insert an article to Drupal there are thee ways of doing that:
by admin panel - really slow and not feasable if talking about 400 articles
by pure sql - number of tables that have to maintained and calculated (content, core, cat etc.) is quite high and it's not really reliable
by using drupal API - that's something that I was trying to implement but can't find a good documentation on it. What I'm trying to achieve is to use drupal classes and insert content (ie. running a PHP file with (catid,title,introtext....))
Example: what i want to add node in xyz.com/drupal site but my php code should be run in irankmedi.com
Can you please point me into direction where I can find some info on how to manage articles and categories this way?
Thanks in advance
If your request is very specific and you can't find a module that does what you need it shouldn't be too difficult to make a module on your own and import (create drupal) content from your code. Should be something like this:
Create a php file that will do the job.
At start of your script include standard Drupal's bootstrap code so you'll have all Drupal's functionality available in your script
Make code that will read content (from database or feed or something else).
Use Drupal api to insert node programatically:
https://www.drupal.org/node/1388922
Call your script manually or set cron to call it on specific time periods.
First you have to read RSS feed in drupal custom module. Then, following code can create node programatically.
$node = new stdClass();
$node->type = 'blog'; // This type should exist
node_object_prepare($node); // Sets some defaults.
$node->language = LANGUAGE_NONE; // The language of the default
$node->title = "testnode";
$node->body[LANGUAGE_NONE][0]['value'] = "Body";
node_save($node); // function that actually saves node in database

Grunt-init copyAndProcess function: Can I pass in multiple values to 'noProcess' option?

I'm using grunt-init to build a template for a site structure I repeat regularly.
The template.js file uses the init.copyAndProcess function to customize most of files but a few of them get corrupted by the file processing (some fonts and image files) and I want to include those files in the 'noProcess' option. If these files all existed in the same directory, I could use the noProcess option as mentioned in the documentation [ See: http://gruntjs.com/project-scaffolding#copying-files ] and pass in a string like and it works:
var files = init.filesToCopy(props);
init.copyAndProcess(files, props, {noProcess: 'app/fonts/**'} );
Unfortunately the files that I need to have no processing performed on are not all in the same directory and I'd like to be able to pass in an array of them, something like the following block of code, but this does not work.
var files = init.filesToCopy(props);
init.copyAndProcess(files, props, {noProcess: ['app/fonts/**', 'app/images/*.png', 'app/images/*.jpg']} );
Any thoughts on how I can have multiple targets for the 'noProcess' option?
As soon as I posted the question, I realized that my proposed code did work. I simply had an invalid path when I'd renamed my 'app' directory to 'dev'.

How to upload multiple files to a folder in xquery?

How can I upload multiple files to a folder in xquery ?
Are you trying to iterate through a directory on the filesystem and insert those files at some location in the db, like this?
for $e in xdmp:filesystem-directory('c:\my-files\')/dir:entry
let $file := xdmp:filesystem-file($e/dir:pathname)
let $dest-uri := concat('/dest-path/',$e/dir:filename)
where ($e/dir:type eq 'file')
return xdmp:document-insert($dest-uri,$file)
You should take a look at Information Studio. It provides a UI for loading and transforming content. One of the built-in “collectors” allows you to point at a file system directory and recursively load its contents. Information Studio automatically handles the directory traversal and breaking the load up into multiple transactions, among many other conveniences.

Saving nodes with a filefield

I'm in the progress of creating a bulk upload function for a Drupal site. Using flash I'm able to upload the files to a specific url that then handles the files. What I want to do, is not just to upload the files, but create a node of a specific type with the file saved to a filefield that has been setup with CCK. Since these are audio files, it's important that filefield handles the files, so addition meta data can be provided with the getid3 module.
Now I've looked through some of the code as I wasn't able to find an API documentation, but it's not clear at all how I should handle this. Ideally I could just pass the file to a function and just use the data returned when saving the node, but I haven't been able to find that function.
If any one has experience with this I would apreciate some pointers on how to approach this matter.
I had to do something similar some weeks ago and ended up adapting some functionality from the Remote File module, especially the remote_file_cck_attach_file() function. It uses the field_file_save_file() function from the filefield module, which might be the function you're looking for.
In my case, the files are fetched from several remote locations and stored temporarily using file_save_data(). Attaching them to a CCK filefield happens on hook_nodeapi() presave, using the following:
public static function attachAsCCKField(&$node, $filepath, $fieldname, $index=0) {
// Grab the filefield definition
$field = content_fields($fieldname, $node->type);
$validators = array_merge(filefield_widget_upload_validators($field), imagefield_widget_upload_validators($field));
$fieldFileDirectory = filefield_widget_file_path($field);
// This path does not necessarily exist already, so make sure it is available
self::verifyPath($fieldFileDirectory);
$file = field_file_save_file($filepath, $validators, $fieldFileDirectory);
// Is the CCK field array already available in the node object?
if (!is_array($node->$fieldname)) {
// No, add a stub
$node->$fieldname=array();
}
$node->{$fieldname}[$index] = $file;
}
$filepath is the path to the file that should be attached, $fieldname is the internal name of the filefield instance to use within the node and $index would be the 0 based index of the attached file in case of multiple field entries.
The function ended up within a utility class, hence the class syntax for the verifyPath() call. The call just ensures that the target directory is available:
public static function verifyPath($path) {
if (!file_check_directory($path, FILE_CREATE_DIRECTORY)) {
throw new RuntimeException('The path "' . $path . '" is not valid (not creatable, not writeable?).');
}
}
That did it for me - everything else happens on node saving automatically.
I have not used the getid3 module yet, so I have no idea if it would play along with this way of doing it. Also, I had no need to add additional information/attributes to the filefield, so maybe you'd have to put some more information into the field array than just the file returned by field_file_save_file(). Anyways, hope this helps and good luck.
I have done something whith imagefield which worked, I think the structure has to be right otherwise it won't work. It took a lot of trial and error. This is is what I populated the imagefield with.
$image['data'] =array(
'title' => $media_reference_attributes->getNamedItem("source")->value,
'description' => $description,
'alt' => "",);
$image['width'] = $width;
$image['height'] = $height;
$image['mimetype'] = $mime_type
$image['uid'] = 1;
$image['status'] = 1;
$image['fid'] = $fid;
$image['filesize'] = $file->filesize;
$image['nid'] = $id;
$image['filename'] = $url;
$image['timestamp'] = $file->timestamp;
$image['filepath'] = $file_path;
Hope this is of some help.
You might want to look at Image FUpload if you need a look at integrating the flash upload.
To push the files on to another server while still handling them through Drupal sounds a little like the CDN space, maybe look at the behavior in the CDN or CDN2 projects?
If you find a clear solution please come back and post it!

Resources