Views: 9982
Last Modified: 19.09.2014

Quite often there are several solutions available to solve the same task. It is up to the developer to choose the final option based on the task at hand. Let us consider the examples of solving a task for which two solutions are available.

How to embed a video when posting news on a website? It may seem difficult at first. However, it is actually quite easy. The idea consists in connecting the component Media Player (bitrix:player) for a file attached to the news. The component News details (bitrix:news.detail) will be used to display the news.

Whatever solution you choose, you will have to create a property of the File type in the news infoblock.

Solution Involving Template Editing

  • Copy the component template news.detail to the website template. You will not have to change the component itself.
  • Create a new page using visual editor and place the component Media Player (bitrix:player) on it. Indicate basic settings (do not enter the path to the video file at this stage). Copy the following code from the obtained:
    <?$APPLICATION->IncludeComponent(
       "bitrix:player",
       "",
       Array(
          "PLAYER_TYPE" => "auto", 
          "USE_PLAYLIST" => "N", 
          "PATH" => "",
          "WIDTH" => "400", 
          "HEIGHT" => "300", 
          "FULLSCREEN" => "Y", 
          "SKIN_PATH" => "/bitrix/components/bitrix/player/mediaplayer/skins", 
          "SKIN" => "bitrix.swf", 
          "CONTROLBAR" => "bottom", 
          "WMODE" => "transparent", 
          "HIDE_MENU" => "N", 
          "SHOW_CONTROLS" => "Y", 
          "SHOW_STOP" => "N", 
          "SHOW_DIGITS" => "Y", 
          "CONTROLS_BGCOLOR" => "FFFFFF", 
          "CONTROLS_COLOR" => "000000", 
          "CONTROLS_OVER_COLOR" => "000000", 
          "SCREEN_COLOR" => "000000", 
          "AUTOSTART" => "N", 
          "REPEAT" => "N", 
          "VOLUME" => "90", 
          "DISPLAY_CLICK" => "play", 
          "MUTE" => "N", 
          "HIGH_QUALITY" => "Y", 
          "ADVANCED_MODE_SETTINGS" => "N", 
          "BUFFER_LENGTH" => "10", 
          "DOWNLOAD_LINK_TARGET" => "_self" 
       )
    );?>
  • In the component template, set up the media player connection instead of the movie property. Find the strings for property display:
    30         <?foreach($arResult["DISPLAY_PROPERTIES"] as $pid=>$arProperty):?>
     31
     32                 <?=$arProperty["NAME"]?>: 
     33                 <?if(is_array($arProperty["DISPLAY_VALUE"])):?>
     34                         <?=implode(" / ", $arProperty["DISPLAY_VALUE"]);?>
     35                 <?else:?>
     36                         <?=$arProperty["DISPLAY_VALUE"];?>
     37                 <?endif?>
     38                 <br />
     39         <?endforeach;?>
  • Insert checking and replacement. The result:
    <?foreach($arResult["DISPLAY_PROPERTIES"] as $pid=>$arProperty):?>
    <?if ($arProperty["CODE"]=='movie' && $arProperty["DISPLAY_VALUE"]) {?>
    
    <?$APPLICATION->IncludeComponent(
       "bitrix:player",
       "",
       Array(
          "PLAYER_TYPE" => "auto", 
          "USE_PLAYLIST" => "N", 
          "PATH" => CFile::GetPath($arProperty["VALUE"]),
          "WIDTH" => "400", 
          "HEIGHT" => "300", 
          "FULLSCREEN" => "Y", 
          "SKIN_PATH" => "/bitrix/components/bitrix/player/mediaplayer/skins", 
          "SKIN" => "bitrix.swf", 
          "CONTROLBAR" => "bottom", 
          "WMODE" => "transparent", 
          "HIDE_MENU" => "N", 
          "SHOW_CONTROLS" => "Y", 
          "SHOW_STOP" => "N", 
          "SHOW_DIGITS" => "Y", 
          "CONTROLS_BGCOLOR" => "FFFFFF", 
          "CONTROLS_COLOR" => "000000", 
          "CONTROLS_OVER_COLOR" => "000000", 
          "SCREEN_COLOR" => "000000", 
          "AUTOSTART" => "N", 
          "REPEAT" => "N", 
          "VOLUME" => "90", 
          "DISPLAY_CLICK" => "play", 
          "MUTE" => "N", 
          "HIGH_QUALITY" => "Y", 
          "ADVANCED_MODE_SETTINGS" => "N", 
          "BUFFER_LENGTH" => "10", 
          "DOWNLOAD_LINK_TARGET" => "_self" 
       ),
       $component   
    );?> 
    <? } else {?>
          <?=$arProperty["NAME"]?>: 
          <?if(is_array($arProperty["DISPLAY_VALUE"])):?>
             <?=implode(" / ", $arProperty["DISPLAY_VALUE"]);?>
          <?else:?>
             <?=$arProperty["DISPLAY_VALUE"];?>
          <?endif?>
    <?}?>
          <br />
       <?endforeach;?>
Note: Please pay attention to the following:
  • The system call CFile::GetPath is used to obtain the path to the file from the ID.
  • When connecting components, the fourth parameter $component is indicated so that its parameters cannot be changed from the public part

Solution Using result_modifier.php

If you want to continue using the updated component the task should be solved using result_modifier.php.

  • Create the file result_modifier.php with the code:
    <?
    // transfer property value using the link:
    $arProperty = &$arResult['DISPLAY_PROPERTIES'][$arParams['PROPERTY_VIDEO']];
    
    if ($arProperty['DISPLAY_VALUE']) // verify whether the property is set
    {
       global $APPLICATION;
       ob_start(); // activate buffering to catch component retrieval
       $APPLICATION->IncludeComponent(
          "bitrix:player",
          "",
          Array(
             "PLAYER_TYPE" => "auto", 
             "USE_PLAYLIST" => "N", 
             "PATH" => CFile::GetPath($arProperty["VALUE"]),
             "WIDTH" => "400", 
             "HEIGHT" => "300", 
             "FULLSCREEN" => "Y", 
             "SKIN_PATH" => "/bitrix/components/bitrix/player/mediaplayer/skins", 
             "SKIN" => "bitrix.swf", 
             "CONTROLBAR" => "bottom", 
             "WMODE" => "transparent", 
             "HIDE_MENU" => "N", 
             "SHOW_CONTROLS" => "Y", 
             "SHOW_STOP" => "N", 
             "SHOW_DIGITS" => "Y", 
             "CONTROLS_BGCOLOR" => "FFFFFF", 
             "CONTROLS_COLOR" => "000000", 
             "CONTROLS_OVER_COLOR" => "000000", 
             "SCREEN_COLOR" => "000000", 
             "AUTOSTART" => "N", 
             "REPEAT" => "N", 
             "VOLUME" => "90", 
             "DISPLAY_CLICK" => "play", 
             "MUTE" => "N", 
             "HIGH_QUALITY" => "Y", 
             "ADVANCED_MODE_SETTINGS" => "N", 
             "BUFFER_LENGTH" => "10", 
             "DOWNLOAD_LINK_TARGET" => "_self" 
          )
       ); 
       $arProperty['DISPLAY_VALUE'] = ob_get_contents(); // substitute $arResult
       ob_clean(); // clean our buffer so that the player do not appear twice
       ob_end_clean(); // close the buffer
    }
    ?>

    The symbol code of the property can be made the parameter of the component to avoid the strict connection to a specific infoblock. To do so, the file .parameters.php of the News details component located in the copied component template must be adjusted.

  • Change the code of the file .parameters.php:
    <?
    if (!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true) die();
    $arProps = array(); 
    $rs=CIBlockProperty::GetList(array(),array("IBLOCK_ID"=>$arCurrentValues['IBLOCK_ID'],"ACTIVE"=>"Y"));
    while($f = $rs->Fetch())
       $arProps[$f['CODE']] = $f['NAME'];
    
    $arTemplateParameters = array(
       "DISPLAY_DATE" => Array(
          "NAME" => GetMessage("T_IBLOCK_DESC_NEWS_DATE"),
          "TYPE" => "CHECKBOX",
          "DEFAULT" => "Y",
       ),
       "DISPLAY_NAME" => Array(
          "NAME" => GetMessage("T_IBLOCK_DESC_NEWS_NAME"),
          "TYPE" => "CHECKBOX",
          "DEFAULT" => "Y",
       ),
       "DISPLAY_PICTURE" => Array(
          "NAME" => GetMessage("T_IBLOCK_DESC_NEWS_PICTURE"),
          "TYPE" => "CHECKBOX",
          "DEFAULT" => "Y",
       ),
       "DISPLAY_PREVIEW_TEXT" => Array(
          "NAME" => GetMessage("T_IBLOCK_DESC_NEWS_TEXT"),
          "TYPE" => "CHECKBOX",
          "DEFAULT" => "Y",
       ),
       "PROPERTY_VIDEO" => Array(
          "NAME" => "Property where the video is stored",
          "TYPE" => "LIST",
          "VALUES" => $arProps
       ),
    );
    ?>

As a result, a new parameter will appear in the component settings.

Do not forget to indicate a property where the video is stored in the component connection parameters. Otherwise, it will not be displayed.



Courses developed by Bitrix24