Jump to content

Create InputfieldPageListSelectMultiple inside module


dragan
 Share

Recommended Posts

I am trying to create an input field type InputfieldPageListSelectMultiple in a module, but for some reason, it doesn't quite work as expected...

(abbreviated) code:

$this->addHookAfter('ProcessPageLister::renderResults', function ($event) {
	$out = '';

	if(wire('page')->id === 1224) { // the particular Lister instance page id

		$form = wire('modules')->get("InputfieldForm");
		$field = wire('modules')->get("InputfieldPageListSelectMultiple");
		$field->attr('name+id', 'current_selection');
		$field->attr('class', 'InputfieldAsmSelect');
		$field->label = 'Your current selection';
		$field->derefAsPage = FieldtypePage::derefAsPageOrNullPage;

		// this part works - $total is taken from a session variable
		if($total) {
			$sel = $total["selector"];
			$myPages = wire('pages')->findIDs($sel);
			foreach($myPages as $k=>$v) {
				$field->attr('value', $v, wire('pages')->get($v)->title);
			}
		}

		$form->append($field);
		$myForm = $form->render();
		$event->return = $myForm;
	}
});

What I get is this:

non-working-multipleselect.PNG.def8923087c0950696c6eb44a991e8b4.PNG

I can't drag and drop, the layout is not correct (trashcan should be on the right), and I also can't delete. Furthermore, I see a textinput with the page IDs underneath. Why?

Oh, and also: the first item is "Label" o_O

I've googled, and looked at Ryan's code, but I guess I'm missing important stuff.

(if you're wondering: I'm trying to do a little proof of concept that would (partly) solve this question here)

 

Link to comment
Share on other sites

It looks like the JS and CSS dependencies for InputfieldPageListSelectMultiple are not included in the page. Probably because ProcessPageLister::renderResults is called via AJAX after the page has already loaded, at which time it is too late to tell PW that the dependencies are needed in the <head>.

You could try forcing the dependencies to be loaded in a hook to ProcessPageLister::execute...

$wire->addHookAfter('ProcessPageLister::execute', function(HookEvent $event) {
    if($this->config->ajax) return; // not needed for AJAX-loads
    $inputfield = $this->modules->get("InputfieldPageListSelectMultiple");
    $inputfield->renderReady(); // load JS/CSS dependencies
});

 

  • Like 2
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...