0

Field creation code:

if (! field_info_field(MY_FIELD_NAME)) {

    field_create_field(array(
        'field_name => MY_FIELD_NAME,
        'type' => 'list_integer',
        'settings' => array(
            'allowed_values' => some_allowed_values(),
        ),
        'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    ));

    field_create_instance(array(
        'field_name' => MY_FIELD_NAME,
        'entity_type' => MY_ENTITY_NAME,
        'bundle' => MY_ENTITY_BUNDLE_NAME,
        'widget' => array(
            'type' => 'options_select',
        )
    ));

}

Setter code:

//posted options are list values keys e.g (3,4)
$wrapper->{MY_FIELD_NAME}->set(array_values($values['posted_options']));
//not work too:
$wrapper->{MY_FIELD_NAME}->set($values['posted_options']);
$wrapper->{MY_FIELD_NAME}->set(array($values['posted_options']));
//in for loop
$wrapper->{MY_FIELD_NAME}[] = $option_key;

Error:

EntityMetadataWrapperException: Invalid data value given. Be sure it matches the required data type and format. in EntityMetadataWrapper->set()

or

Fatal error: Cannot use object of type EntityValueWrapper as array

It works only when I set single value:

$wrapper->{MY_FIELD_NAME}->set($option_key);

This is not related to: Using entity metadata wrapper with custom entity type

because this is different field type.

Found solution:

   field_create_field(array(
        'field_name' => MY_FIELD_NAME,
        'type' => 'list_integer',
        'settings' => array(
            'allowed_values' => options_values(),
        ),
        'default_widget' => 'options_select',
        'default_formatter' => 'list_default',
        'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    ));

    field_create_instance(array(
        'field_name' => MY_FIELD_NAME,
        'entity_type' => MY_ENTITY_NAME,
        'bundle' => MY_ENTITY_BUNDLE_NAME,
        'widget' => array(
            'type' => 'options_select',
            'formatter' => 'list_default',
        )
    ));
3
  • @ar7max no, it's a different question. other field type.
    – Codium
    Commented Sep 5, 2015 at 10:16
  • ok, but I need moderator's response Commented Sep 5, 2015 at 10:22
  • I agree with the dup on this. You asked two question about custom fields w/ EFQ on back to back days, and I don't see what the difference is between the two?
    – mpdonadio
    Commented Sep 10, 2015 at 1:27

0

Browse other questions tagged or ask your own question.