Jump to content

Custom Inputfield Dependencies


Robin S

Recommended Posts

Custom Inputfield Dependencies

A module for ProcessWire CMS/CMF. Extends inputfield dependencies so that inputfield visibility or required status may be determined at runtime by selector or custom PHP code.

Overview

Custom Inputfield Dependencies adds several new settings options to the "Input" tab of "Edit Field". These are described below.

Note that the visibility or required status of fields determined by the module is calculated once at the time Page Edit loads. If your dependency settings refer to fields in the page being edited then changes will not be recalculated until the page is saved and Page Edit reloaded.

Usage

Install the Custom Inputfield Dependencies module.

Optional: for nice code highlighting of custom PHP install InputfieldAceExtended v1.2.0 or newer (currently available on the 'dev' branch of the GitHub repo).

The custom inputfield dependencies are set on the "Input" tab of "Edit Field".

'Visibility' settings

'Required' settings

Visibility

Show only if page is matched by custom find

Use InputfieldSelector to create a $pages->find() query. If the edited page is matched by the selector then the field is shown.

Custom find settings

Show only if page is matched by selector

As above, but the selector string may be entered manually.

Selector string settings

Show only if custom PHP returns true

Enter custom PHP/API code – if the statement returns boolean true then the field is shown. $page and $pages are available as local variables – other API variables may be accessed with $this, e.g. $this->config

Custom PHP settings

In most cases $page refers to the page being edited, but note that if the field is inside a repeater then $page will be the repeater page. As there could conceivably be cases where you want to use the repeater page in your custom PHP the module does not forcibly set $page to be the edited page. Instead, a helper function getEditedPage($page) is available if you want to get the edited page regardless of if the field in inside a repeater or not.

$edited_page = $this->getEditedPage($page);

Required

The settings inputfields are the same as for Visibility above, but are used to determine if the field has 'required' status on the page being edited.

 

https://github.com/Toutouwai/CustomInputfieldDependencies

http://modules.processwire.com/modules/custom-inputfield-dependencies/

  • Like 17
  • Thanks 3
Link to comment
Share on other sites

v0.0.2 released - a complete overhaul of the module. Custom Inputfield Dependencies now does the same thing but in a completely different way (thanks to a great idea from @adrian) :)

The module no longer requires Hanna Code and inputfield dependencies are now set on the "Input" tab of "Edit Field". See the first post for all the details.

If you are using v0.0.1 of the module then this is a breaking change and you will need to redefine your dependencies (sorry :(). But the new approach taken in v0.0.2 is so much better that hopefully you'll find the change worthwhile.

  • Like 8
Link to comment
Share on other sites

  • 2 months later...

Hey @Robin S - I just wanted to remind everyone how awesome this is :)

It can reduce the number of required templates (and template files) substantially in some cases where you have a standard setup, but with some minor differences to the fields in each template. Of course you could take it to extremes and have just one template per site, so you need to be careful to find the right balance, but it's a really powerful addition to PW IMO.

Thanks again!

  • Like 5
Link to comment
Share on other sites

  • 1 month later...

Thanks for the module.

I would like to report that It seems that this module is breaking the inputfieldwrapper field. When I edit the inputfieldwrapper field, it shows error as following.

Error: Uncaught TypeError: Argument 2 passed to ProcessWire\InputfieldWrapper::insertAfter() must be an instance of ProcessWire\Inputfield, null given, called in /var/app/current/site/modules/CustomInputfieldDependencies/CustomInputfieldDependencies.module on line 136 and defined in /var/app/current/wire/core/InputfieldWrapper.php:296
Stack trace:
#0 .../site/modules/CustomInputfieldDependencies/CustomInputfieldDependencies.module(136): ProcessWire\InputfieldWrapper->insertAfter(Object(ProcessWire\InputfieldTextarea), NULL)
#1.../wire/core/WireHooks.php(782): CustomInputfieldDependencies->addFieldOptions(Object(ProcessWire\HookEvent))
#2 .../wire/core/Wire.php(442): ProcessWire\WireHooks->runHooks(Object(ProcessWire\Field), 'getConfigInputf...', Array)
#3 .../wire/modules/Process/ProcessField/ProcessField.module(943): ProcessWire\Wire->__call('getConfigInputf...', Array)
#4 .../wire/core/Wi (line 296 of /var/app/current/wire/core/InputfieldWrapper.php) 

  • Like 1
Link to comment
Share on other sites

33 minutes ago, Karl_T said:

I would like to report that It seems that this module is breaking the inputfieldwrapper field. When I edit the inputfieldwrapper field, it shows error as following.

Could you explain some more about what you are doing at the time you see this error? I'm not sure what you mean by 'edit the inputfieldwrapper field'. Are you editing a field's settings in admin or via the API?

Link to comment
Share on other sites

On 2017-5-4 at 1:52 PM, Robin S said:

Could you explain some more about what you are doing at the time you see this error? I'm not sure what you mean by 'edit the inputfieldwrapper field'. Are you editing a field's settings in admin or via the API?

I was editing a field setting in the admin panel. This happens also when redirecting to the edit page after you create new inputfieldwrapper field.

Link to comment
Share on other sites

1 hour ago, Karl_T said:

I was editing a field setting in the admin panel. This happens also when redirecting to the edit page after you create new inputfieldwrapper field.

I think I get it now - you are talking about a "Fieldset in Tab" fieldtype, right? An InputfieldWrapper is something different.

Thanks for the report - should be fixed in v0.0.3.

  • Like 2
Link to comment
Share on other sites

  • 3 weeks later...

Hello @Robin S

the problem is still there in version 0.0.3.

Argument 2 passed to ProcessWire\InputfieldWrapper::insertAfter() must be an instance of ProcessWire\Inputfield, null given, called in /home/.sites/24/site1275/web/site/assets/cache/FileCompiler/site/modules/CustomInputfieldDependencies/CustomInputfieldDependencies.module on line 156 and defined

Best regards

  • Like 1
Link to comment
Share on other sites

  • 4 weeks later...
  • 3 months later...

Module works amazing. In my case, I needed to show the fields only if the page does not have a parent of a certain template (as the child pages simply display the parents fields on the webpage), selector:

parent.template!=complete-guide

...and the out the box options only allows for fields on the same page. Thanks @Robin S

  • Like 2
Link to comment
Share on other sites

Hi

Still another wonderful module I just discovered! I was wondering why fieldsetTab doesn't have visibility rule? (also not present in the core) I have to apply the same rule to all fields contained in the tab? Does the tab will disappear if empty (sorry didn't try yet I have ~15 fields in it for now)?

Thanks

Mel

Link to comment
Share on other sites

2 hours ago, mel47 said:

I was wondering why fieldsetTab doesn't have visibility rule? (also not present in the core)

FieldsetTab is not supported by the core "show if" dependency, but it turns out it is possible to support it in this module. Please update to v0.0.5.

  • Like 3
Link to comment
Share on other sites

  • 4 weeks later...

@Robin S Thanks for this module!

I was hoping to use this for a setup where some fields need to show/hide based on their parent, but using it caused the editor page load to go from ~1883ms to ~18056ms, using 4 custom dependencies; not sure if it is totally this module or some interaction with other modules, but the speed changed back once i removed all of the custom dependencies...

ProcessWire: 3.0.82
PHP: 7.1.11
Webserver: Apache
MySQL: 5.6.37
CustomInputfieldDependencies: 0.0.5

  • Like 1
Link to comment
Share on other sites

these are the 4 fields and the custom selector:

internal_notes
parent.id=6587

secure_files
parent.id!=6587

document_status
parent.id=6587

pr_select
parent.id=6485

now i'm thinking it would be more efficient anyway to maybe create a cache field of the parent id, and just use regular inputfield dependencies?

also, these pages have thousands of siblings, not sure if that matters...

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
  • Recently Browsing   0 members

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