Drupal - How to write a property callback for a custom field to use with the Entity module

Kind of :) ... if its just numbers and strings you can cheat a little. I added some basic integer columns to the User table and wanted EntityAPI and views etc to be aware of them and did the following:

/**
 * Implementation of hook_entity_property_info(). This dpeends on the EntityAPI.
 *
 * This defines the properties/attributes/"fields" of our Entity regardless of
 * whether they are drupal Fields or custom data (in the base table).
 */
function dew_nagging_entity_property_info_alter(&$info) {
  $properties = &$info['user']['properties'];
  $properties['dew_survey_count_to_complete'] = array(
    'label' => t('Number of Surveys to Complete'),
    'type' => 'integer',
    'description' => 'When a student logs in the system derives how many surveys they must complete. We store this value to make sending the nag email business logic easier.',
    'schema field' => 'dew_survey_count_to_complete',
    'getter callback' => 'entity_property_verbatim_get',
    'setter callback' => 'entity_property_verbatim_set',
  );
  $properties['dew_survey_completed_count'] = array(
    'label' => t('Number of Completed Surveys to Date'),
    'type' => 'integer',
    'description' => 'This is a count of the completed surveys completed by a student in real-time. We store this value to make sending the nag email business logic easier.',
    'schema field' => 'dew_survey_completed_count',
    'getter callback' => 'entity_property_verbatim_get',
    'setter callback' => 'entity_property_verbatim_set',
  );
  $properties['dew_surveys_all_completed'] = array(
    'label' => t('Has the student completed all their Surveys?'),
    'type' => 'integer',
    'description' => 'This is a boolean indictator (0,1) of whether or not a student has completed their surveys. We store this value to make sending the nag email business logic easier.',
    'schema field' => 'dew_surveys_all_completed',
    'getter callback' => 'entity_property_verbatim_get',
    'setter callback' => 'entity_property_verbatim_set',
  );
  return $info;
}

entity_property_verbatim_set and *_get just pull the field from the database if $info['schema field'] is defined.

I also had to make the DB changes in my .install file with db_add_field() for each database column. I also had to notify EntityAPI of my schema changes to the user table via hook_schema_alter(). All these DB schema fields and EntityAPI meta data field defintions must correlate properly.


if its more complicated, you do something like found in Ubercart products, you're just returning the item from the given Node and Field.

See http://api.ubercart.me/api/drupal/ubercart%21uc_order%21uc_order.module/function/uc_order_uc_order_get_properties/7.

EDIT

Apparently the entity_metadata_* functions have been deprecated and you should call entity_property_verbatim_get(), or entity_property_verbatim_set().

Tags:

Entities