<?php
use Drupal\media\Entity\Media;
use Drupal\Core\File\FileSystemInterface;
$image_data = file_get_contents('https://learndrupalway.com/themes/claro/images/learndrupal.png');
$file_repository = \Drupal::service('file.repository');
$image = $file_repository->writeData($image_data, "public://learndrupal.png", FileSystemInterface::EXISTS_REPLACE);
$media = Media::create([
'name' => 'Learn Drupal',
'bundle' => 'image',
'uid' => 1,
'langcode' => 'en',
'status' => 0,
'field_media_image' => [
'target_id' => $image->id(),
'alt' => t('Learn Drupal.png'),
'title' => t('Learn Drupal'),
],
]);
$media->save();
- Log in to post comments
Category