hey,

I'm building a module that makes a custom content type - an audio upload to be precise. I have it already so that it adds the content type with a file upload box, but i cannot find how to change the allowed file extensions value.

I've read through every doc page on hook_field_info and field_create_field twice - as well as all the field_create_instance stuff but none of the methods suggested there seem to work. Below is my code:

 //Add Fields 
  //MP3 Field
  $field = array(
	'field_name' => 'field_sermon_audio',							//Machine Name
	'type'		 => 'file',											//type, e.g. file, taxonomy_term_reference, text...
	'cardinality'=> 1,												//how many of these can there be in one field.
	'settings'	 => array(
		'instance_settings' => array(	
			'file_extensions' => 'mp3',								//file extensions (seperated by a comma?)
			'file_directory'  => 'sermons/audio'),					//directory (withing files/ ?)
		'label'	=> t('Audio'),										//Label
		'description' => t('Store the Audio File'),
		'file_extensions' => 'mp3'
		),
	);
  field_create_field($field); //create field
  
  $instance = array(
    'field_name' => 'field_sermon_audio',
    'entity_type' => 'node',
    'label' => 'Audio',
    'bundle' => 'sermon',											//bundle is a posh name for the content type.
    'description' => 'The audio recording of the sermon.',
    'widget' => array(
      'type' => 'file_generic',
      'weight' => -4,
    ),
    'display' => array(
      'default' => array(
        'type' => 'file_default',
        'weight' => 10,
      ),
      'teaser' => array(
        'type' => 'file_default',
        'weight' => 10,
      ),
    ),
  );
  field_create_instance($instance); 

Any ideas?

Comments

mcaden’s picture

add:

    'settings' => array(
      'file_directory' => 'sermons/audio',
      'file_extensions' => 'mp3',
      'max_filesize' => '2MB',
      'title_field' => '',
    ),

to $instance and you should be able to get rid of the settings array on $field.

Additionally, do not separate the file extensions by comma in your code. Separate them by space. When I tried using commas to default a document field drupal translated it as txt,, doc,, doct,, (etc...). It turned it into double-commas. Spaces work great.

What I ended up with for my "Document" field was the following:

  //Document Field
  $field = array(
    'field_name' => 'field_document',
    'type'  => 'file',                                       
    'cardinality' => FIELD_CARDINALITY_UNLIMITED,                          
    );
  field_create_field($field);
  
  $instance = array(
    'field_name' => 'field_document',
    'entity_type' => 'node',
    'label' => 'Document',
    'bundle' => 'article',
    'description' => 'A document attached to a news article.',
    'settings' => array(
      'file_directory' => 'field/document',
      'file_extensions' => 'txt doc docx pdf ods odt xls xlsx',
      'max_filesize' => '2MB',
      'title_field' => '',
    ),
    'widget' => array(
      'type' => 'file_generic',
      'weight' => -4,
    ),
    'display' => array(
      'default' => array(
        'type' => 'file_default',
        'weight' => 10,
      ),
      'teaser' => array(
        'type' => 'file_default',
        'weight' => 10,
      ),
    ),
  );
  field_create_instance($instance); 
blainelang’s picture

Download the field_inspector module. Create the field manually with all the required settings and then use the field inspector (access via admin/config/development/field-inspector) and select the entity + bundle + field and it will generate the PHP you need to create the field programatically.

Focusing on Business Applications but heck we do anything Drupal