Documentation

onBlockGetRepository

Event allows updating an array of blocks, passed from repository. For example, for deleting specific templates.

Parameters

:
Class Description Available from version
blocks Array of blocks.

Important! This method is not suitable for adding new templates (see. template introduction). It is used only for correcting (for example, for categories) or deleting existing templates.

Example

Abstract example below illustrates a block array and we delete it when block version is incorrect.

$eventManager = \Bitrix\Main\EventManager::getInstance();
$eventManager->addEventHandler('landing', 'onBlockGetRepository',
   function(\Bitrix\Main\Event $event)
   {
      $result = new \Bitrix\Main\Entity\EventResult;
      $blocks = $event->getParameter('blocks');

      foreach ($blocks as $secCode => &$section)
      {
         foreach ($section['items'] as $blockCode => &$block)
         {
            if (isset($block['manifest']['block']['version']))
            {
               $blockVersion = $block['manifest']['block']['version'];
               if (version_compare('15.0.0', $blockVersion) < 0)
               {
                  unset($section['items'][$blockCode]);
               }
            }
         }
         unset($block);
      }
      unset($section);

      $result->modifyFields(array(
         'blocks' => $blocks
      ));

      return $result;
   }
);


© «Bitrix24», 2001-2024
Up