Singapore
Account created on 7 May 2024, 7 months ago
#

Recent comments

πŸ‡ΈπŸ‡¬Singapore kunilkuda Singapore

Fix the issue of ConfigFormBase forms throwing __construct error. Tested on Drupal v11.0.5 and Structure Sync v2.0.7.

Please merge with the upcoming release because the module is totally broken without the patch.

πŸ‡ΈπŸ‡¬Singapore kunilkuda Singapore

Thank you πŸ‘. I'll try this next time.

Along the way I found Drupal RESTful module (and it can export raw values). So I used it to build my view, export to REST then I use JQuery to fetch and insert it to my page. 

πŸ‡ΈπŸ‡¬Singapore kunilkuda Singapore

The code that you posted worked too.. You're right, I need to make sure that there's image posted in the field. 

πŸ‡ΈπŸ‡¬Singapore kunilkuda Singapore

Finally, I found the way.. To do this your post must have an image being set. Otherwise getValue()[0] will return null, even though you have set a default image.

$image_field = $post->field_featured_image->getValue()[0];
$image_url = null;

if (!is_null($image_field)) {
  $file_id = $image_field['target_id'];
  $file = \Drupal\file\Entity\File::load($file_id);
  $image_url = \Drupal::service('file_url_generator')->generateAbsoluteString($file->getFileUri());
}

// This will be sent to the twig template
$current_post['featured_image'] = $image_url;
πŸ‡ΈπŸ‡¬Singapore kunilkuda Singapore

Thanks @Jaypan.

$file_uri = $post->field_featured_image->entity->getFileUri();
// Error: Call to a member function getFileUri() on null 

I tried your snippet, and I got the error that I tried to "call member function getFileUri() on null". So my assumption is that

$post->field_featured_image->entity

is returning null (or there's no member called 'entity' ).

πŸ‡ΈπŸ‡¬Singapore kunilkuda Singapore

Hi @Jaypan,

Thanks for the reply. The 'featured_image' is an image type field. 

πŸ‡ΈπŸ‡¬Singapore kunilkuda Singapore

Hi,

I've tried the same thing with Drupal 10.2. My module template is only used when the module is rendered in the main theme region. My solution is that I built a custom theme (making my own page.html.twig as well) to match my module. 

Production build 0.71.5 2024