首页 > 解决方案 > 以编程方式使用 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 临时上传文件并将其提供给某些功能,但找不到任何示例。任何帮助将非常感激。

标签: symfonydoctrine-ormbolt-cms

解决方案


对此不太确定,但无论如何都会回答,因为它可以工作并且可能会帮助其他人,但最好清除一些位。

图像示例:

//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' => '', 'filename' => $filename, 'alt' => $image['Alt'], '0' => '');  }
}

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

推荐阅读