1

I have an entity whose field schema is already defined and now I am trying to add couple of fields to it.

But I am not sure how to code it up using schema_alter.Below is an example on how I would code the schema for the field using hook_field_schema().

function hook_field_schema($field){
  $columns = array(); 
  if($field['type'] == 'some_field_name'){
  $columns = array(
              'col1'=>array(
                     'type =>'text'
                     'size' => 'big'
                     'not null' => false),
              'col2'=>array(
                     'type =>'text'
                     'size' => 'big'
                     'not null' => false),
              );
  } 
 //Similar for other fields  
}

Confusion is on how to decompose this schema according to schema_alter.

I am following this article to implement hook_field_schema_alter()

Edit: I am using an update function to create the field (field_create_instance), since this is an already an existing module and I do not want to disable it and enabled it again.

1 Answer 1

0

You would need to call the field_create_instance() function passing a new array.

 $instance = array(
    'field_name' => 'field_my_custom', // ** This already exists, or check with field_info_field()
    'entity_type' => 'node',
    'label' => 'a label', 
    'bundle' => $content_type, 
    'widget' => array(), // if you're not sure about your widget settings, take a glimpse at an existing field instance using debug(field_info_instance(...));
);
field_create_instance($instance);

More links to read and better understand the above functions:

https://api.drupal.org/api/drupal/modules%21field%21field.info.inc/function/field_info_field/7 https://api.drupal.org/api/drupal/modules%21field%21field.info.inc/function/field_info_instance/7 https://api.drupal.org/api/drupal/modules%21field%21field.crud.inc/function/field_create_instance/7

Also, take a look at the Examples for Developers module, node example and field example.

1
  • Thanks for your answer,but I am trying to alter the schema of the field.I am already doing the field_create_instance using an update function.But I am unable to represent the schema information in the form according to hook_field_schema_alter.The example given is on how I would do it in hook_field_schema just for demonstration purposes
    – Wang Liqin
    Nov 17, 2014 at 11:58

Your Answer

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

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