2

I'm dealing building several modules that should cope with automatic creation of content synchronized with external sources. The whole process is automatized, e.g. everything had been created programatically. My content type has and image field attached to it. When appempting to create a node without defining what this image field should contain (and it should stay empty), I fall with exception:

    PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048
    Column 'fid' cannot be null: INSERT INTO {file_usage} (fid, module, type, id, count) 
    VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4);
    Array ( [:db_insert_placeholder_0] => [:db_insert_placeholder_1] => file [:db_insert_placeholder_2] => node [:db_insert_placeholder_3] => 264 [:db_insert_placeholder_4] => 1 ) 
    in file_usage_add() (line 657 of /usr/local/drupal7/includes/file.inc).

When on hook_node_presave I'm adding an image to this field all works correctly, but I would like to leave this field empty, that may affect the rest of development process.

If I create the node type programatically but add the image field manually -- all works ok. I tried it, then printed the definition of this field using function field_info_field() and copied some values of there into my code, but it didn't help.

Here is my image field definition:

...

'field_member_photo' => array(
'label' => t('Member Photo'),
'field_name' => 'field_member_photo',
'type' => 'image',
'module' => 'image',
'active' => '1',
'locked' => '0',
'cardinality' => '1',
'deleted' => '0',
'widget' => 'image_image',
'entity_types' => array('node'),
'settings' => array('uri_scheme' => 'public', 'default_image' => 0,),
),

...

Would appreciate any thought about how to solve this problem.

2
  • You want to add field problematically? I didn't fully understood you.
    – Roy Segall
    Commented May 7, 2013 at 11:59
  • Emmm... I want to add field programatically. From code. And then to create a node programatically, while the node should have this field empty. Commented May 7, 2013 at 12:13

1 Answer 1

1

I think this is already been covered here some where but i'll answer this any way because creating fields manually is not something easy.

Let's start: 1. First thing - create the field through the UI 2. Export the fields with features. In the latest version of features, features devide the export to to diffrent things: file with all the fileds settings and a file with all the field instances. From the settings of the field that features create you need only the settings: cardinality, field name, module and settings. From the instance you will need the settings: label, required and the widget. You can save them to a function.

  1. After you saved the settings you will need to use a couple of functions for next you will install your module or anything like this: field_info_field(), this function will let return you the settings of the field if he exists, if not you will need to create it with the function field_create_field() like this:

    field_create_field($field_config);

The argument you'll need to give her is the settings of the field you saved later. For attaching the field to an entity you'll need to use the function field_create_instance that get the instance settings you save with three more keys:

  • field_name - the name of the field
  • bundle - node type, message type etc. etc,
  • entity_type - the entity type: node, comment, message etc. etc.

You can see here hot it's done:

// This is the instance settings you saved.
$instance;
$instance += array(
  'field_name' => $field_config['field_name'],
  'bundle' => 'article',
  'entity_type' => 'node',
);

field_create_instance($instance);

You can have a look in the module OG in the function og_create_field() and how he saved fields inside hook. This could very easy and other module you will wrote can be based on this if you have a very a lot of fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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