Working on a custom theme for Concrete5 5.7. I am trying to allow my client to choose between an icon or an image in the Feature block. I have successfully been able to enter an image into the database via the updated block. However, I can't figure out how to get the image to show. Below are my updates to the code. (FYI - I am learning PHP, so I mostly copied things from the "image" block to try and make the feature block work. There may be code that I don't need or things that I am missing).
Controller.php
<?php
namespace Application\Block\Feature;
use Page;
use File;
use Database;
use Concrete\Core\Block\BlockController;
use Less_Parser;
use Less_Tree_Rule;
use Core;
defined('C5_EXECUTE') or die("Access Denied.");
class Controller extends BlockController
{
public $helpers = array('form');
protected $btInterfaceWidth = 400;
protected $btCacheBlockOutput = true;
<?php
namespace Application\Block\Feature;
use Page;
use File;
use Database;
use Concrete\Core\Block\BlockController;
use Less_Parser;
use Less_Tree_Rule;
use Core;
defined('C5_EXECUTE') or die("Access Denied.");
class Controller extends BlockController
{
public $helpers = array('form');
protected $btInterfaceWidth = 400;
protected $btCacheBlockOutput = true;
protected $btCacheBlockOutputOnPost = true;
protected $btCacheBlockOutputForRegisteredUsers = true;
protected $btExportPageColumns = array('internalLinkCID');
protected $btExportFileColumns = array('fID');
protected $btInterfaceHeight = 520;
protected $btTable = 'btFeature';
protected $btFeatures = array(
'image',
);
public function getBlockTypeDescription()
{
return t("Displays an icon, a title, and a short paragraph description.");
}
public function getBlockTypeName()
{
return t("Feature");
}
public function getLinkURL()
{
if (!empty($this->externalLink)) {
return $this->externalLink;
} else {
if (!empty($this->internalLinkCID)) {
$linkToC = Page::getByID($this->internalLinkCID);
return (empty($linkToC) || $linkToC->error) ? '' : Core::make('helper/navigation')->getLinkToCollection(
$linkToC
);
} else {
return '';
}
}
}
public function registerViewAssets($outputContent = '')
{
$this->requireAsset('css', 'font-awesome');
if (is_object($this->block) && $this->block->getBlockFilename() == 'hover_description') {
// this isn't great but it's the only way to do this and still make block
// output caching available to this block.
$this->requireAsset('javascript', 'bootstrap/tooltip');
$this->requireAsset('css', 'bootstrap/tooltip');
}
}
public function add()
{
$this->edit();
}
public function view()
{
$this->set('linkURL', $this->getLinkURL());
$f = File::getByID($this->fID);
if (!is_object($f) || !$f->getFileID()) {
return false;
}
$this->set('f', $f);
}
public function getJavaScriptStrings()
{
return array(
'image-required' => t('You must select an image.'),
);
}
public function isComposerControlDraftValueEmpty()
{
$f = $this->getFileObject();
if (is_object($f) && !$f->isError()) {
return false;
}
return true;
}
public function getImageFeatureDetailFileObject()
{
// i don't know why this->fID isn't sticky in some cases, leading us to query
// every damn time
$db = Database::get();
$fID = $db->GetOne('select fID from btContentImage where bID = ?', array($this->bID));
if ($fID) {
$f = File::getByID($fID);
if (is_object($f) && !$f->isError()) {
return $f;
}
}
}
public function getFileID()
{
return $this->fID;
}
public function getFileObject()
{
return File::getByID($this->fID);
}
public function validate_composer()
{
$f = $this->getFileObject();
$e = Core::make('helper/validation/error');
if (!is_object($f) || $f->isError() || !$f->getFileID()) {
$e->add(t('You must specify a valid image file.'));
}
return $e;
}
protected function getIconClasses()
{
$iconLessFile = DIR_BASE_CORE . '/css/build/vendor/font-awesome/variables.less';
$icons = array();
$l = new Less_Parser();
$parser = $l->parseFile($iconLessFile, false, true);
$rules = $parser->rules;
foreach ($rules as $rule) {
if ($rule instanceof Less_Tree_Rule) {
if (strpos($rule->name, '@fa-var') === 0) {
$name = str_replace('@fa-var-', '', $rule->name);
$icons[] = $name;
}
}
}
asort($icons);
return $icons;
}
public function edit()
{
$this->requireAsset('css', 'font-awesome');
$classes = $this->getIconClasses();
// let's clean them up
$icons = array('' => t('Choose Icon'));
$txt = Core::make('helper/text');
foreach ($classes as $class) {
$icons[$class] = $txt->unhandle($class);
}
$this->set('icons', $icons);
}
public function getSearchableContent()
{
return $this->title . ' ' . $this->paragraph;
}
public function save($args)
{
$args = $args + array(
'fID' => 0,
);
$args['fID'] = ($args['fID'] != '') ? $args['fID'] : 0;
switch (isset($args['linkType']) ? intval($args['linkType']) : 0) {
case 1:
$args['externalLink'] = '';
break;
case 2:
$args['internalLinkCID'] = 0;
break;
default:
$args['externalLink'] = '';
$args['internalLinkCID'] = 0;
break;
}
unset($args['linkType']);
parent::save($args);
}
}
Form.php
<?php defined('C5_EXECUTE') or die("Access Denied.");
$al = Loader::helper('concrete/asset_library');
$bf = null;
$bfo = null;
if ($controller->getFileID() > 0) {
$bf = $controller->getFileObject();
}
?>
<fieldset>
<legend><?php echo t('Icon')?></legend>
<div class="form-group ccm-block-feature-select-icon" style="margin-right: 35px;">
<?php echo $form->select('icon', $icons, $icon);?>
<i data-preview="icon" <?php if ($icon) { ?>class="fa fa-<?php echo $icon?>"<?php } ?>></i>
</div>
</fieldset>
<?php defined('C5_EXECUTE') or die("Access Denied.");
$al = Loader::helper('concrete/asset_library');
$bf = null;
$bfo = null;
if ($controller->getFileID() > 0) {
$bf = $controller->getFileObject();
}
?>
<fieldset>
<legend><?php echo t('Icon')?></legend>
<div class="form-group ccm-block-feature-select-icon" style="margin-right: 35px;">
<?php echo $form->select('icon', $icons, $icon);?>
<i data-preview="icon" <?php if ($icon) { ?>class="fa fa-<?php echo $icon?>"<?php } ?>></i>
</div>
</fieldset>
<fieldset>
<legend><?php echo t('Files')?></legend>
<?php
$args = array();
$constrain = $maxWidth > 0 || $maxHeight > 0;
if ($maxWidth == 0) {
$maxWidth = '';
}
if ($maxHeight == 0) {
$maxHeight = '';
}
?>
<div class="form-group">
<label class="control-label"><?php echo t('Image')?></label>
<?php echo $al->image('ccm-b-image', 'fID', t('Choose Image'), $bf, $args);?>
</div>
</fieldset>
<fieldset>
<legend><?php echo t('Text')?></legend>
<div class="form-group">
<?php echo $form->label('title', t('Title'))?>
<?php echo $form->text('title', $title); ?>
</div>
<div class="form-group">
<?php echo $form->label('paragraph', t('Paragraph:'));?>
<?php
$editor = Core::make('editor');
echo $editor->outputBlockEditModeEditor('paragraph', $paragraph);
?>
</div>
</fieldset>
<fieldset>
<legend><?php echo t('Link')?></legend>
<div class="form-group">
<select name="linkType" data-select="feature-link-type" class="form-control">
<option value="0" <?php echo (empty($externalLink) && empty($internalLinkCID) ? 'selected="selected"' : '')?>><?php echo t('None')?></option>
<option value="1" <?php echo (empty($externalLink) && !empty($internalLinkCID) ? 'selected="selected"' : '')?>><?php echo t('Another Page')?></option>
<option value="2" <?php echo (!empty($externalLink) ? 'selected="selected"' : '')?>><?php echo t('External URL')?></option>
</select>
</div>
<div data-select-contents="feature-link-type-internal" style="display: none;" class="form-group">
<?php echo $form->label('internalLinkCID', t('Choose Page:'))?>
<?php echo Loader::helper('form/page_selector')->selectPage('internalLinkCID', $internalLinkCID); ?>
</div>
<div data-select-contents="feature-link-type-external" style="display: none;" class="form-group">
<?php echo $form->label('externalLink', t('URL'))?>
<?php echo $form->text('externalLink', $externalLink); ?>
</div>
</fieldset>
<script type="text/javascript">
$(function() {
$('div.ccm-block-feature-select-icon').on('change', 'select', function() {
$('i[data-preview="icon"]').removeClass();
if($(this).val()) {
$('i[data-preview="icon"]').addClass('fa fa-' + $(this).val());
}
});
$('select[data-select=feature-link-type]').on('change', function() {
if ($(this).val() == '0') {
$('div[data-select-contents=feature-link-type-internal]').hide();
$('div[data-select-contents=feature-link-type-external]').hide();
}
if ($(this).val() == '1') {
$('div[data-select-contents=feature-link-type-internal]').show();
$('div[data-select-contents=feature-link-type-external]').hide();
}
if ($(this).val() == '2') {
$('div[data-select-contents=feature-link-type-internal]').hide();
$('div[data-select-contents=feature-link-type-external]').show();
}
}).trigger('change');
});
</script>
<style type="text/css">
div.ccm-block-feature-select-icon {
position: relative;
}
div.ccm-block-feature-select-icon i {
position: absolute;
right: -25px;
top: 10px;
}
</style>
View.php
<?php defined('C5_EXECUTE') or die("Access Denied."); ?>
<div class="service span3">
<?php if($icon) { ?>
<div class="icon-awesome">
<i class="fa fa-<?php echo $icon?>"></i>
</div>
<?php } ?>
<?php if (is_object($f)) {
$image = Core::make('html/image', array($f));
$tag = $image->getTag();
} ?>
<?php if ($title) { ?>
<h4><?php echo $title?></h4>
<?php } ?>
<?php if ($paragraph) { ?>
<?php defined('C5_EXECUTE') or die("Access Denied."); ?>
<div class="service span3">
<?php if($icon) { ?>
<div class="icon-awesome">
<i class="fa fa-<?php echo $icon?>"></i>
</div>
<?php } ?>
<?php if (is_object($f)) {
$image = Core::make('html/image', array($f));
$tag = $image->getTag();
} ?>
<?php if ($title) { ?>
<h4><?php echo $title?></h4>
<?php } ?>
<?php if ($paragraph) { ?>
<p><?php echo $paragraph;?></p>
<?php } ?>
<?php if ($linkURL) { ?>
<a href="<?php echo $linkURL;?>">Read more</a>
<?php } ?>
</div>
Any help would be appreciated, thanks!
You can try this to display your image in view.php.