Bug #105600
closedPHP Warning: Undefined array key "currentValueKey" in /var/www/html/vendor/typo3/cms-fluid/Classes/ViewHelpers/CObjectViewHelper.php line 117
0%
Description
While invoking the CObjectViewHelper from an other Viewhelper Class, I've ran into undefined array key warnings:
PHP Warning: Undefined array key "currentValueKey" in /var/www/html/vendor/typo3/cms-fluid/Classes/ViewHelpers/CObjectViewHelper.php line 117
PHP Warning: Undefined array key "table" in /var/www/html/vendor/typo3/cms-fluid/Classes/ViewHelpers/CObjectViewHelper.php line 118
As those arguments registered as optional, there should be now array key warning.
My Code:
$cObjectViewHelper->setArguments([
'typoscriptObjectPath' => $blockType,
'data' => $data->getRawRecord()->toArray(),
'context' => $context,
]);
$cObjectViewHelper->setRenderingContext($subView->getRenderingContext());
$content = $cObjectViewHelper->render();
Full version: Source: https://github.com/TYPO3incubator/surfcamp-team-success/blob/main/local_packages/success/Classes/ViewHelpers/RenderBlockViewHelper.php#L87-L91
Updated by Garvin Hicking 7 days ago
- Related to Task #104786: Avoid renderStatic() in ViewHelpers added
Updated by Garvin Hicking 7 days ago
- Status changed from New to Needs Feedback
Possibly I'd argue that if you pass "raw" arguments and use the ViewHelper as API (which it isn't intended to be) you would need to route all arguments to the setArguments() call.
Instead of wiring this by hand you probably should better use the ViewHelperInvoker->invoke()
method to call another ViewHelper, have you tried that?
Updated by Gerrit Code Review 7 days ago
- Status changed from Needs Feedback to Under Review
Patch set 1 for branch main of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/c/Packages/TYPO3.CMS/+/86989
Updated by André Buchmann 7 days ago
- Description updated (diff)
@Garvin Hicking Haven't tried that yet. Upated the description with a link to the full example.
Patch has been created, but I'll give it a try.
Updated by Simon Praetorius 7 days ago
You can also have a look at the latest version of this patch, which already uses the invoker: https://review.typo3.org/c/Packages/TYPO3.CMS/+/83718
Updated by André Buchmann 7 days ago
Heyho @Garvin Hicking Using the invoker prevents the array key warnings.
Here are both ways configured that they do not need the patch:
The invoker way:
$content = $view->getRenderingContext()->getViewHelperInvoker()->invoke(
CObjectViewHelper::class,
[
'typoscriptObjectPath' => $blockType,
'data' => $data->getRawRecord()->toArray(),
'context' => $context,
'table' => 'tt_content',
],
$subView->getRenderingContext(),
);
The resolver way:
$cObjectViewHelper = $view->getRenderingContext()->getViewHelperResolver()->createViewHelperInstance('f', 'cObject');
$cObjectViewHelper->setArguments([
'typoscriptObjectPath' => $blockType,
'data' => $data->getRawRecord()->toArray(),
'table' => 'tt_content',
'currentValueKey' => null, // prevent array key warning
'context' => $context,
]);
$cObjectViewHelper->setRenderingContext($subView->getRenderingContext());
$content = $cObjectViewHelper->render();
Bottom line: You think there is no need to add the null coalescing operator to the optional arguments? (see patch)
Updated by Garvin Hicking 7 days ago
Personally, yes, I'd follow Simons lead here and would say that the Invoker way is the suggested way to approach it, allowing the Core for future flexibility. While adding array checks in there would help your case, I think it would make the code a bit more "heavy" and extra weight (albeit only little).
So if you're fine with it I would prefer to close the issue, yes. Thanks so much for adding in the resulting code that you worked out, and for offering the patch in first place! :)
Updated by André Buchmann 7 days ago
- Status changed from Under Review to Closed
@Simon Praetorius and @Garvin Hicking thx for pointing out the better solution.
I abandon the patch and close the issue.
Have a nice day.