Pulling info from a block
Permalink
I need to access some info from an image block located on another page... I don't want to display the whole block, I just need to get the path of the image for some other user.
I know how to get the actual block:
$blocks = $page->getBlocks('Main');
But, I'm not quite sure how to pull specific info that has been set by the user.
Thanks!
I know how to get the actual block:
$blocks = $page->getBlocks('Main');
But, I'm not quite sure how to pull specific info that has been set by the user.
Thanks!
Thanks for the reply. And thanks for including my question in the video!
As for your answer, though, that is the part I already know how to do... what I'm really trying to figure out is how to just pull the image path from the block ( after I've already grabbed the block )... I don't what to just grab the block and display it, as that will actually display the image. I want to grab the path of that image to use for other purposes.
Here's the code for the add/edit I'm using for the block:
In my controller file, have have these functions:
So, I'm thinking I need to access the controller and call the get_image() function ( or something similar )... but, my attempts have failed.
Thanks for the help!
As for your answer, though, that is the part I already know how to do... what I'm really trying to figure out is how to just pull the image path from the block ( after I've already grabbed the block )... I don't what to just grab the block and display it, as that will actually display the image. I want to grab the path of that image to use for other purposes.
Here's the code for the add/edit I'm using for the block:
<?php defined('C5_EXECUTE') or die("Access Denied."); $al = Loader::helper('concrete/asset_library'); //for image fields $ps = Loader::helper('form/page_selector', 'designer_content'); //for page link fields ?> <div class="ccm-block-field-group"> <h2>Property Image</h2> <?php echo $al->image('field_1_image_fID', 'field_1_image_fID', 'Choose Image', $field_1_image); ?> <table border="0" cellspacing="3" cellpadding="0" style="width: 95%;"> <tr> <td align="right" nowrap="nowrap"><label for="field_1_image_altText">Alt Text:</label> </td> <td align="left" style="width: 100%;"><?php echo $form->text('field_1_image_altText', $field_1_image_altText, array('style' => 'width: 100%;')); ?></td> </tr> <!-- <tr>
Viewing 15 lines of 21 lines. View entire code block.
In my controller file, have have these functions:
public function view() { $this->set('field_1_image', $this->get_image_object($this->field_1_image_fID, 0, 0)); } public function get_image() { $this->set('field_1_image', $this->get_image_object($this->field_1_image_fID, 0, 0)); return $field_1_image->src; } private function get_image_object($fID, $max_width = 0, $max_height = 0) { if (empty($fID)) { $image = null; } else if (empty($max_width) && empty($max_height)) { //Show image at full size (do not generate a thumbnail) $file = File::getByID($fID); $size = @getimagesize($file->getPath()); $image = new stdClass;
Viewing 15 lines of 28 lines. View entire code block.
So, I'm thinking I need to access the controller and call the get_image() function ( or something similar )... but, my attempts have failed.
Thanks for the help!
You need to call "getInstance()" on the block, like this:
$blocks = $page->getBlocks('Main'); //NOTE that this doesn't work for blocks inside an Area Layout! (See Page List Teasers helper code for how to do that if needed) $images = array(); foreach ($blocks as $block) { if ($block->btHandle == 'your_block_handle') { //<-- Replace this with your block's handle $images[] = $block->getInstance()->get_image(); } } print_r($array);
Andrew answered your question in this week's Totally Random show, around 38:00
http://www.ustream.tv/recorded/15137861...
Essentially you'd want to get a page object for the other page, then fetch the blocks in the area ( for instance, "Main") then test for / select the particular block by its handle.
This thread might be helpful:
http://www.concrete5.org/community/forums/customizing_c5/how_can_i_...
Grabbed this code directly from VortexSurfer's post:
Hope this gets you on the right track!