Jump to content

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


MaierGrem
 Share

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.

Link to comment
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
Link to comment
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..

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

×
×
  • Create New...