【问题标题】:How to get "media id" from media "image name/path" drupal 8如何从媒体“图像名称/路径”drupal 8 中获取“媒体 ID”
【发布时间】:2021-01-29 16:48:07
【问题描述】:

我在媒体内容类型上创建了图像字段,然后在文章节点中创建了媒体引用字段。

我想从图像路径中检索媒体 ID。

【问题讨论】:

    标签: drupal drupal-8


    【解决方案1】:

    这是从文件名中获取媒体 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]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-06
      • 1970-01-01
      • 2019-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多