this question explains how to add code to publish owned objects from objects which are not Versioned.
Using basically the same code without the owner
part (because we are using extended DataObject
s not Extensions
), the owned objects are published when the objects are saved, but the owned objects UI interface is not updated so the untrained or forgetful user doesn't know that their owned object is actually published.
Here is the source code that publishes owned objects on an object that is not versioned:
class SomeDataObject extends DataObject
{
private static $has_one = [
'SomeFileThatIsVersioned' => File::class,
]
// somewhat pointless owns declaration
private static $owns = [
'SomeFileThatIsVersioned',
];
public function onAfterWrite()
{
parent::onAfterWrite();
// Manually force publish since owns doesn't work when object is not Versioned
$ownsKeys = array_keys(self::$owns);
foreach ($ownsKeys as $key) {
$keyID = $key . "ID";
if ($this->$keyID) {
$this->$key()->publishSingle();
}
}
}
Appearance after save
Notice that the pseudo-"owned" object still shows as Draft
Appearance after save and page refresh
Notice how pseudo-"owned" object no longer shows Draft
status.