0

I need to prepopulate my embedded field collection values in a specific form. Here is what I've got so far:

<?php
// Hook form alter
function mp3_template_form_alter(&$form, &$form_state, $form_id)
{
  // Pre fill profile mp3 form with mp3 template data
  if (isSet($form_id) && $form_id == 'profile_mp3_node_form')
  {
    global $user;
    $user_full = profile2_load_by_user($user->uid, 'artists_profile');

    // Get field collection entity ids from the user
    $entity_ids = $user_full->field_copyrights_mp3[LANGUAGE_NONE];

    // Load field collection entities
    $entities = entity_load('field_collection_item', $entity_ids);

    // Here I want to prepopulate the $form['field_copyrights_mp3'] form field with my collection items that contain the data for field_copyrights_mp3
    $form['field_copyrights_mp3'][LANGUAGE_NONE][0] = $entities[0]; // 1 field collection
    $form['field_copyrights_mp3'][LANGUAGE_NONE][1] = $entities[1]; // 2 field collection
    // etc...
  }
}

I have searched a lot and tried multiple approaches to prefill my field collection with multiple rows of data. Hopefully someone can point me into the right direction of what I should do next.

Image of what I'm trying to prepopulate with multiple owners: enter image description here

0

you can use the MODULENAME_form_alter hook to achieve this, by iterating through its nested array, maybe this can help

If this help, awesome, ping me if not, I can share some snippets that achieve that :)

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.