0

I want to loop through Paragraph bundles and return a string (image path uri string).

I can view the Paragraph arrays as such:

foreach ($variables['field_paragraphs'] as $i) { $entity = entity_load('paragraphs_item', array($i['value'])); dpm($entity); } 

This returns each Paragraph bundle as an multidimensional array. I want to take the value of each uri key and place them in their own array.

To that effect I have tried $image_path = $entity['ParagraphsItemEntity']['field_image']['und']['0']['uri'];, but it returns nothing.

I've also tried making a function.

 function array_column_recursive(array $haystack, $needle) { $found = []; array_walk_recursive($haystack, function ($value, $key) use (&$found, $needle) { if ($key == $needle) { $found[] = $value; } }); return $found; } print(array_column_recursive($entity, 'uri')[0]); 

This gives me an error message.

Fatal error: Cannot redeclare array_column_recursive();

(It doesn't seem to matter what the function is named; it will always have this error.)

Would anyone know how to simply get a value from a multidimensional array in Drupal?

2
  • 1
    Did you try entity metadata wrappers? Commented Apr 3, 2017 at 0:18
  • @Kevin, nope, didn't even know they existed... Commented Apr 3, 2017 at 1:23

1 Answer 1

0

OK, here's what I did, after discovering Entity Metadata Wrappers:

//FORMATS ARRAY TO SOMETHING EASIER TO HANDLE $entity_wrapper = entity_metadata_wrapper('paragraphs_item', reset($entity)); //RETURN THE VALUE OF THE IMAGE ARRAY $image = $entity_wrapper->field_image->value(); //RETURN THE (FULL) URL OF THE IMAGE $file_url = file_create_url($image['uri']); 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.