SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question [drupal] hook_field_schema is not called at all!!! What happened??

    Hi, I'm using this piece of code to create a field named zoho_task_id in every node of the support_ticket type.

    PHP Code:
      if (!field_info_field($field_name)) {
          
    //drupal_set_message ("creating field");
          //die();
          // Create the field.
          
    $field = array(
            
    'field_name' => $field_name,
            
    'type' => 'field_hidden_text',
            
    'settings' => array(
              
    'max_length' => 255,
            ),
            
    'cardinality' => 1,       
          );
          
    field_create_field($field);
     
          
    // Create the instance.
          
    $instance = array(
            
    'field_name' => $field_name,
            
    'entity_type' => 'node',
            
    'bundle' => 'support_ticket',
            
    'label' => 'Zoho Task ID',
            
    'description' => 'Zoho Task ID',
            
    'required' => FALSE,
          );
     
          
    field_create_instance($instance);
          
    watchdog('zoho't('!field_name was added successfully.', array('!field_name' => $field_name)));
          
    //drupal_set_message ("created field " . $field_name);
      
    }
      else {
        
    watchdog('zoho't('!field_name already exists.', array('!field_name' => $field_name)));
      } 
    Now I want to override the hook_field_schema to add index and unique key into the field.

    PHP Code:
    function zoho_field_schema($field)
    {
        
    //Config::display_test_result_as_drupal_message($field);
        
    echo "Calling hook_field_schema....";
        
    watchdog('zoho'"Calling hook_field_schema....");
        
    watchdog('zoho't('Creating !field_name....', array('!field_name' => $field['field_name'])));
        
        if(
    $field['field_name'] == Config::$zoho_task_id_fieldname)
        {
          
    watchdog('zoho't('Modifying !field_name.', array('!field_name' => $field['field_name'])));
          return array(
            
    'columns' => array(
              
    'value' => array(
                
    'type' => 'varchar',
                
    'size' => '255',
                
    'not null' => FALSE,
              ),    
            ),
            
    'indexes' => array(
              
    'value' => array('value'),
            ),
            
    'unique keys' => array(
              
    'value' => array('value'),
            ),          
          );
        }
        elseif(
    $field['field_name'] == Config::$zoho_comment_id_fieldname)
        {
          
    watchdog('zoho't('Modifying !field_name.', array('!field_name' => $field['field_name'])));
          return array(
            
    'columns' => array(
              
    'value' => array(
                
    'type' => 'varchar',
                
    'size' => '255',
                
    'not null' => FALSE,
              ),    
            ),
            
    'indexes' => array(
              
    'value' => array('value'),
            ),
            
    'unique keys' => array(
              
    'value' => array('value'),
            ),          
          );
        }    

    As you can see, I used the watchdog function to log when the function is called. But when I checked the log, there is no message "Calling hook_field_schema" in there. And when I check the data table field_data_zoho_task_id, I see there is no unique key nor index in the zoho_task_id_value column. I guesss these mean the hook_field_schema wasn't called. I wonder how can I get it work? I put the function zoho_field_schema in the .install file as instructed in https://api.drupal.org/api/drupal/mo...field_schema/7 but nothing happened. Please help me with this problem. Thanks.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    You will need to use an update hook to alter the schema once the module has been installed. The alternative is to completely uninstall the module and reinstall though that may lead to other problems depending on how the module is built.

    zoho/zoho.install
    PHP Code:
    ...

    function 
    zoho_update_7001() {
       ...

    If you go this route than you will need to update the module through update.php.

    Drupal Docs:
    https://api.drupal.org/api/drupal/mo...ook_update_N/7
    The only code I hate more than my own is everyone else's.

  3. #3
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I used both of them (update & uninstall) but none of them worked. I checked the log and I see the watchdog lines in the hook field_schema weren't called. How can I invoke the hook after the field is created?


    Quote Originally Posted by oddz View Post
    You will need to use an update hook to alter the schema once the module has been installed. The alternative is to completely uninstall the module and reinstall though that may lead to other problems depending on how the module is built.

    zoho/zoho.install
    PHP Code:
    ...

    function 
    zoho_update_7001() {
       ...

    If you go this route than you will need to update the module through update.php.

    Drupal Docs:
    https://api.drupal.org/api/drupal/mo...ook_update_N/7

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    Just defining the functions means nothing. You need to navigate to /update.php and go through the update process for the module(s) that require updates.
    The only code I hate more than my own is everyone else's.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •