7

In Silverstripe 4 a file that is uploaded must be published before it is visible to the public side of the site.

If I create a $Page with a $has_one Image::Class and then also assign that image to $owns[] the uploaded image will be published when I publish the page.

However If I create the following data object structure it will not.

Class Item extends DataObject{
    $has_one[
        'ItemImage'=>Image::Class,
        'Catalog'=>'Catalog'
    ];

    $owns[
        'ItemImage'
    ]
}

Class Catalog extend DataObject{
    $has_many[
        'Items'=>'Item'
    ]
    $owns[
        'Items'
    ]

    public function getCMSFields(){
        $fields = parent::getCMSFields();

        $fields->addFieldToTab('Root.Items', GridField::create('Items', 'Items', $this->Items(), GridFieldConfig_RecordEditor::create()));

        return $fields;

    }
}

If I create a catalog and within it create items with images and then save it, it will not publish the images that were uploaded. I will have to manually: 1. Select the image 2. Edit Original 3. Publish

There has to be an easier way for the user.

4

3 回答 3

4

目前在 GitHub 上的多个存储库中对此进行了讨论。

目前的解决方案是在 onAfterWrite 中手动发布图像,或者版本您的 DataObject,最好通过 YML:

My\Data\Object
  extensions:
    - Versioned
于 2018-01-08T11:33:38.873 回答
4

您的数据对象需要扩展Versioned扩展名。页面已经在SiteTree对象中有这个。

Class Item extends DataObject
{
    private static $has_one = [
        'ItemImage' => Image::Class,
        'Catalog' => 'Catalog'
    ];

    private static $owns = [
        'ItemImage'
    ];

    private static $extensions = [
        Versioned::class . '.versioned'
    ];
}

编辑

上述内容实际上不适用于 ModelAdmin,仅适用于与已经“版本化”的对象(如 SiteTree)相关的对象。
如果您想从 ModelAdmin 中进行此操作,可以添加以下内容:

private static $versioned_gridfield_extensions = true;

这将在您的 ModelAdmin 中创建一些按钮。单击发布后,文件也会发布。

于 2018-01-08T06:56:11.743 回答
1

我有点晚了,但不幸的是 $owns 仍然不适用于非版本化的 DataObjects。我也不想对其进行版本控制,所以这是我手动发布它们的方式:

<?php

namespace app\foo;

use SilverStripe\ORM\DataObject;
use SilverStripe\Assets\Image;

class Bar extends DataObject {
    private static $has_one = [
        'MyImage' => Image::class
    ];

    protected function onAfterWrite() {
        parent::onAfterWrite();
        $img = $this->MyImage();
        if ($img && $img->exists()) {
            $img->publishRecursive();
        }
    }
}
于 2020-10-08T11:55:36.780 回答