- Issue created by @zawlinnoo
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();
Active
10.2 β¨
Last updated