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

<p>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 <code>setContentFromPost</code> on the <code>EntryDraftModel</code>, together with the incoming <code>POST</code> data from <code>craft()-&gt;request-&gt;getParam('fields')</code>.</p>

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

            $fieldValue = $entry-&gt;getFieldValue($fieldHandle);

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

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

    return $field;
}
</code></pre>

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

<pre><code>$draftFields = craft()-&gt;request-&gt;getParam('fields');
$draft-&gt;setContentFromPost(array_merge($entryFields, $draftFields));
</code></pre>

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

CC BY-SA 4.0
2019-08-09T22:20:46.713Z
2019-08-09T22:20:46.713Z
ctigelaar
<p>I have a front-end form which sends a limited selection of the fields to the action <code>entryRevisions/saveDraft</code> (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.</p>

<p>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?</p>

<plugin-development><draft>
Draft from front-end form with limited field set
# Draft from front-end form with limited field set

(Asked by: username_0 on 2019-05-10)

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?

**Tags:** plugin-development, draft


---

## 1 answers

### Answer by username_0 on 2019-08-09. Score: 1 | Accepted answer

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!


---

