Editing dynamic content
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 a component is displayed inside a block, the component content elements are regulated by general editing rules for Sites24.
As described in other articles, component elements can be edited, but not saved (after page is updated). This occurs due to server sending data about specific dynamic element and which part you are editing, but knows nothing about displayed content. The server operates only with component call. However, the Sites24 module provides such option.
Let's take a newsfeed as a simple example. According to Site24 [link=8449617]concept[/link], you will request maximally preset component call parameters inside a block, editing only the actually necessary parameters. Such as, for example, number of news items per page, or a news source.
- First, implement updates in the component template, by adding class and associated ID.
<span class="news-text" data-id="<?= $arItem['ID'];?>"><?echo $arItem["PREVIEW_TEXT"];?></span>
Class news-text below is written in block manifest as text type and news ID is passed indata-id
. - In the nodes section inside the manifest block, indicate that this class has option for editing as text:
'.news-text' => array( 'name' => 'News description', 'type' => 'text_component', 'extend' => array( 'attrs' => array( 'data-id' ) ) ),
Please, take a note that we indicate nottype = text
, but a text_component. We will go in more detail below. But now, pass in "extend" the very same attribute that we have indicated in the template – data-id. This will inform the system that this attribute must be passed to server when saving (otherwise we won't find out which news item we are editing at this moment). - Now we need to register our custom node type text_component. You can indicate a different name.
- We are happy with the behaviour of "text" type, let's inherit from it. As a result, we'll get the following descendant class:
\Bitrix\Main\Loader::includeModule('landing'); class MyTextNode extends \Bitrix\Landing\Node\Text { public static function saveNode(\Bitrix\Landing\Block &$block, $selector, array $data, $additional = array()) { if (!\Bitrix\Main\Loader::includeModule('iblock')) { return; } $iblockEl = new \CIBlockElement; foreach ($data as $pos => $value) { if (isset($additional[$selector . '@' . $pos])) { $data = $additional[$selector . '@' . $pos]; $id = $data['data-id']; $iblockEl->update( $id, array( 'PREVIEW_TEXT' => $value, 'PREVIEW_TEXT_TYPE' => 'html' ) ); } } } }
Redefine only one method saveNode. Now the process is finalized. We return saved data in$data
(in this example, it's a modified news item text), get the news item ID from the returned attribute and modify corresponding news item via iblock module API. - Do not forget to register this new type by the handler
$eventManager = \Bitrix\Main\EventManager::getInstance(); $eventManager->addEventHandler('landing', 'onGetNodeClass', function(\Bitrix\Main\Event $event) { $result = new \Bitrix\Main\Entity\EventResult; $type = $event->getParameter('type'); if ($type == 'text_component') { $result->modifyFields(array( 'class' => '\MyTextNode' )); } return $result; } );
This article describes a basic example of content editing. You can make it more complex by editing not only descriptions and names, but also different properties. You'll need additional attributes for passing to the server: for example, edited property ID.