-
Notifications
You must be signed in to change notification settings - Fork 1.7k
#8458 getViewModel() results in null #8459
#8458 getViewModel() results in null #8459
Conversation
…ted calls to those viewModels in layout docs.
In my point of view this is preference choice. But default magento used argument viewModel instead view_model. Both way should work |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my opinion, the view_model
key is the correct one. Since the original idea was to retrieve the view model with the corresponding magic method and conventionally reserve this method name:
$viewModel = $block->getViewModel();
OR using getter in a conventional way, with snake case:
$viewModel = $block->get('view_model');
While the following approach is confusing and seems to be caused by a typo in documentation.
$viewModel = $block->get('viewModel');
I would prefer to fix this doc block accordingly:
https://github.com/magento/magento2/blob/2.4-develop/lib/internal/Magento/Framework/View/Element/Template.php#L24
running tests |
Hi @chrisfwd, thank you for your contribution! |
Purpose of this pull request
This pull request (PR) fixes documentation about viewmodels that explains to use
name="viewModel"
in layout.xml and tries to retrieve that viewmodel in the template via$block->getViewModel()
Affected DevDocs pages
https://devdocs.magento.com/guides/v2.4/extension-dev-guide/view-models.html
https://devdocs.magento.com/guides/v2.4/frontend-dev-guide/layouts/xml-instructions.html
https://devdocs.magento.com/guides/v2.4/frontend-dev-guide/layouts/xml-manage.html
Fixes #8458