Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No resource content with Revo 3 on resource creation #122

Closed
rtripault opened this issue Jan 19, 2023 · 1 comment
Closed

No resource content with Revo 3 on resource creation #122

rtripault opened this issue Jan 19, 2023 · 1 comment

Comments

@rtripault
Copy link
Contributor

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

@muzzwood
Copy link
Contributor

Thanks for this @rtripault! I'll close this for now as VersionX 3.x was a complete rebuild, and this isn't an issue anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants