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.
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.
Thanks for your comment!
I'm having a hard time applying it. Here's what I have:
controller.php:
add.php:
edit.php:
view.php:
db.xml:
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!
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:
edit.php:
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!
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?
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
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
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.
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
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
Change
to
$al->file('ccm-b-file', 'fID', t('Choose File'), $bf)
to
echo $al->file('ccm-b-file', 'fID', t('Choose File'), $bf);
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.
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.
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
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:
You can then do whatever you want with the $filePath var...like display the image or whatever:
$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 . '" />';
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!
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!
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.
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?
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.
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?
Thank you very much Jgarcia,
It's great useful to every one,
thank you for your reply.
It's great useful to every one,
thank you for your reply.
This is very helpful, thank you Jgarcia
@ jgarcia
Your patience and your know-how has turned this thread into a helpful tutorial! A big thank you from my side too!
Your patience and your know-how has turned this thread into a helpful tutorial! A big thank you from my side too!
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:
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:
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?
thumbs up for that. would have cost me a fair bit of searching to figure this on out!
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:
Where $fID is the file ID.