How to automaticaly publish files uploaded to a dataobject in Silverstripe model admin
Asked Answered
L

3

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.

Locality answered 8/1, 2018 at 5:19 Comment(0)
R
4

This is currently discussed on GitHub on multiple repositories.

The solution at the moment, is either publish the images manually in onAfterWrite, or version your DataObject, preferably via YML:

My\Data\Object
  extensions:
    - Versioned
Regretful answered 8/1, 2018 at 11:33 Comment(2)
Why is it preferable to do it in YML as opposed to @Fatal Errors method?Locality
As YML is the primary source for configuration settings for SilverStripe, and PHP is the fallback method. As per the documentation: "SilverStripe comes with a comprehensive code based configuration system through the Config class. It primarily relies on declarative YAML files, and falls back to procedural PHP code, as well as PHP static variables." This means especially performance wise, YML is the better way to go.Regretful
I
5

Your dataobject needs to extend the Versioned extension. Pages already have this in the SiteTree object.

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

    private static $owns = [
        'ItemImage'
    ];

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

Edit

The above doesn't actually work for a ModelAdmin, only for objects related to an object that already is 'Versioned' (like SiteTree).
If you want to this from a ModelAdmin you could add the following:

private static $versioned_gridfield_extensions = true;

Which will create a few buttons in your ModelAdmin. After you click publish, the File will publish too.

Impure answered 8/1, 2018 at 6:56 Comment(1)
Why do you add this: . '.versioned' ?Clostridium
R
4

This is currently discussed on GitHub on multiple repositories.

The solution at the moment, is either publish the images manually in onAfterWrite, or version your DataObject, preferably via YML:

My\Data\Object
  extensions:
    - Versioned
Regretful answered 8/1, 2018 at 11:33 Comment(2)
Why is it preferable to do it in YML as opposed to @Fatal Errors method?Locality
As YML is the primary source for configuration settings for SilverStripe, and PHP is the fallback method. As per the documentation: "SilverStripe comes with a comprehensive code based configuration system through the Config class. It primarily relies on declarative YAML files, and falls back to procedural PHP code, as well as PHP static variables." This means especially performance wise, YML is the better way to go.Regretful
A
2

I'm a bit late, but unfortunately $owns still doesn't work on non-versioned DataObjects. I also didn't want to make it versioned, so here's how I am manually publishing them:

<?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();
        }
    }
}
Arsine answered 8/10, 2020 at 11:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.