Modifying the Lightboxed Image Block, Can't seem to get my variable into controller.php

Permalink
Hello,

The Lightboxed Image Block allows you to set pixel values for the dimensions of the thumbnails. I want to modify the block so you can choose two hard-coded values: large & small.

I added the following field to the db.xml file
<field name="image_size" type="I1">
         <unsigned />
         <notnull />
         <default value="0" />
      </field>


I changed the edit.php file to allow you to select a value for the $image_size field
<strong><?php  echo t('Choose a large or small image')?>:</strong>
<p><input type="radio" name="image_size" value="0" <?php echo ($bObj->image_size)?'checked':''?> /> <?php echo t('Large - For Center Column')?></p>
<p><input type="radio" name="image_size" value="1" <?php echo (!$bObj->image_size)?'checked':''?> /> <?php echo t('Small - For Left and Right Columns')?></p>


Then modified the logic in the controller.php file to change the width/height based on $image_size
public function save($args) {      
         $args['maxWidth'] = ($image_size == 0) ? 770 : 270;
         $args['maxHeight'] = ($image_size == 0) ? 482 : 265;
      }


But it doesn't work. It just defaults to larger size. I can echo $image_size from view.php and it is storing a 1 or 0 value for each block instance based on what was selected during block creation. If I try and print $image_size from within the controller.php however, I get nothing.

I am hacking my way through this and learning as I go, so I wouldn't at all be surprised if I'm doing something dumb. Please help! I'll attach the package I'm making if you want to install/inspect all the code.

Here is the original block I'm trying to edit.
http://www.concrete5.org/marketplace/addons/lightboxed-image/...

1 Attachment

kidconcept
 
mesuva replied on at Permalink Best Answer Reply
mesuva
Hi Daniel,

I put together this original block, nice to see you're hacking away at it!

I think this is just a case of you using a variable in your save function that doesn't exist at that point.

Try changing your save functions to this:

public function save($args) {      
         $args['fID'] = ($args['fID'] != '') ? $args['fID'] : 0;
         $args['maxWidth'] = ($args['image_size'] == 0) ? 770 : 270;
         $args['maxHeight'] = ($args['image_size'] == 0) ? 482 : 265;
         $args['maxWidthLarge'] = 1720;
         $args['maxHeightLarge'] = 880;
         $co = new Config();
         $pkg = Package::getByHandle("weekapaug_image");
         $co->setPackageObject($pkg);
         $co->save('block_type', $args['block_type']);
         parent::save($args);
      }


Cheers
-Ryan
kidconcept replied on at Permalink Reply
kidconcept
Ryan, you are the best! Thanks mate, that did the trick. And a big thanks for putting together this block in the first place, it's proving very useful. :)
kidconcept replied on at Permalink Reply
kidconcept
If anybody else is reading this thread, I found a related one that was helpful to me.

http://www.concrete5.org/community/forums/block_requests/using-a-bl...

To access a field variable in the database in your controller.php use:

$this->fieldName

I'd love to know what $this is referencing. Is it the BlockController class? I really have no idea. Good luck to whoever comes along to find this later. :)
jordanlev replied on at Permalink Reply
jordanlev
In a block controller, $this is referencing the block controller class. Note that your block controllers always "extend BlockController" -- so even though you only see a small number of functions in your block controller (view(), add(), edit(), etc.), there are actually a lot more in that base class (which is defined by the C5 system, in /concrete/core/libraries/block_controller.php). One of the things that base class does is upon instantiation, it grabs all of the data in the block's database record and sets them as properties of the controller itself. Hope that makes sense (if not, I'd be happy to try to explain in more detail).

-Jordan