How to make 1C-Bitrix module

On the network, you can easily find the manual for creating modules with custom components, but other typical situations are not considered. Meanwhile, this could help the development of developers and improve the overall level of code for Bitrix.

You can argue a lot about Bitrix itself, but it continues to exist, and developers need knowledge. I will describe the creation of modules on the example of the comic module “Russian Post”. It will prohibit editing of information block elements on Saturday, Sunday and at lunch.

Solution Method
1. We will use handlers to catch and block the event of editing an element.
2. We will wrap these handlers in a module so that our functionality can be used on any site.

This approach will be useful when
1. When saving an infoblock element, you need to specifically check the data (and this is repeated from time to time, on different projects)
2. When saving or changing one entity, you need to use another, for example:
- post a web form - change something in the infoblocks
- changed the section - you need to change something in its elements or other sections, etc.)
- changed the element in the info block - you need to create an agent that will send letters at the specified time.
etc.
3. Upon reaching an event, you need to throw the event into the statistics module.
4. You want to learn how to make modules for 1C-Bitrix and put them on Marketplace.

Comic module "Russian Post"


Step 1: writing a handler


At this step, everything is simple and well-documented to the point of banality.
Using the OnBeforeIBlockElementAdd handler
and those close to it, we create a function that blocks editing at certain times

function lock($arParams)
{
if (in_array(date(‘w’),array(0,6) || date(‘H’)>18 || date(‘H’)<9)
	{
	global $APPLICATION;
	$APPLICATION->throwException("Куда Вы прёте, у нас закрыто!");
	return false;
	}
}


We will take out the inscription in the module settings in order to learn how to do it.
So, for storing module settings there is a COption class . Add this to our function:
global $APPLICATION;
$APPLICATION->throwException(COption::GetOptionString("russianpostjoke", "WE_ARE_CLOSED_TEXT", "У нас закрыто!"));
return false;


Memo:
After you write the code that you are going to wrap in the component, highlight the settings in it.


Of course, below we will look at how to make an admin panel that allows you to edit module settings. I would also like to warn that the COption class can store only two types of settings - integer and string. Unfortunately, there is no support for arrays and, if necessary, it will have to be implemented in roundabout ways, for example, using a table.

Step 2: create a “blank” of the module.


Create a russianpostjoke folder in the bitrix / modules folder. This will be the folder of our module.
You can read about what files and why you need to create in a folder in the official documentation .
In order not to clutter up the article with listings, I propose to study the sources on the github: https://github.com/may-cat/bitrix-dull-module

Possible errors:
If you create a module and there will be a dot in the title, maybe you will not see it in the list modules in the admin panel. Bitrix converts a dot to an underscore in the name of a class and functions. Carefully study the sources of Bitrix and / or other modules if you intend to use the point.

So, replacing the name of the module in russianpostjoke in the above “blank” wherever it is needed, we got our blank.
Let's move on to filling it with the necessary functionality.

Step 3: we fill the module with functionality


Let's create the cBlocker class and place it in the russianpostjoke / classes / general / cMainRPJ.php folder - there we will implement our handler as a method.

Memo:
Try to place the classes used in your module in the / classes / folder, following the standards set by the standard Bitrix modules.


Now, we need to register the installation of the module. We will use the
russianpostjoke / install / index.php file in which there are DoInstall () and DoUninstall () methods.
Moreover, in our case, three conditions must be met:
a) Use the RegisterModuleDependences function to install our handler from the cBlocker class into the system, binding it to the standard OnBeforeIBlockElementAdd and the like.
b) Inform the system that the module is installed. It will help us RegisterModule($this->MODULE_ID);
c) Call the message output for the user:
$APPLICATION->IncludeAdminFile("Установка модуля russianpostjoke", $DOCUMENT_ROOT."/bitrix/modules/russianpostjoke/install/step.php");

Note that this call must be made last, right before the return construct.

As a result, you should see something similar in your admin panel: The

appeared module can be installed and immediately after installation our declared functionality will work.

Step 4: admin


If now go to the Settings / Product settings / Module settings /% page in the Bitrix admin panel and see the title of our module%, you can see ... nothing. Meanwhile, on this page should be the settings of our module.
The page we are interested in is set by the russianpostjoke / options.php file, and the good and bad news are associated with this.
The bad news is that all settings, including saving settings, at this stage in the development of Bitrix, are set by this file, often in the form of a “sheet”.
The good news is that a lot has already been done before you, and you can take advantage of existing developments. I recommend paying attention to how the settings of the “Performance Monitor” (perfmon) are described - they are quite simple to understand.
We need to create the settings for a single field WE_ARE_CLOSED_TEXT, which we used in step No. 1.
The full source code of the module is posted on the github: https://github.com/may-cat/bitrix-russianpostjoke

Instead of a conclusion


We examined the simplest module, there is something to tell about at least 3 more articles. If you,% username%, found this material useful, or you, like me, want to improve the level of developers for 1C-Bitrix, support the article. Thanks.

Update
For the new kernel, D7 made a new "Harvesting" for the module.
You can download and dig into it on the github: github.com/may-cat/maycat.d7dull
Pull requests are welcome.

Also popular now: