以编程方式使用 Bolt CMS 填充图像和图像列表字段

问题描述

我正在尝试在 Bolt 4 中以编程方式创建条目。我已经成功地创建了基本的文本条目,它涵盖了我需要填写的大多数字段,但不确定如何处理图像和图像列表类型。

$content->setFieldValue('name','Test Name');

适用于大多数字段,但图像字段类型在数据库中如下所示,我不确定下面生成的“Bolt / Symfony / Doctrine”方式是什么:

{"media":11,"filename":"entity\/year\/month\/image.jpg","alt":"","0":""} 

它看起来像一些 JSON 格式,包含媒体 ID、文件路径和 alt 属性。我猜图像列表是相似的,但上面有多个,但希望有一个函数可以用来生成这个输出,因为不确定我将如何获取媒体 ID 等。

我假设我可能需要临时从外部 URL 上传文件并将其提供给某些函数,但是找不到任何示例。任何帮助将不胜感激。

解决方法

对此不太确定,但无论如何都会回答,并且可能会帮助其他人,但最好清除一些问题。

图像示例:

//not sure what goes in media here but blank seemed to not work here but a 7 did as looked at example in database however worried this is wrong and should be an Id
$image = array('media' => '7','filename' => $filename,'alt' => $image['Alt'],'0' => '');

$imageJSON = json_encode($image);
$content->setFieldValue('image',$image);

图像列表示例:

$images = array();

foreach ($images as $image) {
     $ImageId = $image['id'];
     //images may be already on system but I had to download them here 
     if ($fileName = $this->downloadImage($propertyFile->{'url'}->__toString(),$ImageId)) {
         //not sure what goes in media here but seemed to work blank for imagelist type and unsure what the 0 is on the end either
         $image[] = array('media' => '','0' => '');  }
}

$imageJSON = json_encode($image);
$content->setFieldValue('gallery',$image);