Jump to content

Change module settings via API


nurkka
 Share

Recommended Posts

Hi,

the output of a CKEditor field is postprocessed by the MarkupHTMLPurifier module, which has no configuration settings in the backend. I want to tweak the html output, so I'm trying to change the MarkupHTMLPurifier settings. The easiest method would be, to change one line in MarkupHTMLPurifier.module, but I want to be save when updating ProcessWire. So I had the idea to change the module settings on runtime. To achieve that, I wrote following module:

class configModifer extends WireData implements Module {

	public static function getModuleInfo() {
		return array(
			'title' => 'Config Modifier Test',
			'version' => 1,
			'summary' => 'This module tries to change the settings of another module on runtime.',
			'singular' => true,
			'autoload' => true,
		);
	}
	
	public function init() {
		$this->addHookBefore('Inputfield::render', $this, 'beforeInputFieldRender');
	}
	
	public function beforeInputFieldRender (HookEvent $event) {
		$purifier = wire()->modules->get('MarkupHTMLPurifier');
		$purifier->set('HTML.Doctype', 'HTML 4.01 Transitional');
	}

}

Unfortunately this doesn't work. The Hook is called, but it doesn't change the settings of MarkupHTMLPurifier.

How can I change the module settings of MarkupHTMLPurifier on runtime? Or is there another way to achieve that?

Thanks in advance.
 

Link to comment
Share on other sites

You're right, it was the wrong hook. Now I'm using InputfieldCKEditor::processInput, but still don't get the right instance of MarkupHTMLPurifier.

Next, I took a closer look at the code of InputfieldCKEditor.module and I tried to replace the hook method:

public function ___processInput(WireInputData $input) {

	$value = trim($input[$this->name]);
	if($value == self::PLACEHOLDER_TEXT) return $this; // ignore value

	$value = $this->purifyValue($value);

	if($value != $this->attr('value')) {
		$this->trackChange('value');
		$this->setAttribute('value', $value);
	}

	return $this;
}

The MarkupHTMLPurifier module is instantiated in the $this->purifyValue()-Method:

if(is_null(self::$purifier)) self::$purifier = $this->wire('modules')->get('MarkupHTMLPurifier');

Is there any way to get self::$purifier from »outside«, i.e. from a hook method?

If not, is it possible to replace the whole processInput-Method and call a custom instance of MarkupHTMLPurifier? I tried to do that, but it didn't work:

public function hookMethod (HookEvent $event) {

	$this->message("Hook InputfieldCKEditor::processInput"); // the hook is called correctly

	$event->replace = true; // force replace the hook method

	$field = $event->object;
	$arguments = $event->arguments;

// now I tried to adapt the code of the original method, but it didn't work.

	$value = trim($event->arguments[$field->name]);
	if($value == self::PLACEHOLDER_TEXT) return $field;

	$value = $field->purifyValue($value);

	if($value != $field->attr('value')) {
		$field->trackChange('value');
		$field->setAttribute('value', $value);
	}

	return $field;
	
}

I think that must be possible, but how is it done correctly?

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...