Jump to content
Robin S

Finding the fields that use a Textformatter module

Recommended Posts

Posted (edited)

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...