Jump to content

Syntax for Custom Theme Setting Condition field


Go to solution Solved by Ehren,

Recommended Posts

Posted

Hi,

I have a custom theme setting that works and I want to use the Condition field to only display if a previous custom (select type) theme setting isn't set to one of its key values, but I can't seem to get the right syntax:

<?php
if ( theme.my_other_custom_setting == 'value' )

    return FALSE;

I've tried with double curly brackets too. Also an ELSE returning TRUE.

Many thanks.

Posted

You should always use curly brackets, even if it might be valid syntax to drop them. They improve readability and prevents syntax errors. There’s really no reason not to use them.

With that being said, have you tried to print the value of the other theme setting to see what it returns?

Posted

Thanks, that's a good idea, I'll try next time I'm on my localhost. The info underneath that Condition field says to insert raw PHP. I've tried using the double curly brackets shown in the help guidance docs. There are a lot of examples of theme conditions but no example of what to use in this box.

Posted

I spoke too soon! I thought this would show the custom theme setting B to the Admin dependant on the setting of a preceding custom setting A but it doesn't work. The custom theme setting it applies to (B) seems to show no matter if it returns a true or false Boolean.

Posted

I see you have an opening <?php tag there...are you doing this in an actual PHP file somewhere, instead of in a template?

Within templates you can use "theme.setting_key" as a shortcut, and this gets expanded to valid PHP code. If you are using this in an actual PHP file you will need to do this instead:

if( \IPS\Theme::i()->settings['setting_key'] == 'value' )

Note that you need to have the framework available. If this were in a standalone file (again I see you have an opening PHP tag there), that would require loading the init.php file and getting an instance of the external dispatcher.

<?php

require 'init.php';
\IPS\Dispatcher\External::i();

 

Posted (edited)
13 minutes ago, bfarber said:

I see you have an opening <?php tag there...are you doing this in an actual PHP file somewhere, instead of in a template?

Thanks Brandon, I'm trying to use the Condition field here, it inserts an opening PHP tag for you and says its for raw PHP, I didn't use a closing PHP tag:

image.thumb.png.a74f66d9dbfb7882ecb6267ed638d91a.png

At the bottom it says you can make it (this custom setting) condition. I tried so many variations yesterday for over 8 hrs, I actually dreamt about it overnight!

Basically, I just want to this custom setting to be displayed in the theme settings form depending upon the previous setting which is a select box containing line-gradient and radial-gradient. This field (site_title_color_gradient_direction) has no relevance if the user chooses the radial-gradient option in the first gradient-type setting site_title_color_gradient_type in my styles.css file and I have got it working on the front end with this:

#elSiteTitle {     
   background-image: {theme="site_title_color_gradient_type"}( {{if theme.site_title_color_gradient_type === 'linear-gradient'}}
    {theme="site_title_color_gradient_direction"}
{{endif}} {hextorgb="site_title_color_gradient_start" opacity="1"}, {hextorgb="site_title_color_gradient_finish" opacity="1"});  
  -webkit-text-fill-color: transparent;  
  background-clip: text;
  -webkit-background-clip: text;
  font: 400 {theme="site_title_font_size"}px Creepster, Helvetica, sans-serif;
  color: #fff;
  transition: all 300ms ease;
}

So for now, because I can't get the Condition field working, I've just added a note to this custom theme setting that says its ignored for radial gradients.

Hope this makes sense!

PS.

16 minutes ago, bfarber said:

if( \IPS\Theme::i()->settings['setting_key'] == 'value' )

Does this apply to custom theme settings as well as default? Thanks.

Edited by The Old Man
  • Solution
Posted
On 10/27/2020 at 12:26 AM, The Old Man said:

Basically, I just want to this custom setting to be displayed in the theme settings form depending upon the previous setting which is a select box containing line-gradient and radial-gradient.

I experimented with a feature like this with my themes. I only wanted a setting to be displayed if a previous setting was set to a certain value.

What I later realised is that the conditional setting will only be displayed once the existing setting has actually been saved. There isn't any ajax functionality which would make the "Gradient direction" setting appear as soon as "Radial gradient" is selected, which actually results in a pretty poor experience for admins since they won't see the new setting until they've saved the existing page and reloaded it.

In my own themes, I've simply added a description to situations like this which basically says "If Radial Gradient is selected above, this setting will control its direction." I feel like this may also be the best solution in your case.

Posted

Thanks @Ehren I really appreciate that insight also because I've been working on my first theme with custom theme settings and being unfamiliar is the enemy! I thought it was meI So I wonder what purpose it actually serves then as a conditional field?

Re adding a descriptive label, I found it's difficult to keep the label short and so I wondered if there a way to add a description under the custom theme setting like you can when you do a form for say a plugin's settings? In a plug-in form, you would append _desc to your language key and your description appears neatly under the field. I tried this yesterday and got an error message whilst in theme builder mode, it didn't like the _desc appendage on my custom form field at all, which seems odd as supposedly it's a form helper.

Posted

The desc tag works for me. I actually use it in my own theme settings:

<?php
	$lang = array(
		'themesetting_focus_light_dark_enable' => 'Enable light/dark toggle?',
		'themesetting_focus_light_dark_enable_desc' => 'A toggle icon will be added to your theme allowing your members to easily switch to an alternate color scheme.',
	);

 

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...