4

I'm trying to add a custom node type in hook_install() for my custom module. I've been able to add the fields I want but can't seem to find a way to set the allowed file extensions for file fields. (it defaults to .txt)

The example below shows how I create my file field. I have tried setting both allowed_file_extensions and file_extensions in the settings array but that does not seem to work.

How would I set the allowed file extensions for a file field?

    field_create_field(array(
        'field_name' => 'xxx_video',
        'type' => 'file',
        'settings' => array('allowed_file_extensions' => array('mp4', 'avi', 'mov', 'wmv', '3gp', 'mpeg', 'mpeg2', 'mkv')),
        'entity_type' => array('node'),
    ));
    field_create_instance(array(
      'field_name' => 'xxx_video',
      'required' => true,
      'entity_type' => 'node',
      'bundle' => 'xxx',
      'label' => t('Video'),
      'description' => t('Video to play in smartbox.'),
    ));
5

The allowed file extension settings are added to the field instance settings, rather than the global field settings.

The variable is called 'file_extensions' and not 'allowed_file_extensions'

It is also a textfield string value, rather than an array.

You could try something like:

field_create_field(array(
    'field_name' => 'xxx_video',
    'type' => 'file',
    'entity_type' => array('node'),
));
field_create_instance(array(
  'field_name' => 'xxx_video',
  'required' => true,
  'entity_type' => 'node',
  'bundle' => 'xxx',
  'label' => t('Video'),
  'settings' => array('file_extensions' => 'mp4, avi'),
  'description' => t('Video to play in smartbox.'),
));
  • Is the , (comma) necessary in the file_extensions ? or should 'settings' => array('file_extensions' => 'mp4 avi') work as well? – FLY Feb 13 '13 at 10:54
  • Possibly fine with just space separated values there, test to verify. – David Thomas Feb 13 '13 at 10:56
  • I tried this using the /devel/php page on a custom user field I created earlier. using a , will add double comma's seperated values in the allowed extensions field on the /admin/config/people/accounts/fields/user_cv page. No error's and it will still work but I guess it's beter to use space separated values. – FLY Feb 13 '13 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.