verbb / smith

A Craft CMS plugin to add copy, paste and clone functionality to Matrix blocks.
MIT License
25 stars 9 forks source link

Entries field references not beeing saved #11

Closed rocknrolaf closed 4 years ago

rocknrolaf commented 4 years ago

Description If a matrix block type includes an entries field the reference to this entry is not saved and a validation error occurs.

Steps to reproduce

  1. Create a matrix block with an entries field
  2. Relate an entry within this entries field in the matrix block an save the entry
  3. Copy this matrix block
  4. Paste the matrix block into another entry with the same matrix field
  5. Save the entry

Additional info

Additional context I use this workaround which seem to work for the moment (basically it moves the fields array from the _BLOCK... to the parameter which includes the "type" and the "enabled" attribute already. e.g. "new1") :

            if (Craft::$app->request->getIsPost() && Craft::$app->request->getBodyParam('action') === 'entries/save-entry') {
                $params = Craft::$app->request->getBodyParams();
                if (!empty($params['fields']['embeds']['blocks'])) {
                    $keys = array_keys($params['fields']['embeds']['blocks']);
                    $ctr = 0;
                    foreach ($params['fields']['embeds']['blocks'] as $name => $block) {
                        if (preg_match('/^__BLOCK/', $name)) {
                            $keyName = $keys[$ctr - 1];
                            $params['fields']['embeds']['blocks'][$keyName]['fields'] = $block['fields'];
                            unset($params['fields']['embeds']['blocks'][$name]);
                        }
                        $ctr++;
                    }
                    Craft::$app->request->setBodyParams($params);
                }
            }
engram-design commented 4 years ago

Should be fixed in 1.1.8