luyadev/luya-module-cms

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):

$blockObject = Block::createObject($placeholder['class'], $placeholder['block_id'], $placeholder['id'], 'frontend', $this->getNavItem());

But for the admin context the passed page object $navItemPage is of class NavItemPage
(Block::getObject() calls Block::createObject()):

$blockObject = $blockItem->block->getObject($blockItem->id, 'admin', $navItemPage);

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 context
  • ActiveQuery for NavItem in frontend context

So we have to clear up what 'pageObject' should be.

'pageObject' is now always a NavItemPage, fixed by #408.