I am trying to assign a Product Object to a CMS Page Object, so when you have the CMS Page Object, you can do something like this:
$product = $page->getProduct();
I have modified the CMS Page Form to have an input field for the Product ID, which saves to the database. I have then created a Custom ReadHandler for the CMS Page to set the relationship like so:
public function execute($entity, $arguments = [])
{
if ($entity->getId()) {
$additionalData = $this->resourcePage->getAdditionalData((int)$entity->getId());
if (!is_null($additionalData)) {
$additionalData = json_decode($additionalData, true);
$entity->setData('additional_data', $additionalData);
if (isset($additionalData['product_id'])) {
$product = $this->productRepository->getById($additionalData['product_id']);
$entity->setData('product', $product);
}
}
}
return $entity;
}
This works on the frontend when the page loads I can see the Product Object is within the CMS Page Object, and I can do $product = $page->getProduct();
However, when I load the page in the backend (for editing) I get this error:
Uncaught Exception: Serialization of 'Closure' is not allowed in /vendor/magento/framework/Session/SessionManager.php:136
Stack trace:
0 /vendor/magento/framework/Session/SessionManager.php(136): session_write_close()
1 [internal function]: Magento\Framework\Session\SessionManager->writeClose()
2 {main} thrown in /vendor/magento/framework/Session/SessionManager.php on line 136
I am aware I can't serialize a Closure, but I can't change the Product Object which is what contains the Closures, which is why I am here...
I am guessing some standard Magento Backend functionality is causing this, but I cannot figure out how to get around it.