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?

  • 1
    Did you try entity metadata wrappers? – Kevin Apr 3 '17 at 0:18
  • @Kevin, nope, didn't even know they existed... – MeltingDog Apr 3 '17 at 1:23
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']);

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.