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

1.5k Views Asked by At

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?

2

There are 2 best solutions below

0
On BEST ANSWER

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);
0
On

I know you specified "from php" but accessing those values from paragraph template can be also helpful to someone. I.e. if paragraph has image field called "field_image" and you want to print image in "image_style_name" style you can do it from paragraph template like this:

<img
        alt="{{ content.field_image['0']['#item'].alt }}"
        title="{{ content.field_image['0']['#item'].alt }}"
        src="{{ content.field_image['0']['#item'].entity.uri.value|change_image_style('image_style_name') }}" >