Created
June 8, 2018 13:34
-
-
Save andrewmy/8c2bf4737872762ab3f552640ade47f8 to your computer and use it in GitHub Desktop.
Show/hide Sonata admin field groups based on some choice field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Admin; | |
class FooAdmin extends AbstractAdmin | |
{ | |
protected function configureFormFields(FormMapper $formMapper): void | |
{ | |
$formMapper | |
->add('mainField', ChoiceType::class, [ | |
'choices' => $mainChoices, | |
'choice_attr' => function ($value, $label) { | |
return [ | |
'data-service' => 'some-other-value-'.$value, | |
]; | |
}, | |
]) | |
->add('dependentField', ChoiceType::class, [ | |
'choices' => $otherChoices, | |
'attr' => [ | |
'class' => 'js-show-conditional', | |
'data-field' => '[name$=\[mainField\]]', | |
'data-attribute' => 'data-service', | |
'data-value' => 'value-1', | |
'data-scope' => 'tr', | |
], | |
]) | |
; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.fn.showHideByFieldAttribute = function($field, attribute, value) { | |
var doShow = false; | |
if (attribute === 'value') { | |
doShow = ($field.val() === value); | |
} else { | |
if ($field.is('select')) { | |
doShow = ($field.find('option:selected').attr(attribute) === value); | |
} else { | |
doShow = ($field.prop(attribute) === value); | |
} | |
} | |
var $group, | |
propName = 'display', | |
propValue = doShow ? 'block' : 'none'; | |
if ($(this).closest('.sonata-ba-field-inline-table').length) { | |
$group = $(this).closest('.control-group'); | |
propName = 'visibility'; | |
propValue = doShow ? 'visible' : 'hidden'; | |
} else if ($(this).is('div')) { | |
$group = $(this); | |
} else if ($(this).closest('.inline-control-group').length) { | |
$group = $(this).closest('.inline-control-group'); | |
propValue = doShow ? 'inline' : 'none'; | |
} else { | |
$group = $(this).closest('.form-group'); | |
} | |
$group.css(propName, propValue); | |
}; | |
function setupOneConditionalShowField($this, $field, attribute, value) { | |
if ($field.length) { | |
$this.showHideByFieldAttribute($field, attribute, value); | |
$field.change(function () { | |
$this.showHideByFieldAttribute($field, attribute, value); | |
}); | |
} | |
} | |
function setupConditionalShowFields() { | |
$('.js-show-conditional').each(function () { | |
var $this = $(this), | |
$scope = $this.data('scope') ? $this.closest($this.data('scope')) : null, | |
$field = $scope | |
? $scope.find($this.data('field')).filter(':not(div)') | |
: $($this.data('field')).filter(':not(div)'), | |
attribute = $this.data('attribute'), | |
value = $this.data('value'); | |
setupOneConditionalShowField($this, $field, attribute, value, $scope); | |
}); | |
} | |
// override to call additional things | |
Admin.shared_setup = function(subject) { | |
Admin.log("[core|shared_setup] Register services on, patched", subject); | |
Admin.set_object_field_value(subject); | |
Admin.add_filters(subject); | |
Admin.setup_select2(subject); | |
Admin.setup_icheck(subject); | |
Admin.setup_checkbox_range_selection(subject); | |
Admin.setup_xeditable(subject); | |
Admin.setup_form_tabs_for_errors(subject); | |
Admin.setup_inline_form_errors(subject); | |
Admin.setup_tree_view(subject); | |
Admin.setup_collection_counter(subject); | |
Admin.setup_sticky_elements(subject); | |
Admin.setup_readmore_elements(subject); | |
setupConditionalShowFields(); | |
}; | |
$(function() { | |
$(document).on('sonata.add_element sonata-admin-append-form-element sonata-admin-setup-list-modal', function(){ | |
setupConditionalShowFields(); | |
}); | |
setupConditionalShowFields(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment