0

I've created custom entity. In addition I want to add user reference field to my entity:

if (! field_info_field(MY_FIELD_NAME)) {
  // Create the field.
   $field = array(
    'field_name' => MY_FIELD_NAME,
    'type' => 'entityreference',
    'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    'settings' => array(
        'target_type' => 'user',
        'handler' => 'base',
        'handler_settings' => array(
            'target_bundles' => array()
        ),
    ),
    'bundles' => array (
        MY_ENTITY_NAME =>
            array (
                0 => MY_ENTITY_BUNDLE_NAME,
            ),
    ),
);
field_create_field($field);

    // Create the instance.
    $instance = array(
        'field_name' => MY_FIELD_NAME,
        'entity_type' => MY_ENTITY_NAME,
        'bundle' => MY_ENTITY_BUNDLE_NAME,
        'label' => '',
        'description' => '',
        'required' => TRUE,
        'widget' => array(),
        'display' => array(
            'default' => array(
                'label' => 'hidden',
            ),
        ),
    );

    field_create_instance($instance);
}

In form submit I want to add users references to this field:

   foreach($result as $recipient) {
        $wrapper->{MY_FIELD_NAME}[] = $recipient->uid;
    }

I gets:

Fatal error: Cannot use object of type EntityDrupalWrapper as array

What I am missing?

  • Remove brackets – ar7max Sep 4 '15 at 12:31
  • Which brackets? It should be unlimited values field, so should work as an array – Codium Sep 4 '15 at 12:53
  • You can simply dpm($wrapper->{yourfield}) to see structure (devel module should be installed) – ar7max Sep 4 '15 at 13:00
  • I'm missing something huge because even I try to store in entity value without EMW, like: $entity->field_name[LANGUAGE_NONE][]['target_id'] = $id I get nothing in field db table. – Codium Sep 4 '15 at 20:16

Your Answer

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

Browse other questions tagged or ask your own question.