Last updated March 15, 2012. Created by chx on May 27, 2009.
Edited by bfr, fp, drunken monkey, bcn. Log in to edit this page.

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

<?php
/**
* Implements hook_entity_info().
*/
function privatemsg_entity_info() {
  return array(
   
'privatemsg' => array(
     
'label' => t('Private messages'),
     
'base table' => 'privatemsg',
     
'entity keys' => array(
       
'id' => '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);
  }
?>

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.