SPECIAL OFFER: Only $69 for access to everything in OSTraining for 1 year! You save $75!  1
Join today and get access to 1,000's of books and videos. Learn WordPress, Drupal, Magento, Joomla and more! Sign up today!

Field Collection and entity_metadata_wrapper

6 years 3 weeks ago #117390 by marleneayers
Field Collection and entity_metadata_wrapper was created by marleneayers
I am desperately trying to use the entity_metadata_wrapper in my theme__preprocess_field function. I am following the directions from fourword.fourkitchens.com/article/better...me-field-collections and doing fine. But now I have a field that is part of the field_collection called that has multiple images in the field.

function rows_from_field_collection(&$vars, $field_name, $field_array) {
$vars = array();

foreach($vars as $key => $item) {
$entity_id = $item;
//dsm($entity_id);
$entity = field_collection_item_load($entity_id);
$wrapper = entity_metadata_wrapper('field_collection_item', $entity);

$row = array();
foreach($field_array as $field){
$row[$field] = $wrapper->$field->value();
}
$vars[] = $row;

Please Log in to join the conversation.

6 years 3 weeks ago #117393 by steve
Hi Marlene

A couple of things here:

1) Could you explain more about the problem? Where are you getting stuck here?

2) We don't help with custom code I'm afraid www.ostraining.com/support-forum/faq/ But, we can give lots of guidance, and help you find a developer to help, if needed.

Please Log in to join the conversation.

6 years 3 weeks ago #117397 by marleneayers
Replied by marleneayers on topic Field Collection and entity_metadata_wrapper
So the problem is that emw (entitymetadatawrapper) returns properties but I need to use the uri for the image fields. PLUS this is a multi field. I can make this work in the tpl.php file but I am trying to move the logic to the template.php file.

Please Log in to join the conversation.

6 years 3 weeks ago #117399 by marleneayers
Replied by marleneayers on topic Field Collection and entity_metadata_wrapper
I think I am not understanding how the emw returns values to the vars array

Please Log in to join the conversation.

6 years 3 weeks ago #117400 by steve
Ok, great.

Are you familiar with theme functions and how to create those in your template.php file?

Please Log in to join the conversation.

6 years 3 weeks ago #117401 by marleneayers
Replied by marleneayers on topic Field Collection and entity_metadata_wrapper
Yes. If you look at my first post I already have a theme function.
function glendale_preprocess_field(&$vars, $hook) {
// For all Field collections

if ($vars == 'field_main_page_accordion') {
$vars[] = 'field_main_page_accordion';
$field_array = array('field_accordion_header', 'field_accordion_subhead', 'field_accordion_content', 'field_accordion_images',);
rows_from_field_collection($vars, 'field_main_page_accordion', $field_array);
}



}

Please Log in to join the conversation.

6 years 3 weeks ago - 6 years 3 weeks ago #117402 by steve
Sorry, I just saw this in the first post:
function rows_from_field_collection

OK, so moving on to code, entity metadata wrappers this is the best documentation you'll find: www.drupal.org/node/1021556 Does the very last item on that page help?

To be able to see the values you can write your own helper to debug wrapped object

<?php
function _wrapper_debug($w) {
  $values = array();
  foreach ($w->getPropertyInfo() as $key => $val) {
    $values[$key] = $w->$key->value();
  }
  return $values;
}
?>


This may also help: www.pixelite.co.nz/article/how-use-entit...a-wrappers-drupal-7/

As might this:
drupalcommerce.org/developer-guide/utili...ty-metadata-wrappers

In some cases, you may need access to the raw value of a property or field. This is true in cases where you just want the ID of a referenced entity instead of the entity itself. In these instances, use the ->raw() method of a wrapper instead of ->value().

Please Log in to join the conversation.

6 years 3 weeks ago #117403 by marleneayers
Replied by marleneayers on topic Field Collection and entity_metadata_wrapper
I have tried that many times and could not get it to work. I will try again. :)

Please Log in to join the conversation.

6 years 3 weeks ago #117404 by marleneayers
Replied by marleneayers on topic Field Collection and entity_metadata_wrapper
function _wrapper_debug($wrapper) {
$values = array();
foreach ($wrapper->getPropertyInfo() as $key => $val) {
$values[$key] = $w->$key->value();
}
return $values;
dsm($values);
}

error: Warning: Invalid argument supplied for foreach() in element_children() (line 6525 of .../drupal7/includes/common.inc). no output

Please Log in to join the conversation.

6 years 3 weeks ago #117405 by marleneayers
Replied by marleneayers on topic Field Collection and entity_metadata_wrapper
Sorry. I used this and got the same error
function _wrapper_debug($w) {
$values = array();
foreach ($w->getPropertyInfo() as $key => $val) {
$values[$key] = $w->$key->value();
}
return $values;
dsm($values);
}
and called it using _wrapper_debug($wrapper);

Please Log in to join the conversation.

6 years 3 weeks ago #117406 by marleneayers
Replied by marleneayers on topic Field Collection and entity_metadata_wrapper
So now I can get some output but I have been here before. I don't what to do with this output.

I have attached an image

Please Log in to join the conversation.

6 years 3 weeks ago #117407 by marleneayers
Replied by marleneayers on topic Field Collection and entity_metadata_wrapper
My understanding is that this information is living in the entity metadata wrapper object called $wrapper. I don't know how to get it to an array I can use inside my preprocess function. I see there is a row array but I don't understand how to add this information to it.

Please Log in to join the conversation.

6 years 3 weeks ago #117409 by marleneayers
Replied by marleneayers on topic Field Collection and entity_metadata_wrapper
anything?

Please Log in to join the conversation.

6 years 3 weeks ago - 6 years 3 weeks ago #117412 by steve
Hi Marlene

Sorry, just to re-iterate, we don't help write custom code: www.ostraining.com/support-forum/faq/

We do our best to give guidance, however. Could you attach the image with your output again?

For breaking apart the functions, are you using the Devel and Theme Developer modules? That will be a big help: www.ostraining.com/courses/session/drupa...ing/overrides/tools/

I'll ask our developers to see if any of them can offer advice here too.

Please Log in to join the conversation.

Join today and get access to 1,000's of books and videos. Learn WordPress, Drupal, Magento, Joomla and more! Sign up today!