Jump to content

Does using dedicated module files help a lazy programmer?


horst
 Share

Recommended Posts

Does using dedicated module files help a lazy programmer?

For a private helper module I wanted to built, I decided to use the possibilities of dedicated files (implemented in PW 2.5.5) and not to write all into a single module file. Until now, I totally missed or forgot about this possibility. To my excuse: on introduction I may have thought not to use it early in my modules to keep backward compatibility. And then totally forgot about it. :)

I allready use a tool for postprocessing in my sites. And I'm not familiar with all those popular node/grunt/gulp/npm/bower/and-what-else stuff out there. It looks to me a bit oversized for small to medium websites. But I also wanted not miss a comfortable, easy to use config for less and sass files, especially for those from frameworks like Bootstrap 3 / 4 or UIKit. So, what my toolbox should get added was a preprocessor (compiler) for sass (.scss) files first. I choosed the UIKit for the first shot.

The created files for a configurable module called PreAndPostProcessor and a process module called ProcessPreProcessor were:

  • PreAndPostProcessor.module
  • PreAndPostProcessor.info.json
  • PreAndPostProcessorConfig.php
     
  • ProcessPreProcessor.module
  • ProcessPreProcessor.info.json
  • ProcessPreProcessor.css
  • ProcessPreProcessor.js

As you can read about the differences to the old-schooled style in Ryans blog post, I focus on what I found out to be most useful for me. Starting with the Config file, I decided to use the $this->add() method in the __constructor(). It is the way what needs less code to define your inputfields for a configpage: just a collection of arrays with field or fieldset definitions.

Mandatory definitions are: type, name, label. Others are optional: description, notes, required, columnWidth, collapsed, showIf, ...
And, of course, you need the definitions specific to your fieldtypes (value, options, ...).

To group some fields into a fieldset, add a key called "children" to the fieldset array and add all field definitions to it as collection. Simple!

  Reveal hidden contents

 

Another nice thing I discovered, is the use of the *.info.json files. So, this I have used before, but not like here. :)

The behave is like with the *Config.php files, - you write really less code, just arrays with key - value pairs and PW does the rest for you! Only difference here is, that you have to write it in JSON notation.

The ProcessPreProcessor.info.json file contains the neccessary and common parts: title, version, requires. And some others: permission, permissions, page and nav.

Page and nav are specific to Process modules. With page {name, parent, title} you define where PW should create you a page in the admin, and with nav, you can define a complete submenu for this page.

For those who are yet not familiar with the internal structure of Processmodules: you can navigate to a submenu entry by calling it: {pw-admin-url}/{main-module-url}/{submenu-url}/.

Calling the main menu url invokes the ___execute() method of the module. Calling a submenu url invoke the ___executeSubmenu() method. This all is really simple and straight forward. And it is really really helpful in cases like with the new toy for my toolbox. :)

The nav is a collection of arrays, each holding at least an url and a label. Thats enough for PW to implement the menu and submenu for you in the admin. But you also may define an icon too. Aaaand, you also can add your own custom key / value pairs to it. PW does not complain on it.

I have added a description. The nav was generated by parsing all core distribution scss files from the UIKit. They have a name and description in their first two lines. I programatically read this out and generated this nav notation for the info.json file:

  Reveal hidden contents

The (main) ___execute() method of the ProcessPreProcessor.module should present a submenu list with file infos. This now could be done by parsing the info.json file:

    public function ___execute() {
        // generate a navigation
        $dump = json_decode(file_get_contents(dirname(__FILE__) . '/' . basename(__FILE__, '.module') . '.info.json'));
        $nav  = $dump->nav;
        $out  = "\t\t\t<dl class='nav hnpp'>\n";
        foreach($nav as $obj) {
            $out .= "\t\t\t\t<dt><a href='./{$obj->url}'>{$obj->label}</a> ></dt>\n";
            $out .= "\t\t\t\t<dd>{$obj->description}</dd>\n";
        }
        $out .= "\t\t\t</dl>\n";
        return $out;
    }

 

To make it even more friendly for lazy devs, :), I created one method that is called from all sub-execute methods by simply passing their own name over into this function that loads and parse the scss file and displays its config page:

  Reveal hidden contents

 

The first version work out nicely. Here is a screenshot of it:

 

uikit-configurator2.gif

 

... and to answer the initial question: Yes, it does!  :)

 

  • Like 17
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...