Scaffold the Block Type and Add to Package

Let’s create a project_grid block type in our Dreamrs block types directory:

cd public/packages/dreamrs  
mkdir blocks  
cd blocks  
mkdir project_grid

Icon

Let’s generate an icon based on Font Awesome like we did for our Page Header block type. I’m going to choose oi-grid-two-up:

I’ll name this file icon.png, and place it within the project_grid directory.

Controller

Next, we need to create the controller for the block type. Copy the controller from your Page Header block:

cp ../page_header/controller.php .

Now let’s edit controller.php. Change out all the fields specific to the Page Header block:

namespace Concrete\Package\Dreamrs\Block\ProjectGrid;
use Concrete\Core\Block\BlockController;
defined('C5_EXECUTE') or die('Access Denied.');

class Controller extends BlockController
{
    protected $btInterfaceWidth = 470;
    protected $btInterfaceHeight = 300;
    protected $btCacheBlockOutput = true;
    protected $btCacheBlockOutputOnPost = true;
    protected $btCacheBlockOutputForRegisteredUsers = false;
    protected $btTable = 'btDreamrsProjectGrid';
    protected $btDefaultSet = 'basic';

    public function getBlockTypeDescription()
    {
        return t('Shows a grid of project pages.');
    }

    public function getBlockTypeName()
    {
        return t('Project Grid');
    }

    public function add()
    {

    }

    public function edit()
    {

    }


    public function view()
    {

    }
}

We’ve changed the title to Project Grid, changed the namespace, and changed the description. We changed the database table because this block type will need to store its data in its own location. Additionally, we also removed our custom save() method, since it was specific to the Page Header block type.

One more thing: we also changed the $btCacheBlockOutputForRegisteredUsers property. Why? Since this block deals with the display of pages, we’re going to want to make sure that page permissions are respected. Since all unregistered users are treated the same, we can cache the block for those who visit the Projects page that aren’t registered. But if a registered user visits the page, we’re no longer sure whether they have access to view every project page that shows up in the grid. That means we can’t cache the block if a registered user comes to the page. So we set that option to false.

add.php and edit.php

Let’s copy add.php and edit.php into the block’s directory from the Page Header block.

cp ../page_header/add.php .  
cp ../page_header/edit.php .

add.php doesn’t need any further modification -- it simply includes edit.php and that will be the same with this block type. But edit.php needs to have this block’s specific fields included.

Open edit.php in your editor:

Looking at this controller.php, I see our Page Header already has a field that is similar to the one that we need in this block -- it’s the customPageHeaderTitle field. This field is a text field just like our “Project Grid Headline” is going to be. So I think a good way of modifying this template for this new block type will be to:

  1. Remove everything related to the Background Picture field.
  2. Remove everything related to the Override Page Title field.
  3. Change the customPageHeaderTitle field to “Project Grid Headline.”

Let’s do it. First, delete everything related to $fID and $overridePageTitle at the top of the script, then remove the form groups that have to do with those two fields. Finally, remove the $overridePageName JavaScript. You’ll be left with this:

Looks pretty simple. Now, let’s change customPageHeaderTitle into ‘projectGridHeadline’ everywhere, because that’s the field this block type uses. We can also remove the data-group HTML attribute, because it was only used for JavaScript that we no longer use.

view.php

Instead of copying view.php from our Page Header block, let’s just make an empty file, and add a test string to it.

touch view.php

Open view.php and add our C5_EXECUTE line along with some testing text:

db.xml

Let’s copy out the db.xml file from our Page Header block, and perform the same kind of substitution that we did on the form fields. This is the result:

(Important: make sure that you update the <table> name to “btDreamrsProjectGrid”).

We now have everything we need in our folder, so let’s install our block type. Open the block_types.xml in the install/ directory and add an entry for our new block type:

Then, update the package version to 0.6.7 and update the package:

With that, our block type is installed: