Jump to content

using PageListSelect in module


Macrura
 Share

Recommended Posts

Hi -

i have a module i'm trying to improve so i added a page list select to the getModuleConfigInputfields function like this:

$fieldDocsRoot = wire('modules')->get('InputfieldPageListSelect');
$fieldDocsRoot->attr('name+id', 'docsRoot');
$fieldDocsRoot->label = __('Docs Root', __FILE__);
$fieldDocsRoot->description = __('Select the root page of your docs. (Allows the module to know where to create new docs.', __FILE__);
$fieldDocsRoot->attr('title', __('Docs root', __FILE__));

$wrapper->add($fieldDocsRoot);

i also tried to init the docRoot var up in __construct, but i can't figure out how to get it to save the page on that inputfield..

TIA

Link to comment
Share on other sites

This works for me

//create the config screen
$form = new InputfieldWrapper();

$p = 'selected';
$parentId = wire('pages')->get('/')->id;	

//pagelistselects
$parentAdd = wire('modules')->get('InputfieldPageListSelect');
$parentAdd->label = __('Parent Page');
$parentAdd->attr('name+id', $p);
$parentAdd->set('parent_id', $parentId);
$parentAdd->set('startLabel', __('Parent Page'));

$form->add($parentAdd);
Edited by kongondo
Link to comment
Share on other sites

Are you sure its not saving? Un-selection is different from not saved :-)....Can you confirm by looking into the module's data field in the database? As for selection of saved data, I think there is a property for setting that but I can't find it now. You have to tell the module what is selected (saved) I believe. Let me see if I can find the property...

Edited by kongondo
Link to comment
Share on other sites

I tested with Blog module settings and it works (the saving bit) but couldn't figure out how to tell it to show the label of what's saved...I had a look at how this is done in page fields but the rabbit hole was too deep so turned back :-)

Edited by kongondo
Link to comment
Share on other sites

Sorted!
 
Just add a value attribute :-)

$myModuleConfigs = wire('modules')->getModuleConfigData('YourModuleClassName');
$savedPageID= (int) $myModuleConfigs['selected'];// name of our input above (see my previous code => $p)
$parentAdd->attr('value', $savedPageID);// (int) Saved paged ID
#$parentAdd->value = $savedPageID;// alternative way to set

For completeness, here's the full working code
 

$form = new InputfieldWrapper();

$parentId = wire('pages')->get('/')->id;	

// pagelistselects
$parentAdd = wire('modules')->get('InputfieldPageListSelect');
$parentAdd->label = __('Parent Page');
$parentAdd->attr('name+id', 'selected');
$parentAdd->set('parent_id', $parentId);
$parentAdd->set('startLabel', __('Parent Page'));

$data = wire('modules')->getModuleConfigData('YourModuleClassName');
$savedPageID = (int) $data['selected'];// name of our input above
$parentAdd->attr('value', $savedPageID);// (int) Saved paged ID
#$parentAdd->value = $savedPageID;// alternative way to set

$form->add($parentAdd); 
Edited by kongondo
Added full working code for completeness
  • Like 4
Link to comment
Share on other sites

In terms of building this module (PageDocsTab), i wonder how to deal with it needing to work with a 3rd party fieldtype, namely FieldtypeTemplates, because the way i have it working is that you specify on the doc which templates to show that particular doc on, so that field needs to be added to the template that is being used for "Docs'.

I guess it could be in the instructions and also could be made a requirement that the fieldtype is installed before installing this one... and then once this module is installed it could create the required field, e.g. 'template_select'...

Another question - i've looked through a bunch of modules and i was under the impression that if you had css or js named the same as the module that it would load them, and in some modules i don't see any code to load css or js, even though there are css and js files in the module folder;

on the URL checker module, this code is used to get the css to load:

$this->config->styles->add($this->config->urls->siteModules . __CLASS__ . '/' . __CLASS__ . '.css?v=' . time());

so is this the recommended/correct way?

Link to comment
Share on other sites

The resource files with the same name as the module should be loaded automatically and even be cachebusted. But there might be rare edgecases, where you'd need to include them manually because processwire might load them to late or something. I'd only do that if the need arises.

  • Like 1
Link to comment
Share on other sites

It will only auto-load if in your module's init method you call parent::init(); then your code after that. The other conditions have to be true as well, i.e. the resources have to be in the same directory as the module and they have to be named as:

  • MyModuleName.module
  • MyModuleName.js
  • MyModuleName.css

What class is your module extending? I don't think the above works with all module types

Edited by kongondo
  • Like 1
Link to comment
Share on other sites

great - thanks for the clarification - i think in my case i should only load the assets if the hook is run (which hooks into the page edit and adds a tab), because those assets are not needed unless those conditions are met..

Link to comment
Share on other sites

What class is your module extending? I don't think the above works with all module types

I'm extending WireData; this hooks into ProcessPageEdit::buildForm and adds a tab for the found documentation based on the template;

but i realized that it shouldn't load the css/js by default, only when the hook runs, and i wasn't doing parent::init; i may need to rename the css if i need that parent init for some other reason.

but that brings up another question - i also want to have a process module bundled in with this to render the global documentation page;

right now i'm hacking it together by using admin custom pages and then calling this module's (render) method on that; i will look at some other modules that have submodules, so i guess it would install the 2nd module as a child and then i could still get the confiig data somehow (need it to find the root page to render, in the process module)..

Link to comment
Share on other sites

I doubt auto loading of resources will work with extending WireData

Many modules (including Blog, Padloper, etc) are actually several modules bundled together, each fulfilling their specific role. So, that's OK, if you want to go that route. The important thing is to indicate which module requires which one and which one installs which one (if you wish, i.e., otherwise user installs each on its own).

There's is no magic in respect of getting any module's config other than using what PW already provides:

$anyModuleConfigs = wire('modules')->getModuleConfigData('AnyModuleClassName');

You can call that from anywhere, including in template files. It returns an array. It will be empty if there is no config data.

https://processwire.com/talk/topic/648-storing-module-config-using-api/?p=5241

https://processwire.com/talk/topic/3343-how-can-i-setget-module-data-from-db-when-not-implementing-configurablemodule/?p=33087

Edited by kongondo
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...