Domanda

After creating multiple fields with the help of the installation profile I am now also trying to figure out how I can add the allowed file extensions in the installation profile. Here is my code so far:

'name' => 'attachment',
'type' => 'file',
'settings' => array(
    'allowed_file_extensions' => array('txt pdf'),
),

The allowed_file_extensions was just a test to see if it works but it doesn't it gives an fatal PHP error. (** PHP Fatal error: Unsupported operand types** )

How can I add the file extensions at the field?

PS: I also tried putting the settings in the instance, this gives the same error.

È stato utile?

Soluzione

Oke, I was having this problem for the last three days. Now after a few hours after I posted this question I solved the problem.

For those who have the same question or problem here is what solved it for me and what the right way is to add settings. The settings can be different for each instance so the settings go at the instance creation and not the field itself.

Here is an example on how it is supposed to be:

$instance = array(
    'field_name'    => 'file'
    'entity_type'   => 'node',
    'bundle'        => 'article',
    'label'         => 'Attachment',
    'description'   => 'Add an attachment here',
    'required'      => TRUE,
    'settings'      => array(
         'max_filesize'    => '512',
         'file_extensions' => 'zip txt pdf docx'
    ),
);
field_create_instance($instance);

The settings field is not required but i was using an foreach because I generate multiple fields and instances at once, if you do this and you have settings at 1 of your generated field instances then you have to add settings at all instances or check if it is there or not.

I hope my experience can help any of you out when having the same problem.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top