Documentation

Block class

Attention! We strongly recommend first to learn more about Sites module REST documentation to understand how the module functions (with REST available in Bitrix24 Self-hosted editions). View this documentation as useful source when working with Bitrix24 Self-hosted editions and find out more details about API and only REST is not enough.

When components in blocks can be inserted without changes. However, sometime non-customizable component parameters must be modified before passing them into a component call. For example, inserting link to a shipping cart if shopping cart exists in the site structure. Such conditions can be technically inserted into the block.php body, but stylistically it looks messy.

That's why for these purposes (and presently only for them) so-called block class was introduced. It is a class.php file, located next to block.php and containing class, inherited from \Bitrix\Landing\LandingBlock. Class can have any name that doesn't match with system names and is a valid class name.


Attention! Aside from the class.php itself, specify the block manifest's namespace (directory, where block is located):

return array(
   'block' =>
      array(
         .....
         'namespace' => 'bitrix'
      ),

Three methods can be redefined inside the class:

  • init(array $params = [])
    Called once once per page, independently from the number of blocks for this class. Use to define shared parameters for any blocks of this page class.

    Returns an array of parameters:
    site_id – site ID for block page
    landing_id – block page ID.

  • beforeView(\Bitrix\Landing\Block $block)
    Called each time before calling this class on page block. Use the method only when modifying a specific block. Returns current block object.
  • beforeAdd(\Bitrix\Landing\Block $block)

    Called once when creating block on a page. It doesn't matter if block is added in editor, when creating a template or via REST. Allows to additionally process the content for specific block before inserting. Receives object of current block.

Modify internal array $this->params in side one of the methods above. Then you can handle these parameters directly inside block.

Example

Let's take a look at an example. Below is full-fledged class for one of blocks. We return array of system site pages in the method init. Then we decide to initiate the parameter SHOW_PERSONAL_LINK as Y or N based on existing personal section page. The method beforeView is provided as an example and it logs the current block ID.

<?php
if (!defined('B_PROLOG_INCLUDED') || B_PROLOG_INCLUDED !== true)
{
   die();
}

use \Bitrix\Landing\Manager;

class CatalogListBlock extends \Bitrix\Landing\LandingBlock
{
   public function init(array $params = [])
   {
      $syspages = \Bitrix\Landing\Syspage::get(
         $params['site_id'],
         true
      );

      if (isset($syspages['personal']) && Manager::getUserId())
      {
         $this->params['SHOW_PERSONAL_LINK'] = 'Y';
      }
      else
      {
         $this->params['SHOW_PERSONAL_LINK'] = 'N';
      }
   }

   public function beforeView(\Bitrix\Landing\Block $block)
   {
      addMessage2Log('call block#' . $block->getId());
   }
}>

Then when calling components we can directly use a call for this class instance inside block.php:

$classBlock->get('SHOW_PERSONAL_LINK')

Or call a full block.php

<?php
if (!defined('B_PROLOG_INCLUDED') || B_PROLOG_INCLUDED !== true)
{
   die();
}
?>

<?$APPLICATION->IncludeComponent(
   'bitrix:sale.basket.basket.line',
   '.default',
   array(
      'PATH_TO_BASKET' => '#system_cart',
      'PATH_TO_PERSONAL' => '#system_personal',
      'SHOW_PERSONAL_LINK' => 'N',
      'SHOW_NUM_PRODUCTS' => 'Y',
      'SHOW_TOTAL_PRICE' => 'Y',
      'SHOW_PRODUCTS' => 'N',
      'POSITION_FIXED' => 'Y',
      'SHOW_AUTHOR' => $classBlock->get('SHOW_PERSONAL_LINK'),
      'SHOW_REGISTRATION' => 'N',
      'PATH_TO_REGISTER' => ''
   ),
   false
);?>


© «Bitrix24», 2001-2024