Jump to content
MaierGrem

Field 'Page Reference' and condition 'Required if...'

Recommended Posts

Question to professionals)

I use the "Page Reference" field (name = categories, types = multiple, asm-select) and checkbox field (name = notify_user).

When editing the page, I need to check if:

- (categories) values has changed (deselected one or more current pages), make (notify_user) required.

- (categories) values has changed (selected one or more new pages), make (notify_user) required.

- (categories) values don't change, then the (notify_user) is not required.

I tried to make a condition, required if:

1. "categories!=1020|1490" - does not work.

2. "categories!=1020|1490, categories.count != 2" - does not work.

3. "categories%=1020|1490 - js error.

Please advise how best to solve this problem.

Share this post


Link to post
Share on other sites

Maybe it's a matter of rethinking this in light of what the real objective is.

I'm guessing that when the notify_user checkbox is checked and the page is saved, something happens - some sort of action that notifies a user. You're saying that you want to force the notify_user checkbox to be in a checked state if the categories field is changed, presumably to force the action to happen. So why not just do the action if the categories field has changed?

$pages->addHookAfter('saveReady', function(HookEvent $event) {
	$page = $event->arguments(0);
	// If the notify_user checkbox is checked or the categories field value has changed...
	if($page->notify_user || $page->isChanged('categories')) {
		// Do the notify user action...
	}
});

 

  • Like 1

Share this post


Link to post
Share on other sites

@Robin S Thanks for reply!

That's exactly what I did. But only the && operator, because I don't need to automatically send a notification. The administrator must know that he has notified the user.

If he forgot to check the notify_user checkbox, then after saving the page I will not be able to track categories changes..

The notification describes which categories have been deleted, which have been added, etc.

if ($template == 'member') {
	$old = clone($page);
	$old->uncache();
	$changed = WireArray();
	$fields = ['categories'];
	foreach($fields as $fieldname) {
		if ($page->isChanged($fieldname)) {
			$changed->set($fieldname, $old->$fieldname);
		}
	}
	if ($changed->has('categories')) {
		$categories = WireData();
		$categories->set('current', PageArray());
		$categories->set('added', PageArray());
		$categories->set('removed', PageArray());
		foreach ($old->categories as $category) {
			if (!$page->categories->has($category)) {
				$categories->removed->add($category);
			}
			else {
				if (!$categories->current->has($category)) $categories->current->add($category);
			}
		}
		foreach ($page->categories as $category) {
			if (!$old->categories->has($category)) {
				$categories->added->add($category);
			}
			else {
				if (!$categories->current->has($category)) $categories->current->add($category);
			}
		}
		bd($categories->current, 'current');
		bd($categories->added, 'added');
		bd($categories->removed, 'removed');
	}
}

This code in Hook after Pages::saveReady..

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 jds43
      Hello, I'm trying to list the categories, on the front through select options, that have been selected by page reference field (multiple pages PageArray) on the child pages.
      Things to Do (would only display three, six, seven, nine in select)
      -thing one (-category three, -category nine)
      -thing two (-category six, -category seven)

      Lodging (would only display one, two, three, four in select)
      -lodging one (-category one, -category two)
      -lodging two (-category three, -category four)

      Dining (would only display five, six, seven, eight in select)
      -dining one (-category five, -category six)
      -dining two (-category seven, -category eight)
      Categories(hidden page)
      -category one
      -category two
      -category there
      -category four
      -category five
      -category six
      -category seven
      -category eight
      -category nine
      -category ten
      $categories = $pages->find(1129)->children('include=hidden'); foreach($categories->references('category') as $ref) { echo $ref->title; } This selector isn't working, but it seems 'references' would be helpful. I've never used it before, so I'm not sure how to employ for this.
      https://processwire.com/blog/posts/processwire-3.0.107-core-updates/#page-gt-references
    • By Pip
      Hi, Everyone! 
      I'm currently working on a page reference field and set it for multiple pages (AsmSelect) for the input. Is there a way for me to add an image field (aka Avatar) and the title of page in the radio button? 
      I used the field name enclosed in the { }. Didn't work. It appeared a text instead. 
      Thanks in advance and hope to hear from you soon!
    • By jonatan
      Hi! 😄
      SITE SETUP / DESCRIPTION:
      What? Online art magazine with an "All featured works" and also an "All featured artists" index page, and also individual "Work" and "Artists" pages, and data relations between the different artists and their artworks.
      So, I have two different page reference fields, connected by @Robin S's awesome Connect Page Fields module (though that has nothing to do with the issue in fact, Robin's plugin works great! and the issue is the same with or without it).
      The two Page Reference type fields are called "works" and "artists". They are meant to simply connect different artists to different artworks.
      Fx:
      Work 1  (page)  –> artists (Page Reference field) : Artist A (page)           --->>> (automatically connected)         Artist A (page)  –> works (Page Reference field) : Work 1 (page)
      Work 2  (page) –> artists (Page Reference field) : Artist B  (page)           --->>> (automatically connected)         Artist B (page)  –> works (Page Reference field) : Work 2 (page)
      ISSUE:
      On both Page Reference fields, both on "artists" and on "works" this option "Allow unpublished pages" is activated:

      As it says in the option description, supposedly, unpublished pages should be selectable in the page reference field, but they should not be visible, they shouldn't appear, on the front-end...
      Frontend:
      To show all works related to the artist on the artist individual page I'm doing this:
      <?php foreach($page->works as $item) { echo "<img src='{$item->image->first->width(200)->url}' class='pr-2'><a class='pr-4' href='$item->url'>$item->title</a>"; } The problem is now, unexpectedly, if some work is set to "Unpublished", it shows up anyways!
      WORKAROUND:
      So to get around this I figured out that I can do this:
      <?php foreach($page->works as $item) { if($item->is(Page::statusUnpublished)) { return; }; echo "<img src='{$item->image->first->width(200)->url}' class='pr-2'><a class='pr-4' href='$item->url'>$item->title</a>"; } But ofc this is a rather inelegant "solution" which shouldn't really be necessary, right? 
      I might be missing something basic here, but really can't figure out what it is... I hope one of you awesome guys can help me out 😊
      Thanks a lot in advance!
      All the best,
      Jonatan
    • By Flashmaster82
      Hi, can you guys help a beginner with a problem..
      On my template (profile_page) i have a dropdown (page reference) where i can choose a sports team (team_page) that is related to that profile which is also its parent. Then on my Competition1 page (competition_page) I have page reference field (profiles) a dropdown that i want to display only profiles that has choose a specific sports team (template=team_page) the page parent to be specific.
      Structure/Template
      Sports_team1 (team_page)
           Profile1 (profile_page)
           Profile2 (profile_page)
           Profile3 (profile_page)
           Competition1 (competition_page)
       
      ready.php
      <?php $wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) { if($event->object->hasField == 'profiles') { $relative = $page->parent->name; $event->return = $event->pages->find("template=profile_page, sports_team=$relative"); } }); ?> This returns with no results in the dropdown. If i remove sports_team=$relative then it displays all profiles that have profile_page as template, so it works almost. But i will have more sports teams so this is just an example. I only want to display the profiles that has choosen the parent team on there profile page in admin not front end.
      I hope i was able to explain it so you guys can understand a little bit. Need some help please! /Thanks
       
       
    • By Macaco
      It's a bilingual site. There are two pages: "Artists" and "Events" each with a "Page Reference" field connecting each other.
      - Artists has a field where one can choose events available.
      - Events has a field where you can either choose artists available or create new ones.
      The problems: 
      - When I create an "Artist" page and select events from the list, it doesn't update the collection of participating artists on the "Event" page.
      - When I create an artist from the "Event" page. The field 'artist page > settings > language' is not "Active" for the second language.  When the artist page is created manually,"Active" is on by default.
      I know this all have to do with hooks, but I'm don't fully understand the logics.
×
×
  • Create New...