Jump to content

Conditional logic for fields


doolak
 Share

Recommended Posts

Well, i guess this is a very complicated feature which would take a lot of time to implement it...

But - if one day this would be added that would be really absolutely amazing!

BTW: does the Form Builder support conditional logic?

Link to comment
Share on other sites

There are a lot of scenarios where i would like to use such a conditional logic - one e.g. could be the following:

I want provide a repeater where you can choose an image, set the alignment and an optional linking to the big version of the image or an internal/external link

So the repeater fields would be like that:

1. Image field

2. Page field to choose alignments

3. Checkbox if should be linked to bigger image

4. External or internal link

The linking to the bigger image will work automatically if the Checkbox is checked - i use the Fancybox for showing the image.

If this option is checked, the 4. option does not make any sense - clicking the image cannot work as a link and show up the fancybox at one time.

So it wouldn't matter if the client provides that link in option 4 if the conditional logic is set in the template - but to avoid confusion it would be nicer to show this option just if option 3 is not checked, so a conditional logic for the administration would be great.

Sounds a bit like "if then" logic in the backend view.

Correct me if I misunderstood.

That's correct.

  • Like 1
Link to comment
Share on other sites

Ok, just to be sure that's what you wanted. It's funny that I was thinking the same thing some days ago, and came to the conclusion that it could be done with some kind of fieldSet with a checkbox attached and that would work with jQuery hide()/show()... hm, let me see if I can explain... imagine a fieldSet field, those that you get field and a field_END where you can wrap a bunch of fields. The conditional can be on the field settings with a checkbox field, then, with javascript is easy to show or hide all the other fields.

only a proof of concept of course...

Link to comment
Share on other sites

Actually I've recently implemented something like this to a client as a module, though only to solve one very specific problem. Essentially their site has repeaters with checkboxes to select which fields of that particular repeater should be disabled from page edit.. and based on those choices things are handled slightly differently at template level. If all those fields were visible at all times it would've been quite confusing to use so something like that pretty much had to be done.

Based on my experience so far I can say that this kind of feature would be useful - although cases where I've really needed it (ie. where taking a step back and slightly rethinking things to avoid unnecessarily complicating things hasn't helped) have been very rare so far. But that's just me, there are definitely lots of good use cases for this as you've already pointed out :)

Link to comment
Share on other sites

The modules can use jquery. Have a look at how Apeisa's thumbnails module is structured, for instance https://github.com/a...rocessCropImage

I thought about maybe using a module just for that "hook", something similar to the HelloWorld.module or the MapPageField.module - so apply the jQuery after the page is saved or better, rendered.

Just don't know how to add code to the head of the admin page, the HellowWorld.module uses $this->message() but i would need something similar which makes it possible to apply some Javascript in the header...

Link to comment
Share on other sites

Right now i guess i recognized how it could be done in the easiest way...

I think it will be the best solution to add that jQuery by adding it to a admin theme - just have to digg a bit through that and find out if then it would be necessary to use a complete theme or if it's possible just to add the necessary code to the standard theme. But as some themes use some of the files of the default template this may be possible.

Link to comment
Share on other sites

The module diogo linked to does just that with these few lines:

$this->config->scripts->add($this->config->urls->ProcessCropImage . "ProcessCropImage2.js");
$this->config->styles->add($this->config->urls->ProcessCropImage . "ProcessCropImage2.css");

$this->config->scripts->add($this->config->urls->ProcessCropImage . "Jcrop/js/jquery.Jcrop.min.js");
$this->config->styles->add($this->config->urls->ProcessCropImage . "Jcrop/css/jquery.Jcrop.css");

The $this->config->scripts/styles->add basicall ensures that the following file is added to the admin template. As you can see, in the above example, you can get the URL to the module folder itself really easily by using the module name -no need to put stuff directly into the admin template if you're creating a module :)

Link to comment
Share on other sites

Thanks, Pete!

So that seems to be better and easier than using a admin theme... i just had a look at such a admin theme (never tried one before until now) and i guess my idea above will not work in that way without using a complete new admin theme, right? Or would it be possible to have just a addition to the default theme in that new admin theme?

But what you explained would make that unnecessary - if it's possible like that to add code at the admin theme this would be of course the best way.

So if i understood it right, i would have to use $this->config->scripts->add() in a module to add the JQuery to the admin template, right?

Link to comment
Share on other sites

Wow - that works! Thank you very much!

I have just a minor issue left:

If i add a script as follows, it adds this script at the top of the included scripts in the admin page and it seems that JQuery has to be loaded first. (I have done a test with loading jQuery too with that module, this is then added a the top and it works fine.)

Is there a possibility to add the script at the end of the included scripts?

My actual code is as follows:

<?php
/**
* ProcessWire 'Admin jQuery'
*
* ProcessWire 2.x
* Copyright (C) 2010 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
class AdminJQuery extends Process implements Module {
public static function getModuleInfo() {
return array(
'title' => 'Admin jQuery',
'version' => 101,
'summary' => 'Add costum jQuery to the admin theme',
'singular' => true,
'autoload' => true,
);
}
public function init() {
$this->config->scripts->add($this->config->urls->AdminJQuery . "adminjquery.js");

}

}
Link to comment
Share on other sites

The problem there is that you've got a Process module that is 'autoload', and Process modules aren't meant to be autoload except in specific circumstances. I'd suggest extending WireData rather than Process. Another issue is that you don't want to add that script to every page like you are doing now. You only want to add it when it's needed. So you'd hook into something like ProcessPageEdit::execute instead. This would ensure that your JS is only loaded when it is needed.

public function init() {
 $this->addHookAfter('ProcessPageEdit::execute', $this, 'addScript'); 
}

public function addScript(HookEvent $event) {
 $this->config->scripts->add($this->config->urls->AdminJQuery . "adminjquery.js"); 
}
  • Like 2
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.
×
×
  • Create New...