Fixed Webhooks: Including phrases via getters will fail

DragonByte Tech

Well-known member
Affected version
2.3.0 Beta 6
Assume you have a getter like this:
PHP:
    public function getTagline(): \XF\Phrase
    {
        return \XF::phrase($this->getTaglinePhraseName());
    }

In your entity's setupApiResultData you include it like so: $result->includeGetter('tagline');

Sending the webhook will cause this error:

LogicException: Instances of XF\Db\AbstractAdapter cannot be serialized or unserialized in src/XF/Db/AbstractAdapter.php at line 686
  1. XF\Db\AbstractAdapter->__sleep()
  2. serialize() in src/XF/Job/Manager.php at line 581
  3. XF\Job\Manager->_enqueue() in src/XF/Job/Manager.php at line 493
  4. XF\Job\Manager->enqueue() in src/XF/Repository/Webhook.php at line 127
  5. XF\Repository\Webhook->queueWebhook() in src/XF/Behavior/Webhook.php at line 33
  6. XF\Behavior\Webhook->postSave() in src/XF/Mvc/Entity/Entity.php at line 1317
  7. XF\Mvc\Entity\Entity->save() in src/addons/DBTech/eCommerce/Service/Product/Edit.php at line 564
  8. DBTech\eCommerce\Service\Product\Edit->_save() in src/XF/Service/ValidateAndSavableTrait.php at line 42
  9. DBTech\eCommerce\Service\Product\Edit->save() in src/addons/DBTech/eCommerce/Admin/Controller/Product.php at line 599
  10. DBTech\eCommerce\Admin\Controller\Product->actionSave() in src/XF/Mvc/Dispatcher.php at line 352
  11. XF\Mvc\Dispatcher->dispatchClass() in src/XF/Mvc/Dispatcher.php at line 258
  12. XF\Mvc\Dispatcher->dispatchFromMatch() in src/XF/Mvc/Dispatcher.php at line 115
  13. XF\Mvc\Dispatcher->dispatchLoop() in src/XF/Mvc/Dispatcher.php at line 57
  14. XF\Mvc\Dispatcher->run() in src/XF/App.php at line 2604
  15. XF\App->run() in src/XF.php at line 533
  16. XF::runApp() in admin.php at line 13
---

Dumping $payload shows the phrase is not being rendered:

1715257487555.webp


I'm not sure 100% what's at fault here, but from looking at XF/Api/Result/EntityResult::castToFinalValue it would seem like the simplest solution would be this:
Diff:
--- src/XF/Api/Result/EntityResult.php    2024-05-09 15.26.25.000000000
+++ src/XF/Api/Result/EntityResult.php    2024-05-09 15.26.09.000000000
@@ -222,13 +222,13 @@
             else
             {
                 $value = $value->toWebhookResults($verbosity, $options);
             }
         }
 
-        if ($value instanceof ResultInterface)
+        if ($value instanceof ResultInterface || $value instanceof \XF\Phrase)
         {
             $value = $value->render();
         }
 
         return $value;
     }
 
Thank you for reporting this issue, it has now been resolved. We are aiming to include any changes that have been made in a future XF release (2.3.0 Beta 7).

Change log:
Pre-render phrases, pre-escaped-like objects when preparing API results
There may be a delay before changes are rolled out to the XenForo Community.
 
Back
Top Bottom