Views: 10740
Last Modified: 11.09.2014

Let us create a component integrating the popular editor TinyMCE into Bitrix Framework.

Download the latest version of the editor from the manufacturer’s website.

In an own namespace, set up a structure of folders and files:

  • /bitrix/components/tools/;
    • /bitrix/components/tools/editor.tiny.mce/;
      • /bitrix/components/tools/editor.tiny.mce/templates/;
        • /bitrix/components/tools/editor.tiny.mce/templates/.default/;
      • /bitrix/components/tools/editor.tiny.mce/tiny_mce/ - a folder for editor installation package;
      • component.php — component logics;
      • .parameters.php — a file to describe input parameters.

Copy the downloaded installation package into the folder /tiny_mce.

Add the following code in the file component.php:

<?if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true) die();
	$APPLICATION->AddHeadScript($this->__path .'/tiny_mce/tiny_mce.js');

	$sNameTextAria   =  (isset($arParams['TEXTARIA_NAME'])   == false) ? 'content'   : $arParams['TEXTARIA_NAME'];
	$sIdTextAria   	 =  (isset($arParams['TEXTARIA_ID'])     == false) ? '' 		 : $arParams['TEXTARIA_ID'];
             if ('' == trim($sIdTextAria))
             $sIdTextAria = 'content';
	$sEditorID 		 =  (isset($arParams['INIT_ID'])  	     == false) ? 'textareas' : $arParams['INIT_ID'];
	$iTextariaWidth  =  (isset($arParams['TEXTARIA_WIDTH'])  == false) ? '100%'      : $arParams['TEXTARIA_WIDTH'];
	$iTextariaHeight =  (isset($arParams['TEXTARIA_HEIGHT']) == false) ? '300'       : $arParams['TEXTARIA_HEIGHT'];
	$sText 			 =  (isset($arParams['TEXT']) 			 == false) ? ''       	 : $arParams['TEXT'];
	?>

<script type="text/javascript">

<? 
if($arParams['TYPE_EDITOR'] == 'TYPE_1')
{
	?>
	tinyMCE.init(
		{
			language : 'ru',
			mode 	 : "textareas",
			//elements : "<?=$sEditorID?>",
			editor_selector : "<?=$sEditorID?>",
			theme    : "advanced",
			plugins  : "safari, spellchecker, upload.images.komka, wordcount, fullscreen",
			theme_advanced_buttons1 : "formatselect,fontselect,fontsizeselect,bold,italic,underline,link,justifyleft,justifycenter,
                                       justifyright,pasteword,pastetext,images,|,bullist,numlist,|,undo,redo,|,spellchecker,fullscreen",
			theme_advanced_buttons2 : "",
			theme_advanced_buttons3 : "",
			theme_advanced_toolbar_location   : "top",
			theme_advanced_toolbar_align      : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing           : false,
			content_css                       : "<?=$this->__path?>/example.css",
			height : "<?=$iTextariaHeight?>",
			spellchecker_languages : '+Русский=ru,English=en',
			spellchecker_word_separator_chars : '\\s!\"#$%&()*+,-./:;<=>?@[\]^_{|}'
		}
	);
	<? 
}
elseif($arParams['TYPE_EDITOR'] == 'TYPE_2')
{
	?>
		tinyMCE.init({
				language : 'ru',
				mode 	 : "textareas",
				editor_selector : "<?=$sEditorID?>",
				theme    : "advanced",
				plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,
                           iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,
                           fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imagemanager,filemanager",
				theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,
                                           justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
				theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,
                                           numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,
                                           insertdate,inserttime,preview,|,forecolor,backcolor",
				theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,
                                           emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
				theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,
                                           spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,
                                           pagebreak,|,insertfile,insertimage",
				theme_advanced_toolbar_location : "top",
				theme_advanced_toolbar_align : "left",
				theme_advanced_statusbar_location : "bottom",
				theme_advanced_resizing : true,
				content_css : "<?=$this->__path?>/example.css",
				height : "<?=$iTextariaHeight?>",
				template_external_list_url : "js/template_list.js",
				external_link_list_url : "js/link_list.js",
				external_image_list_url : "js/image_list.js",
				media_external_list_url : "js/media_list.js",
				template_replace_values : {username : "Some User", staffid : "991234"}
			}
		);
	<? 
}
?>

</script>
<textarea id="<?=$sIdTextAria?>" class="<?=$sEditorID?>"  name="<?=$sNameTextAria?>" style="width:<?=$iTextariaWidth?>"><?=$sText?></textarea>
<? $this->IncludeComponentTemplate();?>

The file .parameters.php


<? if (!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true) die();

$arComponentParameters = array(
	"PARAMETERS" => array(
		"TYPE_EDITOR" => Array(
	         "PARENT" => "SETTINGS",
	         "NAME" => "Editor mode",
	         "TYPE" => "LIST",
	         "VALUES" => array('TYPE_1' => 'Simplified editor', 'TYPE_2' => 'Full editor'),
		),

		'INIT_ID' => array(
	         "PARENT" => "SETTINGS",
	         "NAME" => "Editor ID (unique)",
	         "TYPE" => "STRING",
	         "DEFAULT" => '',
		),
		
		'TEXT' => array(
	         "PARENT" => "SETTINGS",
	         "NAME" => "Content to be inserted to the editor",
	         "TYPE" => "STRING",
	         "DEFAULT" => $_POST['content'],
		),
		
		'TEXTARIA_NAME' => array(
	         "PARENT" => "SETTINGS",
	         "NAME" => "TEXTARIA field name",
	         "TYPE" => "STRING",
	         "DEFAULT" => 'content',
		),
		
		'TEXTARIA_ID' => array(
	         "PARENT" => "SETTINGS",
	         "NAME" => "TEXTARIA ID field",
	         "TYPE" => "STRING",
	         "DEFAULT" => 'content',
		),
		
		'TEXTARIA_WIDTH' => array(
	         "PARENT" => "SETTINGS",
	         "NAME" => "Editor width",
	         "TYPE" => "STRING",
	         "DEFAULT" => '100%',
		),

		'TEXTARIA_HEIGHT' => array(
	         "PARENT" => "SETTINGS",
	         "NAME" => "Editor height",
	         "TYPE" => "STRING",
	         "DEFAULT" => '300',
		),
	)
);
/*
				array(
					'TEXT' 			  => $_POST['content'], # contents, in html
					'TEXTARIA_NAME'   => 'content',   		# field name
					'TEXTARIA_ID'     => 'content',   		# field ID
					'INIT_ID'	      => 'textareas', 		# editor ID
					'TEXTARIA_WIDTH'  => '100%', 	  		
					'TEXTARIA_HEIGHT' => '300' 		 
				)
*/
?>

Several important points in the code of the component.php must be clarified. Connection of the editor proper:

<?  $APPLICATION->AddHeadScript($this->__path .’/tiny_mce/tiny_mce.js’); ?>

Connection of styles put separately in the component folder for convenience; this setting is made in js in the initialization code:

content_css : ‘<?=$this->__path?>/example.css’,
Attention! If there are two or more editors on a page we have to identify them by the class name editor_selector : ‘<?=$sEditorID?>’.

The text area proper for which all the work is being done:

<textarea id=’<?=$sIdTextAria?>’  name=’<?=$sNameTextAria?>’ style=’width:<?=$iTextariaWidth?>’><?=$sText?></textarea>

How to Use

We get connected as follows:

<? echo $_POST['content'] ?>
<? echo $_POST['content2'] ?>
<form action="" method="post" name="">
<? $APPLICATION->IncludeComponent("tools:editor.tiny.mce", ".default", array(
"TEXT" => $_POST["content"], // contents from the request which must be inserted
"TEXTARIA_NAME" => "content", // field name
"TEXTARIA_ID" => "content",         // field id
"TEXTARIA_WIDTH" => "100%",  // width
"TEXTARIA_HEIGHT" => "300",    // height
 
"INIT_ID" => "ID" // ID of the proper editor
),
false
);
?>
<input value="submit" name="sub" type="submit" />
</form>


Courses developed by Bitrix24