Jump to content
iipa

Change field "Only visible if..." through API

Recommended Posts

Hi!

I have two Page Reference fields: Category and Subcategory. Category is parent of Subcategory, and can have 0 to n Subcategories. Fields are selected by dropdown selection.

I'm trying to achieve following logic:

1. Select Category (obviously works)

2. Change Subcategory options based on selected Category (this works)

3. Hide the Subcategory field if Category has no Subcategories (to prevent weird dropdown with nothing to select)

I have tried to investigate two possible alternatives to achieve step 3:

A) Make dynamic condition into Subcategory selection's "Only visible if..." field

B) Make a hook that fires when Category changes, then hides Subcategory field if Category has no children

So far I haven't been successful in either. Conditions I've tried always lead to hidden field, and I haven't been able to find set up a hook that fires on field change.

So here's the question (finally): Is there a way to alter field visibility in API, for example in ready.php or inside a hook? In API I could loop through categories, find the ones that have children and then make a selector based on their IDs.

Share this post


Link to post
Share on other sites
9 hours ago, iipa said:

Is there a way to alter field visibility in API, for example in ready.php or inside a hook?

Here's an example...

The page structure:

2019-05-30_101406.png.94c9de895a96009038bf2485e47a512e.png

Some pet species have breed child pages and some do not.

The selector string for selectable pages for the breed field is "parent=page.species".

In ready.php:

$wire->addHookAfter('ProcessPageEdit::buildForm', function(HookEvent $event) {
	/* @var InputfieldForm $form */
	$form = $event->return;
	$breed_field = $form->getChildByName('breed');
	if(!$breed_field) return;
	$species_with_breeds = $event->wire('pages')->find('parent=/selects/pets/, children.count>0');
	$breed_field->showIf = 'species=' . $species_with_breeds->implode('|', 'id');
});

selects.gif.4ef74f092352650d806db8db39b15288.gif

  • Like 3

Share this post


Link to post
Share on other sites

Thank you @Robin S, that is exactly what I need! Referring to a field through form in hooks is something I haven't come across yet. It's always exciting to find new things to explore!

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

  • Similar Content

    • By LuisM
      Hi there,
      while developing a sideproject which is completly build with ProcessModules i suddenly had the urge to measure the performance of some modules 😉 as a result, say welcome to the FlowtiAppPerformance module. 
      It comes bundled with a small helper module called FlowtiModuleProfiler. In the first release, even though you could select other modules, it will track the execution of selected Site/ProcessModules. 
      This will give you the ability to gain insights how your Application behaves. 
      The Main Module itself will come with 2 Logging Options, Database or PW Logs. Select Database for Charts and Logs...well If you just want your profiles as a simple log file in PW. 
      You also could choose to dump the request profile into TracyDebugger as shown here:

      Dont wonder about my avg_sysload, somehow my laptop cant handle multiple VMs that good 😄
      Settings Screen

      Monitoring

      FlowtiLogs

      again, dont look at the sysload 😄
      I will update the Module in the future to give some filter options and aggregation, but for now it satisfies my needs. 
      I hope it is helpfull for some. 
      Module is submited to the directory and hosted at github
      https://github.com/Luis85/FlowtiAppPerformance
      Any suggestions, wishes etc. are much appreciated. 
       
      Cheers,
      Luis
    • By Anders
      I want to allow full text search on my site. There is a very nice solution that comes right out of the box:
      $selector = "title|body~=$q, limit=50"; This works, but to make it even better I would want to give higher weight to pages where the search term occurs in the title, than if it just occurs in the body. After all, a page with the title "Wine from France" is probably the best match for the search "france wine". How do I accomplish this in ProcessWire?
      I can see three possible paths, but I am not very fond of any of them:
      Do a direct SQL query, circumventing the API, along these lines. But I would prefer to abstract away the database layout if at all possible. Use something like ElasticSearch, but to be honest that would be to complicated to set up and maintain in the long run. Make multiple lookups, first for matches in the title, then for matches in the body, and merge and sort in PHP. My suspicion is that this would get complicated quite quickly. For instance, how do you deal with a page that has two of the three search terms in the title and the third in the body? Is there a magic option four I should look into? Or are any of the above options better than the others? Any input is welcome!
    • By Clément Lambelet
      Hey!
      I've been working on a Processwire installation (3.0.123) for a few days now and I must have made a big mistake this morning because the links in the admin's main menu no longer appear.
      This is not related to the admin theme, because the bug occurs with all themes (Default, Reno, Uikit).
      I tried to reinstall with the dev version (3.0.136), but the problem is still there. I also uninstalled all the modules I had added, without success. There is no error in the js console. 
      I still can access/view/edit the pages  by going through the admin/page list.
      Thanks in advance for your help!


    • By schwarzdesign
      I'm working on a news feed that will show the most recent news in a full teaser grid, and all older news as a simpler archive-type list view. My selectors so far:
      $news_full = $page->children("template=news, limit={$page->feed_count_full}"); $news_archive = $page->children("template=news, start={$page->feed_count_full}, limit=9999"); The $page->feed_count_full field controls how many items to show in the teaser grid (I've confirmed it contains the correct value, and the $news_full selector works as intended).
      This works, but I don't like the limit in the second selector. Unfortunately, if I leave it out (i.e. I only specify a start, not a limit), the start is ignored and I get all news instead. Not a big problem as we will never have more than 9999 news, but it still bothers me, as semantically speaking I don't want to set a limit in this case.
      Is this the intended behaviour of start/limit selectors? Is there a cleaner way to specify an offset (start selector) without a limit?
      ProcessWire Version 3.0.123
      Thanks!
    • By pwFoo
      I played with the Selectors object and would like to use it for a special use case...
      That is a custom Selectors object with dummy data converted to an php array to see the structure (Selectors object is a WireArray with "fields" added)
      Array ( [0] => Array ( [0] => Array ( [field] => seg1 [value] => val1 [not] => [group] => [quote] => [forceMatch] => ) [1] => Array ( [field] => seg2 [value] => val2 [not] => [group] => [quote] => [forceMatch] => ) ) )  
      But instead of "seg1" the field name is "field" with value "seg1" (= my field name).
       
      So I can't search the Selectors WireArray (= custom WireArray with added Selectors objects) with PW "find('seg1=val1')", Is there a way to search with "find()" or build a simple wrapper to make the elements searchable / filterable with find()?
×
×
  • Create New...