programmatically created media files are not showing up in the views

Created on 31 May 2024, 6 months ago
Updated 9 July 2024, 5 months 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 about 4 hours ago

Created by

🇲🇲Myanmar zawlinnoo

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @zawlinnoo
  • 🇦🇺Australia larowlan 🇦🇺🏝.au GMT+10
  • Assigned to mayurgajar
  • 🇮🇳India mayurgajar

    This is the code, I used to create a media and node programmatically

    // Stored file
    $image_url = {image url};
    $image_content = file_get_contents($image_url);
    $filename = basename($image_url);
    $file_repository = \Drupal::service('file.repository');

    $image = $file_repository->writeData($image_content, "private://". $filename, FileSystemInterface::EXISTS_REPLACE);

    // Create a media
    $media_storage = \Drupal::entityTypeManager()->getStorage('media');
    $media = $media_storage->create([
    'bundle' => 'image',
    'uid' => 1,
    'status' => 1,
    'field_media_image' => [
    'target_id' => $image->id(),
    'alt' => $text,
    'title' => $text,
    ],
    ]);
    $media->setName($text)->setPublished(TRUE)->save();

    // Create a node
    $node = Node::create([
    'type' => 'article',
    'title' => 'new article',
    'field_blog_image' => [
    'target_id' => $media->id(),
    'alt' => 'Hello world',
    'title' => 'Goodbye world'
    ],
    ]);
    $node->save();

    With this code, we are able to add media and node..
    Next step, Create a view of article node and display title and media image - we are able to create a view with title and media image and it rendering correct

    Please add any other scenarios if you are still facing any issue to create a media and its view.
    For better understanding debug your code like, first check file is created or not.. then check media is created or not and last check node is successfully saved or not

  • Issue was unassigned.
Production build 0.71.5 2024