Making an entity fieldable

Last modified: May 27, 2009 - 19:24

There are a number of hooks involved in making an entity fieldable. hook_fieldable_info provides the info (surprise!):

<?php
function privatemsg_fieldable_info() {
 
$return = array(
   
'privatemsg' => array(
     
'name' => t('Private messages'),
     
'id key' => 'pmid',
    ),
  );
}
?>

There are more possible keys, see the API reference for more.
Next up, we need to provide build modes:
<?php
function privatemsg_build_modes($obj_type) {
 
$modes = array();
  if (
$obj_type == 'privatemsg') {
   
$modes = array(
     
'full' => t('Full node'),
     
'teaser' => t('Teaser'),
    );
  }
  return
$modes;
}
?>

Now, the rest is just calling the field attach API on load, save, form, validate and submit.

<?php
function privatemsg_load...
 
// Load the message(s)
 
field_attach_load('privatemsg', $array_of_messages);
?>

<?php
function privatemsg_save($message) {
 
// $message is expected to be an object, same as for drupal_write_record.
 
field_attach_presave('privatemsg', $message);
  if (
/* new message */ {
   
// Save the message.
   
...
   
// And then:
   
field_attach_insert('privatemsg', $message);
  }
  else {
   
// Save the message.
   
...
   
// And then:
   
field_attach_update('privatemsg', $message);
  }
?>

hook_fieldable_info appears

mikey_p - September 22, 2009 - 07:50

hook_fieldable_info appears to have been deprecated in favor of hook_entity_info.

 
 

Drupal is a registered trademark of Dries Buytaert.