Get Package from within block controller

Permalink
Hi,

How can I get the package from within a block controller?

When adding a new block onto an area (so, BlockController::add()) r=there is now BlockObject yet.
How can I get the Package? (other than hardcode the packagehandle).

SnefIT

SnefIT
 
MichaelG replied on at Permalink Reply
MichaelG
I'm a little lost on your goal.

You can always do $pkg = Package::getByHandle('my_package');

but you're saying you want to be able to figure out the package another way?
How so? By figuring out what package the block is part of?
SnefIT replied on at Permalink Reply
SnefIT
In some parts you can do something like:
// Get package
$pkg = $this->getPackage();
// Get packageid
$pkgID = $this->getPackageID();
// Get package handle
$pkgHandle = $this->getPackageHandle();


When there is a block, I can get the block and from there I can get the package like:
// Get block
$b = $this->getBlockObject();
// Get packageID
$pkgID = $b->getPackageID();
// Get package handle
$pkgHandle = $b->getPackageHandle();


Unfortunately when adding a block for the first time, there is no block in code yet, so I can't get the block object.

Normally I also use the simple solution like you do, but I thought it was "better" to get the package/packagehandle "automatic" (less hardcoded spots). Should be available somewhere, because the block (blocktype) was installed within a package so you should have that information available somewhere.

It's no showstopper, just trying to cleanup some code ;-)

PS. I need the packagehandle to get to the config of that package.
MichaelG replied on at Permalink Reply
MichaelG
Yeah - the block object doesn't get loaded until it's been saved. One way or another you'd have to manually get something. Either you write two lines for getting the package config, or a dozen to manually load up the block class.