1
  • I am having a field collection in a content type called 'client'. There are,

    1. Client name
    2. Email
    3. Mobile
    4. Address 1
    5. Address 2
    6. Address 3
    7. etc.. as fields. That's it in the client content type.
  • I am having another content type which basically has a textfield and a dropdown list

  • The texfield allows to enter a client's name
  • Once the client's name is filled in the textfield the dropdown list should be filled with client's addresses

  • So what I am trying to do here is, programmatically generate the dropdown options by sending the client's name and getting their relevant addresses which was saved using the client content type.

  • I'm quite confused with DB structure for field collection values.

Would be great if you can help ASAP

Thank you.

1

You could try Field Collection Values

As it says, try

To get the values of the individual field items call the entity_load function:

<?php
  $node25 = node_load(25);
  $track_1 = entity_load('field_collection_item', array($node25->field_track[LANGUAGE_NONE][0]['value']));
  $track_2 = entity_load('field_collection_item', array($node25->field_track[LANGUAGE_NONE][1]['value']));
?>
1

This is not a complete working example you can just copy and paste but it has the elements you need (I can't do it exact as I don't know all your field names etc.).

// This should be set to the client name that has been entered into the client name field.
$client_name = 'John Doe';
// Get field collection items with the given client name.
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'field_collection_item')
  ->entityCondition('bundle', 'FIELD_COLLECTION_NAME')
  ->fieldCondition('CLIENT_NAME_FIELD_ID', 'value', $client_name);

$result = $query->execute();

$field_collection_items = array();
if (isset($result['field_collection_item'])) {
  $field_collection_item_ids = array_keys($result['field_collection_item']);
  $field_collection_items = field_collection_item_load_multiple($field_collection_item_ids);
}

// If you expect there to only ever be one field collection item for each name, do this:
// From your description it seems like you only want to deal with a single one.
// This will be your client field collection item.
$client_field_collection_item = reset($field_collection_items);

// Otherwise, loop through the items that match the name and do something with each one.
// However, based on your question I will stick with the above single item.
foreach ($field_collection_items as $field_collection_item) {
  // Do something with each $field_collection_item here.
}

// To get the value of one of the fields of the field collection item, do this:
$lang = field_language('field_collection_item', $client_field_collection_item, 'FIELD_NAME');
$address1 = '';
if (!empty($client_field_collection_item[$lang][0])) {
  $address1 = $client_field_collection_item[$lang][0]['value'];
}

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.