Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

#8458 getViewModel() results in null #8459

Conversation

chrisfwd
Copy link
Contributor

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

…ted calls to those viewModels in layout docs.
@magento-engcom-team
Copy link

@chrisfwd thank you for contributing. Please accept Community Contributors team invitation here to gain extended permissions for this repository.

@mrtuvn
Copy link
Contributor

mrtuvn commented Jan 4, 2021

In my point of view this is preference choice. But default magento used argument viewModel instead view_model. Both way should work
Here is example code magento recommend
https://github.com/magento/magento2/blob/2.4-develop/lib/internal/Magento/Framework/View/Element/Template.php#L24
In some case if use $block->getViewModel() maybe get null at variable
CC: @dmytro-ch can you clarify reason of use view_model instead origin viewModel. I see in frontend somewhere use both approach but in backend use view_model. This may lead to inconsistency and make more noise/confusing to dev experience

Copy link
Contributor

@dmytro-ch dmytro-ch left a 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

@dobooth dobooth added 2.3.x Magento 2.3 related changes 2.4.x Magento 2.4.x related changes Technical Updates to the code or processes that alter the technical content of the doc labels Jan 22, 2021
@dobooth
Copy link
Contributor

dobooth commented Jan 22, 2021

running tests

@dobooth dobooth enabled auto-merge January 22, 2021 14:59
@dobooth dobooth merged commit f0a21b2 into magento:2.4.2-develop Jan 22, 2021
@ghost
Copy link

ghost commented Jan 22, 2021

Hi @chrisfwd, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
2.3.x Magento 2.3 related changes 2.4.x Magento 2.4.x related changes Progress: done Technical Updates to the code or processes that alter the technical content of the doc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants