2

I followed the instruction in the post from here to create a compound field and it work like a charm. Now I am trying to fill each of the 3 "columns" that make the compound field programmatically and I can figure it out how.

I am using the entity API to create the node see the code below:

$node = entity_create('node', array('type' => 'MY-CONTENT-TYPE'));
$entity = entity_metadata_wrapper('node',  $node);

$data = array('column_1'=>'value 1', 'column_2'=>'value 2', 'column_3'=>'value 3');

$entity->field_custom_compound_field = $data;

$entity->save();

But it does not work. What am I missing?

Note: I keep getting the error:

EntityMetadataWrapperException: Unknown data property field_custom_compound_field.

  • By 'compound' do you mean a standard field that's got more than one value column? I've not come across the notion of 'sub-fields' before in Drupal, would be good to clear up what that refers to. Off the top of my head try changing the 4th code line to $entity->field_custom_compound_field[] = $data; and see what differences that makes – Clive Oct 1 '13 at 14:30
  • 1
    Yes, that is exactly what I mean. I call them sub fields because the make the compound field. – Emil Orol Oct 1 '13 at 14:31
  • 1
    Cool, Drupal refers to them as 'columns' in case you need to know that at any point. The bottom line is every field can potentially have more than one column (even those that don't) so setting them through a metadata wrapper will be exactly the same; the array will just have extra key/value pairs in it – Clive Oct 1 '13 at 14:32
  • Hi Clive thank you for the correction, I didn't know they were called columns I have updated the question with the right terminology and I also added the error I am getting. – Emil Orol Oct 1 '13 at 14:37
  • Looks like Entity API doesn't know about the field - if you dpm($entity->value() (assume Devel module is installed) do you see any data under field_custom_compound_field? – Clive Oct 1 '13 at 14:38
1

I am posting this here in case someone is having the same issue:

I use the tutorial located here to create my compound custom field with multiple columns what I didn't noticed was in the hook_fields_field_info() implementation I should define the type of data the entity will take. To make things worse in the tutorial the author use the example where all the fields he created are text fields, but in my case I have date, decimal and text fields so I had to define each type. I found the solution in this post.

In a nutshell in my hook_fields_field_info() implementation I have to declare the entity property type, but since I am using multiple columns on the field I have to implement a call back function that will allow me to define the property type for each of my columns.

/**
* Implements hook_field_info().
*
* Provides the description of the field.
*/
function field_example_field_info() {
  return array(
  // We name our field as the associative name of the array.
    'field_example_rgb' => array(
      'label' => t('Example Color RGB'),
      'description' => t('Demonstrates a field composed of an RGB color.'),
      'default_widget' => 'field_example_3text',
      'default_formatter' => 'field_example_simple_text',
      'property_type' => 'field_example_rgb', // we add the propert type here
      'property_callbacks' => array('field_example_rgb_property_info_callback'), // we add the callback
    ),
  );
}

Call back function to define each columns type:

function field_example_rgb_property_info_callback(&$info, $entity_type, $field, $instance, $field_type) {
  $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']];

  $property['getter callback'] = 'entity_metadata_field_verbatim_get';
  $property['setter callback'] = 'entity_metadata_field_verbatim_set';
  unset($property['query callback']);

  $property['property info']['rgb'] = array(
    'type' => 'text',
    'label' => t('RGB'),
    'setter callback' => 'entity_property_verbatim_set',
  );
  $property['property info']['someothervalue'] = array(
    'type' => 'text',
    'label' => t('Some other value'),
    'setter callback' => 'entity_property_verbatim_set',
  );
}

After doing that the following code:

$node = entity_create('node', array('type' => 'MY-CONTENT-TYPE'));
$entity = entity_metadata_wrapper('node',  $node);

$data = array('column_1'=>'value 1', 'column_2'=>'value 2', 'column_3'=>'value 3');

$entity->field_custom_compound_field = $data;

$entity->save();

Worked like a charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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