VersionX icon indicating copy to clipboard operation
VersionX copied to clipboard

No resource content with Revo 3 on resource creation

Open rtripault opened this issue 3 years ago • 0 comments

On Revo 3, when you create a new resource, the modal seems not to contain any content field, resulting in versionx plugin triggering the following kind of error.

INSERT INTO `versionx_resource` (`content_id`, `user`, `mode`, `marked`, `title`, `context_key`, `class`, `fields`, `tvs`) VALUES (...)
Array
(
    [0] => HY000
    [1] => 1364
    [2] => Field 'content' doesn't have a default value
)

Not sure about minimum php version supported by the project, but editing VersionX::newResourceVersion

    public function newResourceVersion($resource, $mode = 'upd') {
        if ($resource instanceof \MODX\Revolution\modResource || $resource instanceof modResource) {
            // We're retrieving the resource again to clean up raw post data we don't want.
            $resource = $this->modx->getObject('modResource',$resource->get('id'));
        } else {
            $resource = $this->modx->getObject('modResource',(int)$resource);
        }

        $rArray = $resource->toArray();

        /* @var vxResource $version */
        $version = $this->modx->newObject('vxResource');

        $v = array(
            'content_id' => $rArray['id'],
            'user' => $this->modx->user->get('id'),
            'mode' => $mode,
            'title' => (empty($rArray[$this->modx->getOption('resource_tree_node_name')]) ? $rArray['pagetitle'] : $rArray[$this->modx->getOption('resource_tree_node_name')]),
            'context_key' => $rArray['context_key'],
            'class' => $rArray['class_key'],
            'content' => $resource->get('content') ?? '', // workaround is here
        );

        $version->fromArray($v);

        unset ($rArray['id'],$rArray['content']);
        $version->set('fields',$rArray);

        $tvs = $resource->getTemplateVars();
        $tvArray = array();
        /* @var \MODX\Revolution\modTemplateVar|modTemplateVar $tv */
        foreach ($tvs as $tv) {
            $tvArray[] = $tv->get(array('id','value'));
        }
        $version->set('tvs',$tvArray);

        if($this->checkLastVersion('vxResource', $version)) {
            return $version->save();
        }
        return true;
    }

did it for us (php 7.x+). Another option could be to set that DB field default as an empty string

rtripault avatar Jan 19 '23 12:01 rtripault