Custom Blocks, Namespaces and Classes

Permalink
Hi-

I am converting custom blocks from a 5.6 site to a 5.8 site. The blocks pull data from proprietary tables and then use custom templates to format the data. When adding a block to a page, the add method is used to obtain a hike_id that in turn is used to query the database and pull the proper data.

I have looked at several concrete blocks, such as Survey and GoogleMaps to try to understand Concrete5 classes. I believe I need the following in my namespace:

<?php
namespace Concrete\Block\HikingDestination;

use Concrete\Core\Block\BlockController;
use Page;
use Core;
use Database;

class Controller extends BlockController {....

Is this correct?
When do you need to use Config?

One of the blocks requires the input of a file id to pull an image. In my 5.6 block I used the helper function:

private function get_image_object

Do I need to use a specific class for images that includes this method? It looks like what I need might be included in the BlockController. Is this correct?

Is there any more detailed documentation explaining the various classes and when they should be used?

Thank you.

View Replies:
hutman replied on at Permalink Reply
hutman
Without seeing the code for your block as it is currently it's very hard to point you in the direction of what namespaces you'll need to use and what functions you'll need to use.
dgreer replied on at Permalink Reply
Sorry for not providing enough information. Here is the controller for a simple block: (Not sure I need the btName and btDescription) edit.php has two inputs: destination_code and destination_name

<?php
namespace Concrete\Block\HikingDestination;

use Concrete\Core\Block\BlockController;
use Page;
use Core;
use Database;

class Controller extends BlockController
{
protected $btTable = 'btHikingDestination';
protected $btName = 'Hiking Destination';
protected $btDescription = 'Hiking Destination Pages';
protected $btInterfaceWidth = "600";
protected $btInterfaceHeight = "450";

public function getBlockTypeDescription()
{
return "Enter a hiking destination state (region if intl) code and name to access the regiondata table and display the hiking destination page for the state/region.";
}

public function getBlockTypeName()
{
return "Hiking Destination";
}

public function getSearchableContent()
{
$content = array();
$content[] = $this->destination_code;
$content[] = $this->destination_name;
return implode(' - ', $content);
}

public function view() {

/* Retrieve data from the main RegionData table */

$destination_key = trim($this->destination_code);

$db = \Database::connection();
$results = $db->fetchColumn('SELECT region_code,region_name,region_order,country_code,state_code,short_desc,latitude,longitude,zoom FROM RegionData WHERE state_code = ? ORDER BY region_order', [$destination_key])

/* Get the data for each region within the state */
$this->set('regioncount' , $results->RecordCount());
$i = 1;
while($row=$results->fetchrow())
{
$this->set("region_code{$i}", $row['region_code']);
$this->set("region_name{$i}", $row['region_name']);
$this->set("region_order{$i}", $row['region_order']);
$this->set("country_code{$i}", $row['country_code']);
$this->set("state_code{$i}", $row['state_code']);
$this->set("short_desc{$i}", $row['short_desc']);
$this->set("latitude{$i}", $row['latitude']);
$this->set("longitude{$i}", $row['longitude']);
$this->set("zoom{$i}", $row['zoom']);
++$i;
}
}

public function edit() {
}

public function save($args)
{
parent::save($args);
}
}
?>
dgreer replied on at Permalink Reply
Sorry -- working too fast --

Corrected two syntax errors:
Should be Application\Block\HikingDestination not Concrete\Block\HikingDestination

Added a semicolon after the $results = $db->fetchColumn .... line
dgreer replied on at Permalink Reply
Also have all the database code wrong because of the new DBAL doctrine. See corrected View function (below), although I am still having problems viewing the data in the rows. Probably more mistakes in how I am fetching the data from the rows.

public function view() {

/* Retrieve data from the main RegionData table */

$destination_key = trim($this->destination_code);

$db = \Database::connection();
$sql = "SELECT region_code,region_name,region_order,country_code,state_code,short_desc,latitude,longitude,zoom FROM RegionData WHERE state_code = ? ORDER BY region_order";
$results = $db->prepare($sql);
$results->bindValue(1, $destination_key);
$results->execute();

/* Get the data for each region within the state */
$this->set('regioncount' , $results->rowCount());
$i = 1;
while($row=$results->fetch())
{
$this->set("region_code{$i}", $row['region_code']);
$this->set("region_name{$i}", $row['region_name']);
$this->set("region_order{$i}", $row['region_order']);
$this->set("country_code{$i}", $row['country_code']);
$this->set("state_code{$i}", $row['state_code']);
$this->set("short_desc{$i}", $row['short_desc']);
$this->set("latitude{$i}", $row['latitude']);
$this->set("longitude{$i}", $row['longitude']);
$this->set("zoom{$i}", $row['zoom']);
++$i;
}
}