By gafir777 on
Hello,
I am developing a module for Drupal 6 and I'm working on the node type form to add a few fields. I would like to add some CCK fields programmatically to this node type from my custom module. More specifically, I would be interested in adding a filefield for that node type from my example.module file.
Is there any documentation on how to do this? I've been searching the FileField pages without success.
Thanks for your help,
Comments
Is it possible to do
Is it possible to do something like:
'#type' => 'filefield'
to define the field?
Actually, I found in the
Actually, I found in the filefield module that the array name of the field is filefield_widget:
So I believe that if I were to add the field from the module file, I would have to do it this way:
I'm not familiar with using cck inside modules, but I found the following post that seemed to be relevant:
http://drupal.org/node/329022
Also looking for this solution
I am also looking to do the same and thank you for the tip and it definitely is close as the filefield widget appears but with errors.
The filefield_widget #process callback is complaining:
* warning: Invalid argument supplied for foreach() in F:\web4\nexdrupal\sites\all\modules\filefield\filefield_widget.inc on line 384.
* warning: implode() [function.implode]: Invalid arguments passed in F:\web4\nexdrupal\sites\all\modules\filefield\filefield_widget.inc on line 393.
It was also complaining that no default_value was passed in and now it wants to know about the validators. I would prefer that it just default validators.
$form['attachment'] = array(
'#type' => 'filefield_widget',
'#title' => 'Attachment',
'#default_value' => array(
'fid' => 0,
'list' => 1,
'data' => array(
'description' => ''
)
),
'#field_name' => 'field_nexfile_file',
'#upload_validators' => array()
);
Focusing on Business Applications but heck we do anything Drupal
Hello blainelang, I just
Hello blainelang,
I just wondered if you figured out your issue with the warnings. I'm currently trying to use a cck field of type date:
I'll post the issues I'm having with it here.
issue solved
This post apparently solves the issue:
http://www.openbandlabs.com/blog/2009/06/programmatic-cck-content-type-c...
Not working if module=>'mymodule'
Thank you Loic, I had found that article as well during my googling and had hoped I did not need to go to that extent. I've spent a couple hours now trying to make this method work and it's failing, and the issue is related to creating the content type for a custom module. In my case the module is called 'nexfile'.
When module nexfile is installed, the HOOK_node_info creates a new content type but without the filefield type field that is needed for the attachments.
function nexfile_node_info() {
return array(
'nexfile_folder' => array(
'name' => t('Nexfile Folder'),
'module' => 'nexfile',
'description' => t('Folder for storing documents'),
'has_title' => TRUE,
'title_label' => t('Folder Name'),
'has_body' => TRUE,
'body_label' => t('Folder Description'),
'locked' => FALSE ));
}
Manually editing the content type and adding the new field using the upload widget and tweak a few of it's default settings and the module works with all the API hooks. This is fine for initial development but need to have an automated install.
Export the content type and follow the noted article - module install silently fails creating content type.
Can I import this now exported 'Nexfile Folder' content type into a clean site?
> No, it indicates success and then reports 'An error has occurred adding the content type' but no explanation in the watchdog logs.
If I modify the export code and change module=> 'nexfile' to module=> 'node' then it will import.
But doing so will then not work as I need for my module. Also tested doing the import after installing the nexfile module but there would be an error about duplicate content type.
Also testing the import with module => 'nexfile' but modifying the type and field name to be unique after installing the nexfile module and the import still fails.
All I really need is to programmatically add the one CCK field to a very standard content type but so far no joy :(
The CCK export that is being used:
$content['type'] = array (
'name' => 'Nexfile Folder',
'type' => 'nexfile_folder',
'description' => 'Folder for storing documents',
'title_label' => 'Folder Name',
'body_label' => 'Folder Description',
'min_word_count' => 0,
'help' => '',
'node_options' =>
array (
'status' => true,
'promote' => true,
'sticky' => false,
'revision' => false,
),
'old_type' => 'nexfile_folder',
'orig_type' => 'nexfile_folder',
'module' => 'node',
'custom' => false,
'modified' => false,
'locked' => false,
'reset' => 'Reset to defaults',
'comment' => 2,
'comment_default_mode' => 4,
'comment_default_order' => 1,
'comment_default_per_page' => 50,
'comment_controls' => 3,
'comment_anonymous' => 0,
'comment_subject_field' => 1,
'comment_preview' => 1,
'comment_form_location' => 0,
);
$content['fields'] = array (
0 =>
array (
'label' => 'Folder File',
'field_name' => 'field_nexfile_file',
'type' => 'filefield',
'widget_type' => 'filefield_widget',
'change' => 'Change basic information',
'weight' => '31',
'file_extensions' => '',
'progress_indicator' => 'bar',
'file_path' => '',
'max_filesize_per_file' => '',
'max_filesize_per_node' => '',
'description' => '',
'required' => 0,
'multiple' => '1',
'list_field' => '1',
'list_default' => 1,
'description_field' => '0',
'op' => 'Save field settings',
'module' => 'filefield',
'widget_module' => 'filefield',
'columns' =>
array (
'fid' =>
array (
'type' => 'int',
'not null' => false,
'views' => true,
),
'list' =>
array (
'type' => 'int',
'size' => 'tiny',
'not null' => false,
'views' => true,
),
'data' =>
array (
'type' => 'text',
'serialize' => true,
'views' => true,
),
),
'display_settings' =>
array (
'label' =>
array (
'format' => 'above',
'exclude' => 0,
),
'teaser' =>
array (
'format' => 'default',
'exclude' => 0,
),
'full' =>
array (
'format' => 'default',
'exclude' => 0,
),
4 =>
array (
'format' => 'default',
'exclude' => 0,
),
),
),
);
$content['extra'] = array (
'title' => '-5',
'body_field' => '0',
'revision_information' => '20',
'comment_settings' => '30',
'menu' => '-2',
);
Focusing on Business Applications but heck we do anything Drupal
Solved - see this topic
There have been several topics initiated by other developers trying to do the same and not having success but this issue is now solved in my case - refer to this topic http://drupal.org/node/702156#comment-2739964
Focusing on Business Applications but heck we do anything Drupal
use features module
You can use features module for adding new fields.