Search code examples
drupaldrupal-modulesdrupal-8paragraphs

How do you access an image file in paragraphs module from php?


I can't seems to be able to access to a image field in a paragraphs module.

I can access to other fields value but not the image field.

          $paragraph = Paragraph::load($team_type['target_id']);
          $new_obj->title = $paragraph->field_title->value;      
          $new_obj->age = $paragraph->field_age->value;
          $new_obj->info = $paragraph->field_about_info->value;
          $new_obj->link = $paragraph->field_link_to_page->value;

          $new_obj->logo = $paragraph->field_logo_image->value;

The field_logo_image doesn't have anything in it. I did a google search but still can't able to find a solution.

I did checked the image field unique name and it's correct.

My question is, how do you access to an image field in Drupal 8 paragraphs module from your custom module?


Solution

  • This may not be exactly what you're looking for but i was able to get the image style url doing this.

          $paragraph = Paragraph::load($paragraph_image[0]['target_id']);
          $ogPara = $paragraph->get('field_image')->entity->uri->value;
          $style = ImageStyle::load('list_thumbnail_square_100x100');
          $url = $style->buildUrl($ogPara);