Example of source registration
Example
use \Bitrix\Main; // our class that will select data by source elements class MyLoader extends \Bitrix\Landing\Source\DataLoader { public function getElementListData() { // gets list of elements from a source $this->seo->clear(); // must be always be available at the method start - clearing SEO parameters $select = $this->getPreparedSelectFields(); // gets list of fields and properties required for elements if (empty($select)) { return []; // won't display anything for an unconfigured block } $filter = $this->getPreparedFilter($this->getFilterFields()); // get filter for ORM or GetList of old kernel $internalFilter = $this->getInternalFilter(); // get additional filter (see SOURCE_FILTER in the event handler) if (!empty($internalFilter) && is_array($internalFilter)) { $filter = array_merge($filter, $internalFilter); // if additional filter isn't empty, it has a priority } if (empty($filter)) { return []; // if no filter, display nothing } $order = []; // result sorting $rawOrder = $this->getOrder(); if (isset($rawOrder['by']) && isset($rawOrder['order'])) { $order[$rawOrder['by']] = $rawOrder['order']; if (!in_array($rawOrder['by'], $select) { $select[] = $rawOrder['by']; // take note that code will operate at mysql 5.7 and higher } } $limit = $this->getLimit(); // get maximum number of elements to be selected if ($limit <= 0) { $limit = 50; // if limit is not specified, limit by a specific sensible value } $result = []; $iterator = \MyModule::getList([ 'select' => $select, 'filter' => $filter, 'order' => $order, 'limit' => $limit ]); while ($row = $iterator->fetch()) { // data must be converted to HMTL-safe view. Implementation depends on developer choice ... $result[] = $row; } return $result; } public function getElementData($element) { // get a specific element from source $this->seo->clear(); // must be always available at the method start - clearing SEO parameters $select = $this->getPreparedSelectFields(); // get list of fields and properties, necessary for element if (empty($select)) { return []; // display nothing for unconfigured block } // suppose that element ID is an integer if (!is_string($element) && !is_int($element)) { return []; } $element = (int)$element; if ($element <= 0) { return []; } // for details page, check the filter to block the element display, not included into our selection $filter = $this->getPreparedFilter($this->getFilterFields()); // get filter for ORM or GetList of old kernel $internalFilter = $this->getInternalFilter(); // get additional filter (see SOURCE_FILTER in the event handler) if (!empty($internalFilter) && is_array($internalFilter)) { $filter = array_merge($filter, $internalFilter); // if additional filter isn't empty, it has priority } if (empty($filter)) { return []; // if no filter is available, display nothing } $filter['ID'] = $element; // limit the selection to include a single element only $iterator = \MyModule::getList([ 'select' => $select, 'filter' => $filter, ]); $row = $iterator->fetch(); if (empty($row)) { return []; } // data must be converted to HMTL-safe view. Implementation depends on developer choice $this->seo->setTitle($row['NAME']); // set as title for element name return [$row]; } /** * Description of fields for converting block filter to ORM filter * * @return array */ private function getFilterFields() { $result = []; // NAME string field $result['NAME'] = [ 'id' => 'NAME', // identifier (matches with array key) 'type' => 'string', // String field (main.ui.filter) 'operators' => [ // prefixes for conditions 'default' => '%' // default prefix ] ]; // поле ID типа число $result['ID'] = [ 'id' => 'ID', // identifier (matches with array key) 'type' => 'number', // Number field (main.ui.filter) 'operators' => [ // prefixes for conditions 'default' => '=', // default prefix 'exact' => '=', // precise match (equal) (X === value) 'range' => '><', // range filter (min <= X <= max) 'more' => '>', // more (X > value) 'less' => '<' // less (X < value) ] ]; // SECTION_ID list field $result['SECTION_ID'] = [ 'id' => 'SECTION_ID', // identifier (matches with array key) 'type' => 'list', // List field type (main.ui.filter) 'operators' => [ // prefixes for conditions 'default' => '=' // default prefix ] ]; // INCLUDE_SUBSECTIONS checkbox field $result['INCLUDE_SUBSECTIONS'] = [ 'id' => 'INCLUDE_SUBSECTIONS', // identifier (matches with array key) 'type' => 'checkbox', Checkbox field type (main.ui.filter) 'operators' => [ // prefixes for conditions 'default' => '' // default prefix ] ]; return $result; } } // handler registration $eventManager = \Bitrix\Main\EventManager::getInstance(); $eventManager->registerEventHandler('landing', 'OnBuildSourceList', 'partner.mymodule', '\MySource', 'sourceBuildHandler'); // handler class for passing a description (registration) of source class MySource { public static function sourceBuildHandler(\Bitrix\Main\Event $event) { /** @var \Bitrix\Landing\Source\Selector $selector */ $selector = $event->getParameter('SELECTOR'); if (!$selector->checkSiteMode([\Bitrix\Landing\Source\Selector::SITE_MODE_PAGE])) // example - source only for sites24 { return new Main\EventResult(Main\EventResult::ERROR, null, 'module_ID'); } $restrictions = $selector->getModuleRestrictions('module_ID'); // gets limits if (!empty($restrictions['SITE_ID']) && $restrictions['SITE_ID'] !== 's2') //example - source only for site with code s2 { return new Main\EventResult(Main\EventResult::ERROR, null, 'module_ID'); } $path = $selector->getSourceFilterBaseUri('module_ID', 'source_ID'); $baseUri = $path->getUri(); // path to be used as the base for filter settings form (per page or selection of elements in form) $componentParams = [ ... // parameters, necessary for component 'BASE_LINK' => $baseUri // key name - any, but component must process it ]; $result = []; $result[] = [ 'SOURCE_ID' => 'source_ID', 'TITLE' => 'my source', 'TYPE' => \Bitrix\Landing\Source\Selector::SOURCE_TYPE_COMPONENT, 'SETTINGS' => [ 'COMPONENT_NAME' => 'my:selector.landing', // filter settings component 'COMPONENT_TEMPLATE_NAME' => '.default', 'COMPONENT_PARAMS' => $componentParams, 'WRAPPER' => [ // optional settings, passed in bitrix:ui.sidepanel.wrapper 'USE_PADDING' => false, 'PLAIN_VIEW' => false, 'USE_UI_TOOLBAR' => 'N' ] ], 'SOURCE_FILTER' => [ // additional filter '=ACTIVE' => 'Y' // display only active elements ], 'DATA_SETTINGS' => [ 'FIELDS' => [ // fields for displaying in block 0 => [ 'id' => 'ID', 'name' => 'Identifier', 'type' => \Bitrix\Landing\Node\Type::TEXT, ] 1 => [ 'id' => 'NAME', 'name' => 'Name', 'type' => \Bitrix\Landing\Node\Type::TEXT, ], 2 => [ 'id' => 'PICTURE', 'name' => 'Image', 'type' => \Bitrix\Landing\Node\Type::IMG, ], ... N => [ 'id' => 'QUANTITY', 'name' => 'Quantity', 'type' => \Bitrix\Landing\Node\Type::TEXT, ], ], 'ORDER' => [ 0 => [ 'id' => 'ID', 'name' => 'Identifier', ], 1 => [ 'id' => 'NAME', 'name' => 'Name', ], ... N => [ 'id' => 'AVAILABLE', 'name' => 'Availability', ], ] ], 'DATA_LOADER' => '\MyLoader' // data loader class ]; return new Main\EventResult(Main\EventResult::SUCCESS, $result, 'module_ID'); } }
© «Bitrix24», 2001-2024