3

How can I automatically attach/create fields to an entity when I enable a module that provides entities?

For example, I want my Entity to have an Image field, and a handful of text, date, and number fields ready to roll.

I see field_create_field and field_create_instance, do I need to define them or can I use whats already defined as fields in Drupal and just give them labels and default values?

2

field_create_field creates a field definition, and field_create_instance creates an instance of that field.

If you look at the UI, you are able to reuse the same field different places, this is what field_create_instance does.

Both functions requires an array with a lot of data containing info about which entity to attach the field on, labels various settings, how the field should be displayed etc.

  • Thanks, picked up on it pretty quick with those two functions. – Kevin Jan 24 '12 at 20:04
4

This is a just a follow up to @googletorp's answer, you can use the following code to export an existing field instance definition to PHP code which you can then alter as you need to for your new instance:

$entity_type = 'node';
$field_name = 'field_name';
$bundle_name = 'node_type';

$info_config = field_info_field($field_name);
$info_instance = field_info_instance($entity_type, $field_name, $bundle_name);
unset($info_config['id']);
unset($info_instance['id'], $info_instance['field_id']);
include_once DRUPAL_ROOT . '/includes/utility.inc';
$output = "field_create_field(" . drupal_var_export($info_config) . ");\n";
$output .= "field_create_instance(" . drupal_var_export($info_instance) . ");";
drupal_set_message("<textarea rows=30 style=\"width: 100%;\">". $output .'</textarea>');

That will give you both the field_create_field and field_create_instance code for the field.

  • So I can use the output here, and dump that into field_create_instance, after creating my entity specific field with field_create_field()? – Kevin Jan 24 '12 at 20:07
  • Yep that's right, the code above will actually output the field_create_instance(... part of the field code as well so it's a straight copy and paste – Clive Jan 24 '12 at 22:10
  • thats awesome. I used that for Image and Location where I was lost. – Kevin Jan 24 '12 at 22:13
1

I always solve this by creating the appropriate entity configuration in an install, exporting it with Features, and then make the install profile depend on it. As googletorp points out, manually creating a field requires a lot of data, and your bound to mess it up quite a few times if you write it all manually.

Even if you don't want to use Features for storing the field, you could still export them that way, and then copy paste the code into your own module.

  • I can't depend on that here, though. I am working on a module that needs to stand independent of being a Feature, for now. – Kevin Jan 24 '12 at 20:03
  • You don't need to depend on the module, you can just use it as a convenient way to export the definitions, as I said in the second paragraph. – Letharion Jan 24 '12 at 21:19

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.