Custom Block assistance ?
Permalink
Hi all, I was tinkering with creating my own block and I think I'm on the right path but not quite sure. If anyone wants to take a look see and offer up any advice/ suggestions. That would be most excellent.
db.xml
controller.php
view.php
add.php
edit.php
db.xml
<?xml version="1.0"?> <schema version="0.3"> <table name="btQuoteBlock"> <field name="bID" type="I"> <key /> <unsigned /> </field> <field name="content" type="X2" size="400"> <default value="" /> </field> <field name="name" type="X2" size="100"> <default value="" /> </field> </table> </schema>
controller.php
<?php defined('C5_EXECUTE') or die(_("Access Denied.")); class QuoteBlockController extends BlockController { protected $btName = "Quote Block"; protected $btDescription = "A simple way to add customer testimonials or quotes to your website"; protected $btTable = "btQuoteBlock"; protected $btInterfaceWidth = "300"; protected $btInterfaceHeight = "250"; public function getBlockTypeName() { return t('Quote Block'); } public function getBlockTypeDescription() { return t('A simple way to add formatted quote blocks'); } }
view.php
<?php defined('C5_EXECUTE') or die(_("Access Denied.")) ?> <ul class="quotes"> <li id="words"> <p> <?php echo $content ?> </p> </li> <li id="person"> <i> <?php echo $name ?> </i> </li id="link"> <li> <a href="<?php $item->link->getHref();?>"> </li>
Viewing 15 lines of 16 lines. View entire code block.
add.php
<?php defined('C5_EXECUTE') or die(_("Access Denied.")) ?> <div class="QuoteBlock.ccm-ui"> <?php echo $form->label('content', t('Name')) ?> <?php echo $form->text('content', array('style' => 'width: 300px'));?> <?php echo $form->label('title', t('Quote'));?> <?php echo $form->text('content', array('style'=> 'width:300px'))?> <?php echo $form->label('content', t('Link')) ?> <?php echo $form->text('content', array('style'=> 'width:300px'))?> </div>
edit.php
<?php defined('C5_EXECUTE') or die(_("Access Denied.")) ?> <div class="QuoteBlock.ccm-ui"> <?php echo $form->label('content', t('Name');?> <?php echo $form->text('content', $content, 'array'('style'=> 'width:300px'));?> <?php echo $form->label('content', t('Quote');?> <?php echo $form->text('content', $content, 'array'('style'=> 'width:300px'));?> <?php echo $form->label('content', t('Link');?> <?php echo $form->text('content', $link, 'array'('style'=> 'width:300px'));?> </div>
In the db.xml, use the smallest data type compatible with your data. So 'name' is unlikely to need an X or X2. I don't think size="" with an X does anything useful. Content could be an X and Name a C with a size.
http://www.concrete5.org/documentation/how-tos/developers/creating-...
In the add, you are missing empty parameters for the not-yet defined values. However, don't add them because most developers combine the add and edit forms, with add merely doing $this->inc of the edit. When adding the variables will be empty anyway.
In the edit form, use extra parameters to set a maxlength attribute for database fields so users cant enter anything bigger than you want.
While you can use style attributes to set a width, bear in mind that you also have all the dashboard bootstrap css available to do that sort of thing.
http://getbootstrap.com/2.3.2/base-css.html#forms...
I am not sure what class="QuoteBlock.ccm-ui" will do. Add/Edit dialogs are already wrapped in a ccm-ui container.
In the controller, only use the functions to return the name and description. The variables btName etc were deprecated many c5 versions ago and are not used if you have the functions.
In the view, try not to use id if you can use a class. Using an id will break if you add more than one block to a page (and hence get a duplicate id). Different browsers may recover from that in different ways. If you really need id attributes, make them unique (there are some howtos on that).
I don't know what $item->link->getHref(); will do. I can't see any declaration of $item and it appears at odds with the $link field. For links, c5 has the page select helper http://www.concrete5.org/documentation/developers/forms/concrete5-w...