In the network you can easily find a manual for creating modules with custom components, but other typical situations are not considered. In the meantime, this could help the development of developers and improve the overall code level under 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 the editing of information block elements on Saturday, Sunday and afternoon.
Solution technique1. We will use handlers to catch and block an element edit event.
2. We will wrap these handlers into a module so that our functionality can be used on any site.
This approach will be useful when1. When you save an information block 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:
- posted a web form - changed something in the information blocks
- changed section - it is necessary to change something in its elements or other sections, etc.)
- changed the element in the information block - it is necessary to create an agent who will send letters at a 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 upload them to the Marketplace.
')
Comic module "Russian Post"
Step 1: write the handler
At this step, everything is banal simply and accurately documented.
Using the OnBeforeIBlockElementAdd
Handlerand
close to it, we create a function that blocks editing at a certain time
function lock($arParams) { if (in_array(date('w'),array(0,6) || date('H')>18 || date('H')<9) { global $APPLICATION; $APPLICATION->throwException(" , !"); return false; } }
We'll take out the inscription in the module settings 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, select the settings in it.
Of course, below we will look at how to make the admin panel, which allows editing module settings. I would also like to warn that the COption class can store only two types of settings - integer and string. Unfortunately, support for arrays does not exist and, if necessary, it will have to be implemented in a roundabout way, for example, using a table.
Step 2: create a “blank” module.
Create the russianpostjoke folder in the bitrix / modules folder. This will be our module folder.
About what files and why you need to create a folder can be found in the
official documentation .
In order not to litter the article with listings, I suggest studying the source code on the githaba:
https://github.com/may-cat/bitrix-dull-modulePossible mistakes:If you create a module and there is a dot in the name, you may not see it in the list of modules in the admin panel. Bitrix converts the dot to an underline in the name of the class and functions. Carefully study the Bitrix source code and / or other modules if you are going to use the dot.
So, by replacing the module name with russianpostjoke in the above “blank” wherever it is needed, we received our blank.
Let's move on to filling it with the necessary functionality.
Step 3: fill the module with functionality
Let's create the cBlocker class and place it in the russianpostjoke / classes / general / cMainRPJ.php folder - we will implement our handler as a method there.
Memo:
Try to place the classes used in your module in the / classes / folder, following the standards specified by the standard Bitrix modules.
Now, we need to register the installation of the module. Use the file
russianpostjoke / install / index.php in which there are DoInstall () and DoUninstall () methods.
In this case, in our case, it is necessary to fulfill three conditions:
a) Use the
RegisterModuleDependences function to install our handler from the cBlocker class into the system, associating it with the regular OnBeforeIBlockElementAdd and the like.
b) Inform the system that the module is installed. Help us
RegisterModule($this->MODULE_ID);
c) Display a message for the user:
$APPLICATION->IncludeAdminFile(" russianpostjoke", $DOCUMENT_ROOT."/bitrix/modules/russianpostjoke/install/step.php");
Please note that this call should be made last, right before the return construct.
As a result, you should see something like this in your admin panel:

The appeared module can be installed and immediately after installation our declared functionality will work.
Step 4: admin panel
If you now go into the Bitrix administration panel on the Settings / Product Settings / Module Settings /% page. The title of our module% can be seen ... nothing. Meanwhile, it is on this page that the settings of our module should be located.
The page we are interested in is set by the russianpostjoke / options.php file, and good and bad news are connected with it.
The bad news is that all settings, including saving settings, at this stage of Bitrix development, 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 to pay attention to how the settings of the “Performance Monitor” (perfmon) are described - they are quite simple to understand.
We need to create settings for a single WE_ARE_CLOSED_TEXT field, which we used in step # 1.
The full source of the module is posted on github:
https://github.com/may-cat/bitrix-russianpostjokeInstead of conclusion
We considered the simplest module, there is something to tell at least 3 articles. If you,%% username, have found this material useful, or you, like me, want to improve the developer level under 1C-Bitrix, support the article. Thank.
UpdateFor the new core, the D7 has made a new “Procurement” module.
You can download and dig out with it on the githaba:
github.com/may-cat/maycat.d7dullPull requests are welcome.