Building a block with library image input?

Permalink 8 users found helpful
I'm trying to build a block that has the ability to pull an image from the library (or, since I'm desperate, from anywhere, even a remote URL), but I can't figure it out, and I'm new to PHP...

Does anyone have any pointers? I tried readinghttp://phplens.com/lens/adodb/docs-datadict.htm... andhttp://www.concrete5.org/api/Helpers/FormHelper.html... but haven't made much progress. Is there any (relatively) simple way of doing this (without any of the bells and whistles attached to the default Image block, or others, hard for a PHP beginner to distinguish between and separate the classes without breaking something every time) that I might be missing?

Any guidance is much appreciated.

stephmars
View Replies: View Best Answer
jgarcia replied on at Permalink Reply
jgarcia
In order to create a field in a form within Concrete5 that will let you select a file from the C5 file manager, use:

$al = Loader::helper('concrete/asset_library');
$al->file('ccm-b-file', 'fID', t('Choose File'), $bf)


Where "ccm-b-file" is the HTML id of the field, "fID" is the HTML name of the field, "Choose File" is the label of the field, and $bf is the file object that points to the selected file.

You can setup the file object using the following code:

$bf = File::getByID($fID);


Where $fID is the file ID.
stephmars replied on at Permalink Reply
stephmars
Thanks for your comment!

I'm having a hard time applying it. Here's what I have:

controller.php:

<?php
   class BasicTestBlockController extends BlockController {
      var $pobj;
      protected $btDescription = "A simple testing block for developers.";
      protected $btName = "Basic Test";
      protected $btTable = 'btBasicTest';
      protected $btInterfaceWidth = "350";
      protected $btInterfaceHeight = "300";
   }
?>


add.php:

<?php  
$al = Loader::helper('concrete/asset_library');
$al->file('ccm-b-file', 'fID', t('Choose File'), $bf)
 ?>
<?php echo $form->label('content', 'Name');?>
<?php echo $form->text('content', array('style' => 'width: 320px'));?>
<?php  
$bf = File::getByID($fID);
 ?>


edit.php:

<?php  
$al = Loader::helper('concrete/asset_library');
$al->file('ccm-b-file', 'fID', t('Choose File'), $bf)
 ?>
<?php echo $form->label('content', 'Name');?>
<?php echo $form->text('content', $content, array('style' => 'width: 320px'));?>


view.php:

<div style="color: red;"><?php echo $content?></div>


db.xml:

<?xml version="1.0"?>
<schema version="0.3">
   <table name="btBasicTest">
      <field name="bID" type="I">
         <key />
         <unsigned />
      </field>
      <field name="fID" type="I">
         <key />
         <unsigned />
      </field>
      <field name="content" type="X2">
      </field>
   </table>
</schema>


I realize that must look completely hopeless. I'm so frustrated! I've been working on this for days and can't seem to do this one thing. I'm amazed I got a response so fast, thank you so much, and thanks for any further help!
jgarcia replied on at Permalink Reply
jgarcia
What problem are you having specifically right now? Will it not install? Can you add it to a page? Are you getting an error message?
stephmars replied on at Permalink Reply
stephmars
It installed without error, but when I go to "Add Block" and select it, I see only a "Name" field. No option to select a library.

Edited to add: When I type in a name in the "Name" field and add it, I get this error message:

Fatal error: Uncaught exception 'ADODB_Exception' with message 'mysql error: [1048: Column 'fID' cannot be null] in EXECUTE("INSERT INTO btBasicTest (bID,content,fID) VALUES (116,'Name',NULL)") ' in /home/sites1/sites1.com/client/concrete/libraries/3rdparty/adodb/adodb-exceptions.inc.php:78 Stack trace: #0 /home/sites1/sites1.com/client/concrete/libraries/3rdparty/adodb/adodb.inc.php(1037): adodb_throw('mysql', 'EXECUTE', 1048, 'Column 'fID' ca...', 'INSERT INTO btB...', false, Object(ADODB_mysql)) #1 /home/sites1/sites1.com/client/concrete/libraries/3rdparty/adodb/adodb.inc.php(1012): ADOConnection->_Execute('INSERT INTO btB...', false) #2 /home/sites1/sites1.com/client/concrete/libraries/3rdparty/adodb/adodb-lib.inc.php(212): ADOConnection->Execute('INSERT INTO btB...') #3 /home/sites1/sites1.com/client/concrete/libraries/3rdparty/adodb/adodb.inc.php(1658): _adodb_replace(Object(ADODB_mysql), 'btBasicTest', Array, Array, false, false) #4 /home/ms in /home/sites1/sites1.com/client/concrete/libraries/3rdparty/adodb/adodb-exceptions.inc.php on line 78
jgarcia replied on at Permalink Reply
jgarcia
In your db.xml, take out <key /> for the fID field. The way you have it you are telling the table to have two primary keys, thus making the fID field not nullable...hence the error. I suggest uninstalling the block, deleting the table manually and re-installing.
stephmars replied on at Permalink Reply
stephmars
Wow, thanks, that worked! The error message is gone, and the "Name" field shows up when I put a name in.

However, the library/image option field still doesn't show up on the block. Just the "Name" field and that's it.

Here's a screenshot:http://imgur.com/gUjuE.jpg
jgarcia replied on at Permalink Reply
jgarcia
Change
$al->file('ccm-b-file', 'fID', t('Choose File'), $bf)

to
echo $al->file('ccm-b-file', 'fID', t('Choose File'), $bf);
stephmars replied on at Permalink Reply
stephmars
That worked! Amazing. Thank you so much.

The bad news is that after I choose the image from the library and apply the block, it doesn't show on the page. The "Name" field does, but the image is nowhere to be found.

Edited to add: Maybe it's something that needs to be added to the view.php page? I can't figure out exactly what, though.
jgarcia replied on at Permalink Reply
jgarcia
Is it saving it? It should just be a number (the file ID). Are you able to print $fID in the same way that you are printing $content
stephmars replied on at Permalink Reply
stephmars
It seems to be saving it. After I click "Add" and check the result, only the "Name" field result shows - no image.

I changed view.php from:

<?php echo $content?>
<?php echo $bt?>


to:

<?php echo $content?>
<?php echo $fID?>


...and now the result is the name, plus a number on the second line.
jgarcia replied on at Permalink Best Answer Reply
jgarcia
That's correct. The number you are seeing is the $fID - the ID number associated with the file you selected. To get the path to that file, do this:

$file = File::getByID($fID);
$filePath = $file->getVersion()->getRelativePath();


You can then do whatever you want with the $filePath var...like display the image or whatever:

echo '<img src="' . $filePath . '" />';
stephmars replied on at Permalink Reply
stephmars
It works!

Thank you so much! Thank you so, so much. I would have never figured this out on my own. Now I have a bit more understanding and can try to build from it. Thanks for taking time out of your day to help!
applejus replied on at Permalink Reply
Thank you jgarcia and everyone involved in this post. So far, it has really helped my understanding of blocks.

I seem to be at the point where my view.php will print the numerical value of $fID. However, the next step (->getRelativePath) still doesn't yield an image on my page.

I tried printing the $bf and $filePath variables as well, but nothing appears. After choosing an image, the resulting HTML is blank where those variables were placed.

<?php
   $bf = File::getByID($fID);
   $filePath = $bf->getVersion()->getRelativePath();
   $al = Loader::helper('concrete/asset_library');
   echo $al->file('ccm-b-file', 'fID', t('Choose File'), $bf);
?>


As far as I can tell, my code is a duplicate of the code in this tutorial. The exception being that I have added two textArea forms. They both print exactly as they are meant to.

What am I not realizing?
kranthi20 replied on at Permalink Reply
kranthi20
Thank you very much Jgarcia,
It's great useful to every one,
thank you for your reply.
jestyjt replied on at Permalink Reply
This is very helpful, thank you Jgarcia
okapi replied on at Permalink Reply
okapi
@ jgarcia
Your patience and your know-how has turned this thread into a helpful tutorial! A big thank you from my side too!
BigBobbyD replied on at Permalink Reply
BigBobbyD
Super helpful thread!

One detail in case anyone else is following this as a tutorial...

In edit.php, '$bf = File::getByID($fID);' needs to run before the line that invokes the js file selector so that the previously selected image appears. That statement is unnecessary in add.php.

So the lines in edit.php would look like:
<?php
$al = Loader::helper('concrete/asset_library');
$bf = File::getByID($fID);
echo $al->file('ccm-b-file', 'fID', t('Choose File'), $bf);
?>
fr3dy replied on at Permalink Reply
Hi. i follow this. but i wanted to do is to have more input fields. and add more as needed, the default inputs could be 5. and if need some more, can hit a button to add more. do i need to create more field names on the db.xml?
boltn replied on at Permalink Reply
boltn
thumbs up for that. would have cost me a fair bit of searching to figure this on out!