Jump to content
Robin S

Finding the fields that use a Textformatter module

Recommended Posts

A recent GitHub request got me thinking about ways to get an overview of which fields are using which Textformatter modules. Here are a couple of approaches...

1. For all Textformatter modules that are in use, show the fields they are applied to

Execute the following code in the Tracy Debugger console:

// Loop over fields and get their Textformatters
$textformatters = array();
foreach($fields as $field) {
    if(empty($field->textformatters)) continue;
    foreach($field->textformatters as $textformatter) {
        $textformatters[$textformatter][] = $field->name;
    }
}
d($textformatters);

2019-03-23_115809.thumb.png.a896f67542b3c8372d99e9ca0f0d2a69.png

Any Textformatter modules that are not included in the dump output are not applied to any fields.

 

2. In the config screen for a Textformatter module, show the fields where the module is applied

Add the following to /site/ready.php

$wire->addHookBefore('ProcessModule::executeEdit', function(HookEvent $event) {
	// Get the module name
	$module_name = $this->wire('input')->get->name('name');
	// Return if it's not a Textformatter module
	if(strpos($module_name, 'Textformatter') !== 0) return;
	// Add field to module edit form
	$event->wire()->addHookBefore('InputfieldForm(id=ModuleEditForm)::render', function(HookEvent $event) use ($module_name) {
		$value = '';
		// Find any fields using this Textformatter and build markup value
		foreach($this->wire('fields') as $field) {
			if(empty($field->textformatters)) continue;
			foreach($field->textformatters as $textformatter) {
				if($textformatter === $module_name) {
					$value .= "<a href='{$this->wire('config')->urls->admin}setup/field/edit?id={$field->id}#fieldtypeConfig' target='_blank'>$field->name</a><br>";
				}
			}
		}
		if(!$value) $value = 'No fields are using this Textformatter module';
		// Add markup field to form
		$form = $event->object;
		$f = $this->wire('modules')->InputfieldMarkup;
		$f->label = 'Fields using this Textformatter module';
		$f->value = $value;
		$form->insertAfter($f, $form->children->get('id=ModuleInfo'));
	});
});

2019-03-23_121517.png.90160a9f620312a12be5009622fc80e2.png

Edited by Robin S
Updated module config hook to avoid loading module
  • Like 8
  • Thanks 1

Share this post


Link to post
Share on other sites

Works very well. Extremely useful! Thank you!

  • Like 2

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...