7

I have a custom field with multiple values. I would like it to work with the Entity module. I need to create the correct property_callbacks in hook_field_info(). My field schema looks like this:

'ref' => array(
  'type' => 'varchar',
  'length' => 32, 
  'not null' => TRUE,
),
'title' => array(
  'type' => 'varchar',
  'length' => 255,
  'not null' => TRUE,
  'default' => '',
),
'description' => array(
  'type' => 'text',
  'not null' => TRUE,
  'size' => 'big',
),
'weight' => array(
  'type' => 'int',
  'unsigned' => TRUE,
  'not null' => TRUE,
  'default' => 0,
),
'status' => array(
  'type' => 'int',
  'unsigned' => TRUE,
  'not null' => TRUE,
  'default' => 1,
),

Does anyone know how to actually do this? There is very little documentation around for it.

2 Answers 2

5

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().

3
  • Thanks for the reply. Do you know if I should be doing the following: $property['getter callback'] = 'entity_metadata_field_verbatim_get'; $property['setter callback'] = 'entity_metadata_field_verbatim_set'; unset($property['query callback']); Commented Jul 12, 2012 at 18:14
  • I dont know what function you're trying to put that in. the "property getter" and setters go in hook_property_info() or hook_property_info_alter(), hook_field_info() is the Core drupal API hook_property_* is the EntityAPI. I have no other advice at this time.
    – tenken
    Commented Jul 12, 2012 at 18:24
  • I'm using hook_field_info and property_callbacks. I've just seen it in other examples so I guess I should add it. Either way, I've now tested the wrapper and it's saving correctly and getting the data as expected. Commented Jul 12, 2012 at 18:30
0

Here is simpler example:

/**
 * Callback to alter the property info of custom field.
 */
function MYMODULE_property_info_callback(&$info, $entity_type, $field, $instance, $field_type) {
  $name = $field['field_name'];
  $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$name];

  $property['type'] = 'MYFIELD';
  $property['getter callback'] = 'entity_metadata_field_verbatim_get';
  $property['setter callback'] = 'entity_metadata_field_verbatim_set';

  unset($property['query callback']);
}

This should be called from property_callbacks in your hook_field_info and it should automatically store your values per each property.

Your Answer

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

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