Blocks and widgets

What's the difference?

Blocks and widgets are the visible parts of your websites. The blocks, you'll find in the actions folder, the widgets you'll find in the widgets folder. Their operation is exactly the same with two differences:

  • There can only be one “block” on a page.
  • Because of this, each block has an url (that can be fetched using the function getUrlForBlock)

This block can be a module, with every action included, or just one action of a module.

As long as the selected template allows it, there can be as many widgets as you want on a page. The structure of a widget is (most of the time) less complicated than a block because they are used merely for displaying data.

Structure

When you check out other existing Fork modules, you will see that most actions use the same structure, using the same method names. Beneath you'll find the complete code of the detail-action of our mini blog.

namespace Frontend\Modules\MiniBlog\Actions;

use Frontend\Core\Engine\Base\Block as FrontendBaseBlock;

class Detail extends FrontendBaseBlock
{

The classname needs to be the exact same as the action name and the file name, in our case Detail. Because our action is a Block, it extends Frontend\Core\Engine\Base\Block. This class takes care of everything concerning url-handling, breadcrumbs, ...

/** 
 * The blogpost 
 * 
 * @var array 
 */ 
private $record; 

Then we define our (private) variables. In our case we'll use an array to save the record with the article we will be viewing.

public function execute(): void
{
 // call the parent 
 parent::execute();

 // hide contenTitle, in the template the title is wrapped with an inverse-option
 $this->template->assign('hideContentTitle', true); 

 // load template 
 $this->loadTemplate(); 

 // load the data 
 $this->getData(); 

 // parse 
 $this->parse(); 
}

The execute function is always present and is called by Fork CMS when opening any action. As you can see, the execute method of Frontend\Core\Engine\Base\Block is called too. This makes sure that the js-files and css-files are autoloaded.

The line starting with "$this->template->assign(" ... assigns a variable to the template we'll be using to display the action.

loadTemplate (also defined in Frontend\Core\Engine\Base\Block) loads the template file in which we parse the data we'll be loading in our self defined method getData.

use Frontend\Core\Engine\Navigation as FrontendNavigation;
use Frontend\Modules\MiniBlog\Engine\Model as FrontendMiniBlogModel;
use Frontend\Modules\Tags\Engine\Model as FrontendTagsModel;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

...
private function getData(): void
{
 // if no parameter was passed we show the 404-page
 if($this->url->getParameter(1) === null) {
     throw new NotFoundHttpException();
 }

 // get the record, or at least try it
 $this->record = FrontendMiniBlogModel::get($this->url->getParameter(1));

 // if the record is empty it is an invalid one, so show the 404-page
 if(empty($this->record)) {
     throw new NotFoundHttpException();
 }

 // add some extra info to the record
 $this->record['full_url'] = FrontendNavigation::getUrlForBlock('mini_blog' , 'detail') . '/' . $this->record['url'];
 $this->record['tags'] = FrontendTagsModel::getForItem('mini_blog' , $this->record['id']);
} 

The getData function first checks if the item given in the url exists and adds some extra data which will be used in the template, or redirects to a 404-page if it doesn't. (The 404 page is installed by default when installing Fork). If an article was found, the data we fetched is parsed into the template-file.

private function parse(): void
{
 $this->breadcrumb->addElement($this->record['title']);

 $this->header->setPageTitle($this->record['title']);
 $this->header->setMetaDescription($this->record['meta_description'] , $this->record['meta_description_overwrite']);
 $this->header->setMetaKeywords($this->record['meta_keywords'] , $this->record['meta_keywords_overwrite']);

 $this->template->assign('item', $this->record);
 $this->template->assign('navigation' , FrontendMiniBlogModel::getNavigation($this->record['id']));
 }
}

As you can see, it's fairly easy to add an item to Fork's breadcrumb object and to add the meta-data to the

of the page. We discuss both objects later on.