V8.5.4 - Adding Page Selector to Custom Block - driving me bonkers
Permalink
February 23, 2021 at 3:09 PM
Hi Folks
I have made a Custom Block called Product Advert, its pretty simple, it has an image selector, a Was Price, a Now Price and also a View Product button. Its all working apart from the View Product Button. I just cant seem to get the Internal Link Selector to work.
The database table shows the cID of the page I have chosen so I know its partially working. But maybe I am missing how to convert that to a url? Am I missing a helper class somewhere?
Can anyone help me out? I have tried searching for this in the forums, but cant find anything more recent than 2015. I did find one post but it just doesnt work. I know I am doing something wrong, just dont know what.
Below is my code.
Controller.php
<?php
namespace Application\Block\ProductAdvert;
defined ( 'C5_EXECUTE' ) or die ( _ ( "Access Denied." ) ) ;
use Concrete\Core\Block\BlockController;
use Core;
use Concrete\Core\Page\Page;
use Concrete\Core\File \File ;
class Controller extends BlockController
{
public $helpers = array ( 'form' ) ;
protected $btTable = "btProductAdvert" ;
protected $btCacheBlockOutput = true ;
protected $btCacheBlockOutputOnPost = true ;
protected $btCacheBlockOutputForRegisteredUsers = true ;
protected $btInterfaceWidth = "450" ;
<?php
namespace Application\Block\ProductAdvert;
defined ( 'C5_EXECUTE' ) or die ( _ ( "Access Denied." ) ) ;
use Concrete\Core\Block\BlockController;
use Core;
use Concrete\Core\Page\Page;
use Concrete\Core\File \File ;
class Controller extends BlockController
{
public $helpers = array ( 'form' ) ;
protected $btTable = "btProductAdvert" ;
protected $btCacheBlockOutput = true ;
protected $btCacheBlockOutputOnPost = true ;
protected $btCacheBlockOutputForRegisteredUsers = true ;
protected $btInterfaceWidth = "450" ;
protected $btInterfaceHeight = "560" ;
protected $btDefaultSet = 'basic' ;
protected $btExportPageColumns = [ 'internalLinkCID' ] ;
public function getBlockTypeName( )
{
return t( 'Product Advert' ) ;
}
public function getBlockTypeDescription( )
{
return t( 'Add a product advert to the page' ) ;
}
public function view( )
{
$image = false ;
if ( $this -> fID ) {
$f = \File :: getByID ( $this -> fID ) ;
if ( is_object ( $f ) ) {
$image = Core:: make ( 'html/image' , array ( $f , false ) ) -> getTag ( ) ;
$image -> alt ( $this -> productName ) ;
$this -> set ( 'image' , $image ) ;
}
}
}
function getInternalLinkCID( ) { return $this -> internalLinkCID ; }
function getLinkURL( ) {
$linkToC = \Page:: getByID ( $this -> internalLinkCID ) ;
if ( is_object ( $linkToC ) ) {
return $linkToC -> getCollectionPath ( ) ;
}
}
}
form.php
<?php defined ( 'C5_EXECUTE' ) or die ( "Access Denied." ) ;
$app = \Concrete\Core\Support\Facade\Application:: getFacadeApplication ( ) ;
?>
<?php
if ( $fID > 0 ) {
$fo = File :: getByID ( $fID ) ;
if ( ! is_object ( $fo ) ) {
unset ( $fo ) ;
}
}
?>
<div class="form-group">
<?php echo $form -> label ( 'productName' , t( 'Page Name' ) ) ; ?>
<?php echo $form -> text ( 'productName' , $productName ) ?>
</div>
<?php defined ( 'C5_EXECUTE' ) or die ( "Access Denied." ) ;
$app = \Concrete\Core\Support\Facade\Application:: getFacadeApplication ( ) ;
?>
<?php
if ( $fID > 0 ) {
$fo = File :: getByID ( $fID ) ;
if ( ! is_object ( $fo ) ) {
unset ( $fo ) ;
}
}
?>
<div class="form-group">
<?php echo $form -> label ( 'productName' , t( 'Page Name' ) ) ; ?>
<?php echo $form -> text ( 'productName' , $productName ) ?>
</div>
<div class="form-group">
<?php echo $form -> label ( 'wasPrice' , t( 'Was Price' ) ) ; ?>
<?php echo $form -> text ( 'wasPrice' , $wasPrice ) ?>
</div>
<div class="form-group">
<?php echo $form -> label ( 'nowPrice' , t( 'Now Price' ) ) ; ?>
<?php echo $form -> text ( 'nowPrice' , $nowPrice ) ?>
</div>
<div class="form-group">
<?php echo $form -> label ( 'internalLinkCID' , t( 'Choose Page:' ) ) ?>
<?php echo Loader:: helper ( 'form/page_selector' ) -> selectPage ( 'internalLinkCID' , $internalLinkCID ) ?>
</div>
<div class="form-group">
<?php echo $form -> label ( 'fID' , t( 'Product Image' ) ) ; ?>
<?php
$al = Loader:: helper ( 'concrete/asset_library' ) ;
echo $al -> file ( 'ccm-b-file' , 'fID' , t( 'Choose File' ) , $fo ) ;
?>
</div>
view.php
<?php defined ( 'C5_EXECUTE' ) or die ( _ ( "Access Denied." ) ) ?>
<a href="<?php echo $linkToC ?> " class="product-link">See Product</a>
<div>
<div class="promo-text">
<h2><?php echo $productName ?> </h2>
<p class="was-price">Was £<?php echo $wasPrice ?> </p>
<p class="now-price">Now £<?php echo $nowPrice ?> </p>
</div>
</div>
<div class="promo-image"><?= $image ?> </div>
db.xml
<? xml version= "1.0" encoding= "UTF-8" ?>
<schema xmlns="http://www.concrete5.org/doctrine-xml/0.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.concrete5.org/doctrine-xml/0.5http://concrete5.github.io/doctrine-xml/doctrine-xml-0.5.xsd"&...
<table name="btProductAdvert">
<field name="bID" type="integer">
<unsigned/>
<key/>
</field>
<field name="fID" type="integer">
<unsigned/>
<default value="0"/>
</field>
<field name="productName" type="string" size="255"></field>
<field name="wasPrice" type="string" size="255"></field>
<field name="nowPrice" type="string" size="255"></field>
<field name="internalLinkCID" type="integer">
<? xml version= "1.0" encoding= "UTF-8" ?>
<schema xmlns="http://www.concrete5.org/doctrine-xml/0.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.concrete5.org/doctrine-xml/0.5http://concrete5.github.io/doctrine-xml/doctrine-xml-0.5.xsd"&...
<table name="btProductAdvert">
<field name="bID" type="integer">
<unsigned/>
<key/>
</field>
<field name="fID" type="integer">
<unsigned/>
<default value="0"/>
</field>
<field name="productName" type="string" size="255"></field>
<field name="wasPrice" type="string" size="255"></field>
<field name="nowPrice" type="string" size="255"></field>
<field name="internalLinkCID" type="integer">
<unsigned/>
<default value="0"/>
</field>
</table>
</schema>
<a href="<?php echo Page::getByID($internalLinkCID)->getCollectionPath(); ?>" class="product-link">See Product</a>
Edit: Also, here's a good starting point to learn more! https://documentation.concrete5.org/developers/pages-themes/working-...
Edit Edit:
It looks like your href should actually be <?php echo $this->getLinkURL(); ?>
your getLinkURL() method should look something like...