📜 ⬆️ ⬇️

Creating modules for MODX Evolution in 2017 for the smallest

What are modules


MODX EVO provides the following types of resources: templates, chunks, snippets, plug-ins and modules. If you work in the system, then you know exactly what the first three are responsible for, but it is possible that you have never personally worked with the latter. Plug-ins are MODX-integrated code that allows you to change the mechanics of interaction with the admin interface. Using plug-ins, you can change the way data is displayed to edit any resources and specify what and at what stages of the interaction you should perform. Simply put with the help of plugins you hang your handlers.

But in this article I want to tell a little about something else, about modules. I am writing this article for the reason that there is very little information on this topic on the Internet, I did not find one in the Russian segment at all.
A module is an application based on the MODX architecture and extends the capabilities of the control system. The module can group a set of elements (snippets, chunks, data), implementing the principle of encapsulation through the separation of the interface and implementation.

More specifically, with the help of modules you can create add-ons with their own interfaces, for example, for mass editing documents or for managing an online store, which will be convenient for the user.

Creating a new module


Creating a module is pretty obvious. Just go to the modules-> module management tab and in the upper right-hand corner press the new module button, then fill in all the required fields and the save button, with this I am sure no one can have any problems. After you have saved the module, it will appear in the modules tab.

image
')

Module code


Creating modules is great, but sometimes we have a strange desire to have this module do something ... For all that the module does, the module's code field is responsible, it simply writes the php code that will be executed as soon as you enter your module. Please note here you cannot use the opening and closing <? Php?> Tag, so if you want to output some html code, be ready to write to output it through the standard echo stream or reqiure to a file in which this code will be more convenient the form. I certainly recommend to stay on the removal of the code in separate files. Traditionally, module files are located in the "/ assets / modules / {module name}" directory, so create a corresponding folder in this directory and refer to the module code.

//         modx  MODX_BASE_PATH require_once(MODX_BASE_PATH.'assets/modules/newmodule/index.php'); 

All now this code will be executed when you start your module.

Now let's try to do something useful, for example, we will display in one place all the documents with a specific template, this is convenient, for example, in cases when you have a lot of products on your site, and they are scattered in categories. To do this, we can use the functions of modx itself or, for example, through the $ modx-> runSnippet method, get all documents through all evolutionists, the favorite Ditto, in short, complete freedom of action. But we will first stop on something very simple and just get these documents through a wrapper for working with the base given $ modx-> db.

 //   ,             $docs = $modx->db->query("SELECT id, pagetitle FROM modx_site_content WHERE template='8'")->fetch_all(); foreach($docs as $doc){ $docId = $doc[0]; $docTitle = $doc[1]; echo "<p><a href='index.php?a=3&id=$docId'>$docTitle</a></p>"; } 

Now if we go to the module we will see not the most beautiful, but still links to all documents with id = 8.

image

But how can we be if we do not want to crawl into the code and write the template id there?

Configurations in MODX


For setting variables for our application, there are so-called configurations in plug-ins and modules, in them you can specify as many variables as you like with different types of input that will be available as defined variables in the code. Configurations are configured in the properties tab as a JSON array, and then after saving, they are automatically converted into a beautiful label on the configuration page. Here is an example of how to populate the property tab.

 { "card_template": [ { "label": " ", "type": "string", "default": "", "desc": "id   " } ], "price_tv": [ { "label": "", "type": "string", "default": "", "desc": "id tv    " } ] } 

There should be no questions here either. After that we can rebuild our code so that the template id is taken from this variable named $ card_template .

How to make beautiful


You probably want your application besides doing something else and fit into the overall style of the admin panel? Then you will have to suffer a little, because the digging of the modx code does not give much pleasure, so below I will insert the code that you can use as a template for filling your modules.

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>AndyShop</title> <link rel="stylesheet" type="text/css" href="media/style/MODxRE2/style.css"> <link rel="stylesheet" href="media/style/common/font-awesome/css/font-awesome.min.css"> <script type="text/javascript" src="media/script/tabpane.js"></script> </head> <body> <h1 class="pagetitle"> <span class="pagetitle-icon"> <i class="fa fa-shopping-bag"></i> </span> <span class="pagetitle-text">   </span> </h1> <div class="sectionBody" id="andyShopPane"> <div class="tab-pane"> <script type="text/javascript"> tpResources = new WebFXTabPane( document.getElementById( "andyShopPane" ), true); </script> </div> <div class="tab-page" id="Products"> <h2 class="tab"><i class="fa fa-newspaper-o"></i></h2> <script type="text/javascript">tpResources.addTabPage( document.getElementById( "Products" ) );</script> <!--    --> </div> <div class="tab-page" id="Orders"> <h2 class="tab"><i class="fa fa-shopping-bag"></i></h2> <script type="text/javascript">tpResources.addTabPage( document.getElementById( "Orders" ) );</script> <!--    --> </div> </div> </body> </html> 

image

Well, now we have quite a concise page that can be shown to the client.

Why MODX EVO


Now in a decent society, it is customary to strongly defy the evolutionary branch of MODX development. I disagree with this opinion and believe that even with such a powerful tool as MODX Revolution, his older brother still has the right to exist. This is largely due to the fact that, in my opinion, EVO is more suitable for developing corporate websites and changeable landing pages.

First, due to its more obvious syntax and the absence of many interface-overloading tools. Secondly, due to the fact that with the release of version 1.2 from December 2016, it began to be delivered with a pleasant theme for the user. Well, in the third admin panel Evolution feels really faster Revo. As for the community and a wide range of ready-made solutions, the young fighter certainly wins here.

image

Conclusion


There are a great many useful modules for MODX Evolution, such as shopkeeper or Doc Manager, but most of them either require significant refinement or are not at all suitable for solving your task, I hope this article will clarify the process of working with modules a bit. Some kind of note was not enough for me when I first started working with this CMF. If you have any questions, I will be happy to answer them.

Source: https://habr.com/ru/post/336182/


All Articles