【发布时间】:2021-01-29 16:48:07
【问题描述】:
我在媒体内容类型上创建了图像字段,然后在文章节点中创建了媒体引用字段。
我想从图像路径中检索媒体 ID。
【问题讨论】:
我在媒体内容类型上创建了图像字段,然后在文章节点中创建了媒体引用字段。
我想从图像路径中检索媒体 ID。
【问题讨论】:
这是从文件名中获取媒体 ID 的方法
// Load file by filename
// array.
$file = $this->entityTypeManager
->getStorage('file')
->loadByProperties(['filename' => $file_name]);
// Get First file (make a loop if you get many files)
$fileId = array_shift($file)->fid->value;
// Array of Medias witch contains your file.
$this->entityTypeManager
->getStorage('media')
->loadByProperties(['field_media_image' => $fileId]);
【讨论】: