Skip to content

Commit

Permalink
add rename tab
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanedemotte committed Aug 27, 2018
1 parent d30ded5 commit 05b3376
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions acf-origin.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function acf_init() {
function acf_include_field_types( $version = false ) {
include_once('fields/origin-acf-field-unique.php');
include_once('fields/origin-acf-field-slug.php');
include_once('fields/origin-acf-field-tab-name.php');
}
}

Expand Down
64 changes: 64 additions & 0 deletions assets/js/origin-acf-field-tab-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
(function($){

acf.add_action('ready', function( $el ){
if(acf.field_group)
var acf_settings_tn = acf.field_group.field_object.extend({
type: 'tn',
actions: {
'render_settings': 'render',
},
render: function () {
var $choices = $('.acf-field-object-tab')
var $input = this.setting('origin_tn_select').find('select')

$input.off('select2:open')
$input.on('select2:open', this.render.bind(this))

$.each($choices, function(k, c) {
var data = acf.get_data($(c))
var name = $('#acf_fields-' + data.id +'-label').val()
var text = name + ' (' + data.type + ')'

if($input.find("option[value='" + data.key + "']").length)
return

var newOption = new Option(text, data.key, false, false)
$input.append(newOption).trigger('change')
})
}
})
})

acf.add_action('prepare', function( $el ){
acf.fields.tn = acf.field.extend({
type: 'tn',
$input: null,
ref: null,
actions: {
'ready': 'render',
'append': 'render',
},
focus: function() {
this.$input = this.$field.find('input')
this.ref = this.$input.data('ref')
this.$ref = $('a[data-key="' + this.ref + '"]')
},
render: function () {
var $ref = this.$ref

var onType = function() {
$ref.html(this.value)
}

if(this.$input.val() != ''){
$ref.html(this.$input.val())
}

this.$input.off('input', onType)
this.$input.on('input', onType)
}
})
})

})(jQuery);

54 changes: 54 additions & 0 deletions fields/origin-acf-field-tab-name.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
if( ! defined( 'ABSPATH' ) ) exit;

if( !class_exists('origin_acf_field_tn') ) :

class origin_acf_field_tn extends acf_field {

function __construct( $settings ) {
$this->name = 'tn';
$this->label = 'Origin/Tab Name';
$this->category = 'basic';
$this->settings = $settings;
parent::__construct();
}

function render_field( $field ) {
?>
<input
type="text"
data-ref="<?php echo esc_attr($field['origin_tn_select']) ?>"
name="<?php echo esc_attr($field['name']) ?>"
value="<?php echo esc_attr($field['value']) ?>" />
<?php
}

function render_field_settings( $field ) {
acf_render_field_setting($field, [
'label' => 'Fields',
'instructions' => 'Select a tab',
'type' => 'select',
'name' => 'origin_tn_select',
'multiple' => 0,
'allow_null' => 0,
'required' => 1,
'choices' => $this->get_tn_setting_choices( $field['origin_tn_select'] ),
'ui' => 1,
'placeholder' => '',
]);
}

function get_tn_setting_choices( $value ) {
$field = acf_get_field($value);
return [$value => $field['label'] . ' (' . $field['type'] . ')'];
}

function input_admin_enqueue_scripts() {
wp_register_script('acf-origin-tn', "{$this->settings['url']}assets/js/origin-acf-field-tab-name.js", array( 'acf-input'), $this->settings['version']);
wp_enqueue_script('acf-origin-tn');
}
}

new origin_acf_field_tn( $this->settings );

endif;

0 comments on commit 05b3376

Please sign in to comment.