Accessing values in the $node variable..
Probably a simple syntax question but as a non-programmer I am battling with it.. I have created an "Invoice" content type that uses an entity reference to a "Patient" content type and within that there is a field collection for addresses.. I am trying to get the address in the first item in the field collection.. My guess is that I have to use something like "entity_load" which I will have to work out when I get there but at the moment I can't work out the syntax for accessing the field collection ID so I can try and load the data for the field collection.. Below is the content in the $node variable.. Can someone point me in the right direction to access the "field_pat_fc_addr[und][0][value]".. Thanks.. <?php print_r($node); ?> ##################################################### stdClass Object ( [vid] => 21 [uid] => 1 [title] => Inv. 26 - Doe, Mrs. Jane [log] => [status] => 1 [comment] => 0 [promote] => 0 [sticky] => 0 [nid] => 21 [type] => invoice [language] => und [created] => 1369041537 [changed] => 1369318484 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1369318484 [revision_uid] => 1 [field_inv_num] => Array ( [und] => Array ( [0] => Array ( [value] => 26 ) ) ) [field_inv_date] => Array ( [und] => Array ( [0] => Array ( [value] => 2013-05-20 00:00:00 [timezone] => Europe/London [timezone_db] => Europe/London [date_type] => datetime ) ) ) [field_ref_patient] => Array ( [und] => Array ( [0] => Array ( [target_id] => 10 [entity] => stdClass Object ( [vid] => 10 [uid] => 1 [title] => Doe, Mrs. Jane [log] => [status] => 1 [comment] => 0 [promote] => 0 [sticky] => 0 [nid] => 10 [type] => patient [language] => und [created] => 1358202099 [changed] => 1358263267 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1358263267 [revision_uid] => 1 [field_pat_num] => Array ( [und] => Array ( [0] => Array ( [value] => 1555 ) ) ) [field_pat_name_title] => Array ( [und] => Array ( [0] => Array ( [value] => Mrs. [format] => [safe_value] => Mrs. ) ) ) [field_pat_name_first] => Array ( [und] => Array ( [0] => Array ( [value] => Jane [format] => [safe_value] => Jane ) ) ) [field_pat_name_last] => Array ( [und] => Array ( [0] => Array ( [value] => Doe [format] => [safe_value] => Doe ) ) ) [field_pat_fc_addr] => Array ( [und] => Array ( [0] => Array ( [value] => 15 [revision_id] => 15 ) ) ) [field_pat_referred_by] => Array ( ) [field_pat_acc_status] => Array ( [und] => Array ( [0] => Array ( [value] => active ) ) ) [field_ref_practice] => Array ( [und] => Array ( [0] => Array ( [target_id] => 1 ) ) ) [field_pat_fc_tel] => Array ( ) [field_pat_dob] => Array ( [und] => Array ( [0] => Array ( [value] => 1994-01-26 00:00:00 [timezone] => Europe/London [timezone_db] => Europe/London [date_type] => datetime ) ) ) [field_pat_email] => Array ( ) [field_pat_filing_ref] => Array ( ) [field_pat_medical_cond] => Array ( ) [field_pat_recall] => Array ( ) [field_pat_notes] => Array ( ) [field_pat_fc_treatment] => Array ( ) [field_pat_attachments] => Array ( ) [print_display] => 1 [print_display_comment] => 0 [print_display_urllist] => 1 [name] => admin [picture] => 0 [data] => b:0; ) [access] => 1 ) ) ) [field_inv_fc_items] => Array ( [und] => Array ( [0] => Array ( [value] => 29 [revision_id] => 29 ) [1] => Array ( [value] => 30 [revision_id] => 30 ) ) ) [field_inv_total] => Array ( [und] => Array ( [0] => Array ( [value] => 120.00 ) ) ) [field_inv_invoiced] => Array ( [und] => Array ( [0] => Array ( [value] => 0 ) ) ) [field_inv_paid] => Array ( [und] => Array ( [0] => Array ( [value] => 0 ) ) ) [print_display] => 1 [print_display_comment] => 0 [print_display_urllist] => 1 [name] => admin [picture] => 0 [data] => b:0; [entity_view_prepared] => 1 )
Here's some code I use for dealing with collections. Perhaps it will help you to solve you problem. $collection = field_get_items($entity_type, $entity, 'field_consultant_cost_breakout'); $count = count($collection); $total = 0; for ($i = 0; $i < $count; ++$i) { // Entity_load will return an array with the entity number as the key. $item = array_pop(entity_load('field_collection_item', array($entity->field_consultant_cost_breakout[LANGUAGE_NONE][$i]['value']))); $total += $item->field_total_cost[LANGUAGE_NONE][0]['value']; } Nancy Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L. King, Jr.
________________________________ From: Wipe_Out <wipe_out@users.sourceforge.net> To: support@drupal.org Sent: Tuesday, May 28, 2013 6:05 AM Subject: [support] Accessing values in the $node variable..
Probably a simple syntax question but as a non-programmer I am battling with it..
I have created an "Invoice" content type that uses an entity reference to a "Patient" content type and within that there is a field collection for addresses.. I am trying to get the address in the first item in the field collection..
My guess is that I have to use something like "entity_load" which I will have to work out when I get there but at the moment I can't work out the syntax for accessing the field collection ID so I can try and load the data for the field collection..
If don't mind trying something new, I'd recommend using entity_metadata_wrapper from the entity API. Here's a specific example<http://www.appnovation.com/part_2_entity_metadata_wrappers_and_entity_api>using field collections and a more general example <https://drupal.org/node/1021556>. I've found entity_metadata_wrapper to be much easier than dealing with $node->field_foo [LANGUAGE_NONE][0]['value'] all over the place. Place a dependency on entity in your .info file and have fun. Lucas MTech, LLC <http://www.mtechinformationsolutions.com> On Tue, May 28, 2013 at 7:59 AM, Nancy Wichmann <nan_wich@bellsouth.net>wrote:
Here's some code I use for dealing with collections. Perhaps it will help you to solve you problem. $collection = field_get_items($entity_type, $entity, 'field_consultant_cost_breakout'); $count = count($collection);
$total = 0; for ($i = 0; $i < $count; ++$i) { // Entity_load will return an array with the entity number as the key. $item = array_pop(entity_load('field_collection_item', array($entity->field_consultant_cost_breakout[LANGUAGE_NONE][$i]['value']))); $total += $item->field_total_cost[LANGUAGE_NONE][0]['value']; }
*Nancy* Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L. King, Jr.
------------------------------ *From:* Wipe_Out <wipe_out@users.sourceforge.net> *To:* support@drupal.org *Sent:* Tuesday, May 28, 2013 6:05 AM *Subject:* [support] Accessing values in the $node variable..
Probably a simple syntax question but as a non-programmer I am battling with it..
I have created an "Invoice" content type that uses an entity reference to a "Patient" content type and within that there is a field collection for addresses.. I am trying to get the address in the first item in the field collection..
My guess is that I have to use something like "entity_load" which I will have to work out when I get there but at the moment I can't work out the syntax for accessing the field collection ID so I can try and load the data for the field collection..
-- [ Drupal support list | http://lists.drupal.org/ ]
Thanks.. I will look into those options and see if I can get there.. On 28 May 2013 14:14, Lucas D Hedding <lucashedding@gmail.com> wrote:
If don't mind trying something new, I'd recommend using entity_metadata_wrapper from the entity API.
Here's a specific example<http://www.appnovation.com/part_2_entity_metadata_wrappers_and_entity_api>using field collections and a more general example <https://drupal.org/node/1021556>. I've found entity_metadata_wrapper to be much easier than dealing with $node-> field_foo[LANGUAGE_NONE][0]['value'] all over the place. Place a dependency on entity in your .info file and have fun.
Lucas MTech, LLC <http://www.mtechinformationsolutions.com>
On Tue, May 28, 2013 at 7:59 AM, Nancy Wichmann <nan_wich@bellsouth.net>wrote:
Here's some code I use for dealing with collections. Perhaps it will help you to solve you problem. $collection = field_get_items($entity_type, $entity, 'field_consultant_cost_breakout'); $count = count($collection);
$total = 0; for ($i = 0; $i < $count; ++$i) { // Entity_load will return an array with the entity number as the key. $item = array_pop(entity_load('field_collection_item', array($entity->field_consultant_cost_breakout[LANGUAGE_NONE][$i]['value']))); $total += $item->field_total_cost[LANGUAGE_NONE][0]['value']; }
*Nancy* Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L. King, Jr.
------------------------------ *From:* Wipe_Out <wipe_out@users.sourceforge.net> *To:* support@drupal.org *Sent:* Tuesday, May 28, 2013 6:05 AM *Subject:* [support] Accessing values in the $node variable..
Probably a simple syntax question but as a non-programmer I am battling with it..
I have created an "Invoice" content type that uses an entity reference to a "Patient" content type and within that there is a field collection for addresses.. I am trying to get the address in the first item in the field collection..
My guess is that I have to use something like "entity_load" which I will have to work out when I get there but at the moment I can't work out the syntax for accessing the field collection ID so I can try and load the data for the field collection..
-- [ Drupal support list | http://lists.drupal.org/ ]
-- [ Drupal support list | http://lists.drupal.org/ ]
Hi, The easiest method to deal with references and field collections is with the entity wrapper which is in the entity module. http://www.drupalfunctions.com/api/rules/entity--entity.module/function/enti... so in this case you would do something like $wrappednode = entity_metadata_wrapper($node); then to access to the field do the following. $wrappednode->field_ref_patient->field_pat_fc_addr I can't see what the fields are in the field_pat_fc_addr entity, you should get the idea. The entity wrapper will lazy load the additions fields, and also as you can see you don't need to worry about language as well. Also you can do things like $wrappednode->author->name will return the user name of the author. Gordon. On 28/05/2013, at 8:05 PM, Wipe_Out <wipe_out@users.sourceforge.net> wrote:
Probably a simple syntax question but as a non-programmer I am battling with it..
I have created an "Invoice" content type that uses an entity reference to a "Patient" content type and within that there is a field collection for addresses.. I am trying to get the address in the first item in the field collection..
My guess is that I have to use something like "entity_load" which I will have to work out when I get there but at the moment I can't work out the syntax for accessing the field collection ID so I can try and load the data for the field collection..
Below is the content in the $node variable.. Can someone point me in the right direction to access the "field_pat_fc_addr[und][0][value]"..
Thanks..
<?php print_r($node); ?> ##################################################### stdClass Object ( [vid] => 21 [uid] => 1 [title] => Inv. 26 - Doe, Mrs. Jane [log] => [status] => 1 [comment] => 0 [promote] => 0 [sticky] => 0 [nid] => 21 [type] => invoice [language] => und [created] => 1369041537 [changed] => 1369318484 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1369318484 [revision_uid] => 1 [field_inv_num] => Array ( [und] => Array ( [0] => Array ( [value] => 26 )
)
)
[field_inv_date] => Array ( [und] => Array ( [0] => Array ( [value] => 2013-05-20 00:00:00 [timezone] => Europe/London [timezone_db] => Europe/London [date_type] => datetime )
)
)
[field_ref_patient] => Array ( [und] => Array ( [0] => Array ( [target_id] => 10 [entity] => stdClass Object ( [vid] => 10 [uid] => 1 [title] => Doe, Mrs. Jane [log] => [status] => 1 [comment] => 0 [promote] => 0 [sticky] => 0 [nid] => 10 [type] => patient [language] => und [created] => 1358202099 [changed] => 1358263267 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1358263267 [revision_uid] => 1 [field_pat_num] => Array ( [und] => Array ( [0] => Array ( [value] => 1555 )
)
)
[field_pat_name_title] => Array ( [und] => Array ( [0] => Array ( [value] => Mrs. [format] => [safe_value] => Mrs. )
)
)
[field_pat_name_first] => Array ( [und] => Array ( [0] => Array ( [value] => Jane [format] => [safe_value] => Jane )
)
)
[field_pat_name_last] => Array ( [und] => Array ( [0] => Array ( [value] => Doe [format] => [safe_value] => Doe )
)
)
[field_pat_fc_addr] => Array ( [und] => Array ( [0] => Array ( [value] => 15 [revision_id] => 15 )
)
)
[field_pat_referred_by] => Array ( )
[field_pat_acc_status] => Array ( [und] => Array ( [0] => Array ( [value] => active )
)
)
[field_ref_practice] => Array ( [und] => Array ( [0] => Array ( [target_id] => 1 )
)
)
[field_pat_fc_tel] => Array ( )
[field_pat_dob] => Array ( [und] => Array ( [0] => Array ( [value] => 1994-01-26 00:00:00 [timezone] => Europe/London [timezone_db] => Europe/London [date_type] => datetime )
)
)
[field_pat_email] => Array ( )
[field_pat_filing_ref] => Array ( )
[field_pat_medical_cond] => Array ( )
[field_pat_recall] => Array ( )
[field_pat_notes] => Array ( )
[field_pat_fc_treatment] => Array ( )
[field_pat_attachments] => Array ( )
[print_display] => 1 [print_display_comment] => 0 [print_display_urllist] => 1 [name] => admin [picture] => 0 [data] => b:0; )
[access] => 1 )
)
)
[field_inv_fc_items] => Array ( [und] => Array ( [0] => Array ( [value] => 29 [revision_id] => 29 )
[1] => Array ( [value] => 30 [revision_id] => 30 )
)
)
[field_inv_total] => Array ( [und] => Array ( [0] => Array ( [value] => 120.00 )
)
)
[field_inv_invoiced] => Array ( [und] => Array ( [0] => Array ( [value] => 0 )
)
)
[field_inv_paid] => Array ( [und] => Array ( [0] => Array ( [value] => 0 )
)
)
[print_display] => 1 [print_display_comment] => 0 [print_display_urllist] => 1 [name] => admin [picture] => 0 [data] => b:0; [entity_view_prepared] => 1 ) -- [ Drupal support list | http://lists.drupal.org/ ]
participants (4)
-
Gordon Heydon -
Lucas D Hedding -
Nancy Wichmann -
Wipe_Out