Jump to content

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


iipa
 Share

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.

Link to comment
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 4
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.
  • Similar Content

    • By Mika
      Hello, can I use the selector to extract only pages with page titles longer than 10 characters?
      I'm looking for a way to search directly without using the php foreach statement, as shown below.
              $result = $this->pages->find("template!=admin, has_parent!=2, include=all");
              foreach ($result as $page) {
                  if (strlen($page->title) > 10) {
                      $this->table[] = $page;
                  }
              }
       
    • By Guy Incognito
      I'm trying to build a search selector that includes a checkbox multiple option field (clinicdetails_specialisms). The whole selector works fine if I don't include the checkbox field. With it included I get errors like "Error: Exception: SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters (in wire/core/PageFinder.php line 627)".
      This is the selector as it currently stands.
      subscription_status=active,(template=user,clinicdetails_description|clinicdetails_short_description|clinicdetails_clinic_name|clinicdetails_first_name|clinicdetails_last_name*=$query),(clinicdetails_specialisms.title=$query) I've also tried setting the specialisms field in one string rather than groups and have also tried with/without the '.title' property.
      Oddly the selector works if specialisms is the only field being searched. It seems to fail when mixed with other selectors.
      Can anyone advise what stupid mistake I'm making!
    • By Ksenia
      Hello! 
      I have a weird situation going on with my selector field, which I can't really get. 
      I create an array of titles as filter using this logic:
      firstname=Mike|Steve id=123|124|125 title*=Red|Blue|Green This is the field I am logging: 

      It goes in a for loop till it looks like what you see on the screen.
      So, my code regarding the selector string looks like this:
      $output = ""; foreach ($allorganisations as $item){ $output .= "$item | "; echo "<div style='color:red;'>//output String://</div>"; echo $output; } $selector_org .= ", title=$output"; I also log the output just after the matched pages are selected to be sure:
      $matches = $pages->find($selector_org); echo $output; foreach ($matches as $match) { echo " <li><a href='$match->url'>$match->title</a></li>"; } But you can see that it doesn't select all four needed pages, only selects one of them.
      Yet when I copy this exact selector text and manually put it in my selector, it works great... I even copy extra space and "|" and all that, it is supposed to be identical. I am very confused, what is the difference between the code? 
      foreach ($allorganisations as $item){ $output .= "Institute for Scientific Research in Cosmic Anthropoecology | Institute for Scientific Research in Cosmic Anthropoecology | Stanford Research Institute | Institute for Scientific Research in Cosmic Anthropoecology | Stanford Research Institute | Institute of Clinical and Experimental Medicine | Institute for Scientific Research in Cosmic Anthropoecology | Stanford Research Institute | Institute of Clinical and Experimental Medicine | Cosmists | "; echo "<div style='color:red;'>//output String://</div>"; echo $output; } $selector_org .= ", title=$output"; } ------>
      If you see where I'm going wrong, please enlighten me. I am very puzzled with this behaviour . :--)
      Best,
      Ksenia
    • By fruid
      I'm having and always have a hard time building PaginatedArrays, I never know where to put the "limit=24" selector so please enlighten me.
      Here's what I'm doing…
      $categories = $page->protable('start=0, limit=999999'); // this I need in order to retrieve the pages's "categories" and I don't know how to make use of $rows instead for that purpose because of this confusing limit-API $rows = $page->protable; // put ("limit=20") here? $custom = buildSelector($input, $rows); // this function returns an array of selectors depending on the user input $items = new PaginatedArray; // or here? // or here? $items->find("limit=20") // or like this? $items("limit=20") // or like this? $items = $items("limit=20") // or like this? $items = $items->find("limit=20") foreach ($rows as $r) : if ($custom->get('selector')->matches($r)) : $items->add($r); endif; endforeach; // or at this point? and then
      if ($items) { // or maybe somewhere here?     echo '<span class="grey">'.$items->getPaginationString(array(     'label' => 'entries',     'zeroLabel' => '0 entries', // 3.0.127+ only     'usePageNum' => false,     'count' => count($items),     'start' => $items->getStart(),     'limit' => count($items),     'total' => $items->getTotal()     )); and then of course…
      $pager = $modules->get("MarkupPagerNav"); echo '<div class="uk-flex uk-flex-center">'.$pager->render($items, $options).'</div>'; I'm out of ideas and confused cause it doesn't make sense to me either way.
      ____
      The buildSelector function above returns and array…
      $selected = new Selectors("$letter, $searchterm, $category"); $custom = new Wirearray; $custom->set('selector', $selected); $built->set('sort', $sort); return $custom; and each of the new selectors are basically strings ("category=whatever")
      Also, you cannot put the "sort=title" or whatever as a selector for the Selectors function (see above). Why, I know not.
      I don't know if that is the proper way but selecting kind of works now as opposed to many other ways I tried. Selectors always require a lot of trial and error, it seems to have a very sensitive API, always depends on double quotes, single quotes and how you concatenate.
      Thanks for help!
    • By SwimToWin
      I want to add a dependent SELECT field on my template page that lists pages from a parent "sub-page" in the current parent node.
      On /product1/page I have the field "photo" which is a SELECT field.
      I want the SELECT to list pages from /ROOTPARENT/photos.
      The idea is that I can reuse the same photo in many places - but only need to keep it update it once under /product1/photos.
      My page structure looks like so:
      /product1/page /product1/photos/photo3 (template=photos) /product2/photos/photo9 I have tried adding these Selector Strings on the Field (Setup -> Fields -> PHOTO -> Input tab -> Selectable Pages field group -> Selector String):
      parent=/product1/page, template=photos, sort=name WORKS (but only on children of current product). parent=page.rootParent ... parent=$page.rootParent ... parent=$page.rootParent parent=$parent ... parent=$parent1 When using a SELECT Input Field Type, the editing pages gives the fatal error "Unrecognized operator: $". parent=parent ... parent=. Returns an empty list How might I find child pages from the current "/product1/photos/ page"?
      Your inputs are appreciated. Thanks.
×
×
  • Create New...