3

I want to create a field, i searched on drupal and find field_create_field can create a field but I also find that hook_field_info is also to create to field.

My question is what is basic difference between both of them? Are they use for same purpose?

I need the difference and where both are used?

0

2 Answers 2

3

function field_create_field is used to create a field. When you debug this function you will get $field array with the id property filled in.

function hook_field_info defines Field API field types. For more info refer here.

For more clarification you can simply debug both these functions then you will get the all basic differences.

1

field_create_field can create field object with passing the field definition as function argument.

hook_field_info can only define the field definition.

The main reason why we need a separate function to define field definition is to get benefit from the alter behavior of Drupal (e.g. hook_field_info_alter). It is what I guess.

4
  • You're right about the alter functionality, but I think the main point for the OP is that hook_field_info is for declaring types of field, and field_create_field is for creating the fields themselves
    – Clive
    Sep 28, 2016 at 10:20
  • @Clive yes. My idea is types does matter because there is alter function. It is the only extra behavior provided by hook_xxx_info I can think of, otherwise, a separate function for defining field type is not necessary if we don't need to consider the ease of code management.
    – Jimmy Ko
    Sep 28, 2016 at 10:27
  • @JimmyKo the both do same work ? Means i can define fields and create them using hook_field_info and supported function like widget and formatter hook? Sep 30, 2016 at 4:50
  • @VarunPandey Nope, they are not doing same work. hook_field_info return array with field key which contains the properties of field. hook_create_field is to read the array for forming the database query to create table and other DB operations.
    – Jimmy Ko
    Sep 30, 2016 at 8:31

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.