Custom Block - addBlock Error
Permalink
I have been attempting to add a Custom Block programatically. Either as a "default" in a page template, or by using "addBlock()". However, it will not add!!!!
Here are the error messages from the console:
Even hardcoding the "$classfile" into the "block_types.php" script exposes other errors. They all seem to indicate a "bug" with adding Custom Blocks programmatically.
$bt = BlockType::getByHandle('wishlist'); $data = array(); $data['uID'] = 1; $data['bName'] = 'wishlist'; $newBlock = $newPage->addBlock($bt, 'Aside1', $data);
Here are the error messages from the console:
[Mon Apr 25 13:49:35 2011] [error] [client ::1] PHP Notice: Undefined variable: classfile in /Library/WebServer/Documents/concrete5/concrete/models/block_types.php on line 569, referer: http://localhost/concrete5/index.php/dashboard/profiles1/ [Mon Apr 25 13:49:35 2011] [error] [client ::1] PHP Notice: Undefined variable: classfile in /Library/WebServer/Documents/concrete5/concrete/models/block_types.php on line 569, referer: http://localhost/concrete5/index.php/dashboard/profiles1/
Even hardcoding the "$classfile" into the "block_types.php" script exposes other errors. They all seem to indicate a "bug" with adding Custom Blocks programmatically.
I did forget to add that this is a Block within a Package.
Is this code in the install() function of your package controller? Why does the error message refer to the page "profiles1"? And where are you defining the $newPage variable? If you could post your entire package controller file that would be helpful (or if it's not the package controller if you could explain in more detail what the context is).
Jordan,
This code is in a script which is call by an AJAX action in the Dashboard (Profiles package section).
Prior to this, in the script, we create a new User and a new Page - which all works fine. Hence, the $newPage is a real object of class Page. The page created is one using a custom type, within which we need the Wishlist Block. We receive the same error if we add the Wishlist Block to the Defaults of the Page Type, and not try to add it programmatically :(
The Block "Wishlist" installs fine manually.
The script can also programmatically install core Blocks; such as "Content".
Here is the code for the Wishlist Block controller:
Here is the code for the Package controller:
The work-around has been to add the Block directly to the database.
Thank you.
Garry
This code is in a script which is call by an AJAX action in the Dashboard (Profiles package section).
Prior to this, in the script, we create a new User and a new Page - which all works fine. Hence, the $newPage is a real object of class Page. The page created is one using a custom type, within which we need the Wishlist Block. We receive the same error if we add the Wishlist Block to the Defaults of the Page Type, and not try to add it programmatically :(
The Block "Wishlist" installs fine manually.
The script can also programmatically install core Blocks; such as "Content".
Here is the code for the Wishlist Block controller:
defined('C5_EXECUTE') or die(_("Access Denied.")); Loader::model('model', 'profiles'); class WishlistBlockController extends BlockController { var $pobj; protected $btDescription = "A wishlist block for profiles."; protected $btName = "Wishlist"; protected $btTable = 'btWishlist'; protected $btInterfaceWidth = "350"; protected $btInterfaceHeight = "300"; }
Here is the code for the Package controller:
Loader::model('model', 'profiles'); class ProfilesPackage extends Package { protected $pkgHandle = 'profiles'; protected $appVersionRequired = '5.3.3'; protected $pkgVersion = '1.0'; public function getPackageDescription() { return t("Installs the Profiles package."); } public function getPackageName() { return t("Profiles"); } public function install() { $pkg = parent::install(); Loader::model('single_page'); BlockType::installBlockTypeFromPackage('profiles', $pkg);
Viewing 15 lines of 21 lines. View entire code block.
The work-around has been to add the Block directly to the database.
Thank you.
Garry
If you can't add the block to pages normally, the problem is with the block code, not the installer code. I would investigate that first and then come back to the installer once that's working properly.
The Block does add to a page normally, without any problems.
Okay, my mistake -- then what do you mean when you say "we receive the same error if we add the Wishlist Block to the Defaults of the Page Type, and not try to add it programmatically"?
I can add the Wishlist Block to the Page Type Defaults without a problem. The errors occur when I then attempt to add that page type programmatically. If adding a new Page, of that type, worked while containing the Wishlist Block, I would not need to add the Wishlist Block programmatically :(
The same errors occur if I attempt to add that Page Type (programmatically) while it contains the Wishlist Block as a Default.
I should have explained that a bit better.
The same errors occur if I attempt to add that Page Type (programmatically) while it contains the Wishlist Block as a Default.
I should have explained that a bit better.
I'm totally confused :)
It's going to be very difficult to offer help unless you can post some more complete code. I don't see any specific typos or errors in the various snippets you've posted but it's missing the overall context -- what files are they in, when do they get called, where are they being called from, etc. etc.
Is this a package or just a bunch of files you have spread around in your site? If it's a package can you zip it up and post it? If not, can you gather the different files and zip them up (but somehow provide information about which directory path they each reside in)?
It's going to be very difficult to offer help unless you can post some more complete code. I don't see any specific typos or errors in the various snippets you've posted but it's missing the overall context -- what files are they in, when do they get called, where are they being called from, etc. etc.
Is this a package or just a bunch of files you have spread around in your site? If it's a package can you zip it up and post it? If not, can you gather the different files and zip them up (but somehow provide information about which directory path they each reside in)?