programmatically created media files are not showing up in the views

Created on 31 May 2024, 29 days ago

programmatically created media files are not showing up in the Views after attaching on the Node::create(). until its manually save that node, then showing in the Views. Re-saving node after creating from Node::create() not working as well. thanks in advance.

$current_user_id = \Drupal::currentUser()->id();
$language_code = \Drupal::languageManager()->getCurrentLanguage()->getId();
$image_data = file_get_contents($params['image']);
$filename = basename($params['image']);
$file_repository = \Drupal::service('file.repository');

$image = $file_repository->writeData($image_data, "private://". $filename, FileSystemInterface::EXISTS_REPLACE);
// Media Creation
$image_media = Media::create([
'name' => $filename,
'bundle' => 'image',
'uid' => $current_user_id,
'langcode' => $language_code,
'status' => 1,
"thumbnail" => [
"target_id" => $image->id(),
"alt" => t('My media alt attribute'),
],
'field_media_image' => [
'target_id' => $image->id(),
'alt' => t('My media alt attribute'),
'title' => t('My media title attribute'),
],
'field_author' => 'admin'
]);
$image_media->save();

// node creation
$node = Node::create([
'type' => 'article',
'title' => 'test',
'body' => 'test,
'field_image' => [
'target_id' => $image_media->id(),
'alt' => t('My media alt attribute'),
'title' => t('My media title attribute'),
],
'status' => 1,
]);
$node->save();
$node = Node::load($event_node->id());
$node->save();

πŸ’¬ Support request
Status

Active

Version

10.2 ✨

Component
ViewsΒ  β†’

Last updated less than a minute ago

Created by

πŸ‡²πŸ‡²Myanmar zawlinnoo

Live updates comments and jobs are added and updated live.
  • views

    Involves, uses, or integrates with views. In Drupal 8 core, use the β€œVDC” tag instead.

Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024