1

I have a front-end form which sends a limited selection of the fields to the action entryRevisions/saveDraft (actually a custom plugin action, but in the it end up there). I expected it to take the already published entry's data and creates a new draft based on the data of the published one overriden with the posted data. But it seems this is not the case. Now I have drafts with only the few fields from the front-end form.

Of course I can get the entry data of the published entry before saving the draft and overwrite it with the post data, but maybe you guys know a simpler/better solution?

1 Answer 1

1

Too bad no one could help me, but found a solution in the meantime. Inspired by others on Github and Stackoverflow (https://craftcms.stackexchange.com/a/14158/3937). Maybe my solution helps someone else with the same problem.

I created a method in my controller which gathers all values from the fields and prepares an array of it, which has exactly the same format as the CP in the browser sends when saving a new draft. I use this array to feed the method setContentFromPost on the EntryDraftModel, together with the incoming POST data from craft()->request->getParam('fields').

public function getFieldValuesFromEntry(BaseElementModel $entry, $fields = null)
{
    foreach ($entry->fieldLayout->fields as $fieldLayoutField) {
        $fieldHandle = $fieldLayoutField->field->handle;
        if ($fields === null || in_array($fieldHandle, $fields)) {

            $fieldValue = $entry->getFieldValue($fieldHandle);

            if ($fieldValue instanceof RichTextData)
            {
                $value = $fieldValue->getRawContent();
            }
            elseif ($fieldValue instanceof ElementCriteriaModel)
            {
                $value = $fieldValue->ids();
            }
            elseif ($fieldValue instanceof SingleOptionFieldData)
            {
                $value = $fieldValue->value;
            }
            else
            {
                $value = $fieldValue;
            }

            $field[$fieldHandle] = $value;
        }
    }

    return $field;
}

And this is the snippet where I feed it into the draft ($entryFields is here the output of the method above):

$draftFields = craft()->request->getParam('fields');
$draft->setContentFromPost(array_merge($entryFields, $draftFields));

If anybody has a more efficient solution to fix my problem, please let me now. Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.