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.
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.
The code that you posted worked too.. You're right, I need to make sure that there's image posted in the field.
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;
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' ).
Hi @Jaypan,
Thanks for the reply. The 'featured_image' is an image type field.
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.