Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions js/fieldmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,16 @@ $( document ).ready( function () {
};
$( '.fm-display-if' ).each( fm.init_display_if );

// Initialize triggers to conditionally show/hide fields based on chosen page template.
var pageTemplate = $( '#page_template' ).val();
$( '.fm-display-if-page-template' ).each( function() {
if ( match_value( getCompareValues( this ), pageTemplate ) ) {
$( this ).show();
} else {
$( this ).hide();
}
} );

// Controls the trigger to show or hide fields
fm.trigger_display_if = function() {
var val;
Expand Down Expand Up @@ -272,6 +282,21 @@ $( document ).ready( function () {
};
$( document ).on( 'change', '.display-trigger', fm.trigger_display_if );

// Trigger show/hide of conditional fields when page template changes.
$( document ).on( 'change', '#page_template', function() {
var val = $( this ).val();

$( '.fm-display-if-page-template' ).each( function() {
if ( match_value( getCompareValues( this ), val ) ) {
$( this ).show();
} else {
$( this ).hide();
}

$( this ).trigger( 'fm_displayif_toggle' );
} );
} );

init_label_macros();
init_sortable();

Expand Down
15 changes: 10 additions & 5 deletions php/class-fieldmanager-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,13 +522,18 @@ public function element_markup( $values = array() ) {

// Checks to see if element has display_if data values, and inserts the data attributes if it does.
if ( isset( $this->display_if ) && ! empty( $this->display_if ) ) {
$classes[] = 'fm-display-if';
if ( isset( $this->display_if['page_template'] ) ) {
$classes[] = 'fm-display-if-page-template';
$fm_wrapper_attrs['data-display-value'] = $this->display_if['page_template'];
} else {
$classes[] = 'fm-display-if';

// For backwards compatibility.
$classes[] = 'display-if';
// For backwards compatibility.
$classes[] = 'display-if';

$fm_wrapper_attrs['data-display-src'] = $this->display_if['src'];
$fm_wrapper_attrs['data-display-value'] = $this->display_if['value'];
$fm_wrapper_attrs['data-display-src'] = $this->display_if['src'];
$fm_wrapper_attrs['data-display-value'] = $this->display_if['value'];
}
}
$fm_wrapper_attr_string = '';
foreach ( $fm_wrapper_attrs as $attr => $val ) {
Expand Down
42 changes: 42 additions & 0 deletions tests/php/test_fieldmanager_field.php
Original file line number Diff line number Diff line change
Expand Up @@ -1078,4 +1078,46 @@ public function test_element_markup_filters() {
$fm->element_markup( rand_str() );
$this->assertSame( 4, $ma->get_call_count(), 'Missing calls to element-markup filters' );
}

public function test_display_if_markup_for_page_template() {
$name = rand_str();
$template = 'toaster.php';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure there's a good story behind this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄

I wish it was more than this being a word unlikely to come up in a non-testing context. Instead of foo and bar, I use waffle iron and toaster pastry, for reasons I can no longer recall; probably something about being self-taught and learning programming when I was a hungry teenager. 😂


$fm = new Fieldmanager_TextField( array(
'name' => $name,
'display_if' => array(
'page_template' => $template,
),
) );

$html = $this->_get_html_for( $fm );

$this->assertContains( 'fm-display-if-page-template', $html );
$this->assertContains( $template, $html );
$this->assertNotContains( 'data-display-src', $html );
$this->assertContains( 'data-display-value', $html );
}

public function test_display_if_markup_for_standard_use() {
$name = rand_str();

$source = 'random_thing';
$value = 'another random thing';

$fm = new Fieldmanager_TextField( array(
'name' => $name,
'display_if' => array(
'src' => $source,
'value' => $value,
),
) );

$html = $this->_get_html_for( $fm );

$this->assertNotContains( 'fm-display-if-page-template', $html );
$this->assertContains( $source, $html );
$this->assertContains( $value, $html );
$this->assertContains( 'data-display-src', $html );
$this->assertContains( 'data-display-value', $html );
}
}