Some block env options are missing in admin context
hbugdoll opened this issue · 4 comments
What steps will reproduce the problem?
According to https://luya.io/guide/cms/blocks.html#env-context-information you can retrieve env / context information via $this->getEnvOption(...)
(in admin context).
In frontend context you have to call $this->block->getEnvOption(...)
instead. BTW this hint is currently missing in the guide.
What is the expected result?
- The env options in admin context are the same as in frontend context.
Frontend example – called $this->block->getEnvOption(...)
inside blocks/views/MyBlock.php
:
id=5
blockId=13
context=frontend
pageObject={
NavItem...}
isFirst=true
isLast=false
index=1
itemsCount=3
isPrevEqual=false
isNextEqual=true
equalIndex=1
What do you get instead?
- In admin context the page object is missing and further options related to the block's position.
Admin example – called $this->getEnvOption(...)
inside blocks\MyBlock::admin()
:
id=5
blockId=13
context=admin
pageObject={}
isFirst=
isLast=
index=
itemsCount=
isPrevEqual=
isNextEqual=
equalIndex=
Additional infos
Q | A |
---|---|
LUYA Version | Admin 5.0.0, CMS 5.0.0 |
PHP Version | 8.2/8.3 |
Platform | Apache |
Operating system | Linux Server/macOS |
After digging into the code, I've found one reason.
For the frontend context the passed page object $this->getNavItem()
is of class NavItem
ActiveQuery
(as pointed out by @nadar):
luya-module-cms/src/models/NavItemPage.php
Line 244 in a05aac2
But for the admin context the passed page object $navItemPage
is of class NavItemPage
(Block::getObject()
calls Block::createObject()
):
luya-module-cms/src/models/NavItemPage.php
Line 461 in a05aac2
For the frontend context the other env options 'index'
, 'itemsCount'
, 'isFirst'
, 'isLast'
, 'isPrevEqual'
, 'isNextEqual'
and 'equalIndex'
are set in NavItemPage::renderPlaceholderRecursive()
,
but for the admin context they are never set.
Since #403 the env options 'index'
, 'itemsCount'
, 'isFirst'
, 'isLast'
, 'isPrevEqual'
, 'isNextEqual'
and 'equalIndex'
are now available in both admin and frontend context.
Now we have to challenge the non-scalar type 'pageObject'
.
Let's start over again! As the name suggests, pageObject may contain a NavItemPage
.
But the guide says:
pageObject: Returns the luya\cms\models\NavItem object...
Currently 'pageObject'
is filled differently, see #401 (comment):
NavItemPage
in admin contextActiveQuery
forNavItem
in frontend context
So we have to clear up what 'pageObject'
should be.