Advertisement:

Author Topic: custom fields  (Read 93620 times)

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: custom fields
« Reply #15 on: December 28, 2015, 04:28:43 pm »
Hm... "sub-selects" (select_2 in this case) are tricky, because they don't have a label as all the other elements have.

Find this in my code:

Code: [Select]
            } else {                   
                var destinationElement = $("select[id='" + elementId + "']").parent('div').parent('div');

and replace it with:

Code: [Select]
            } else {                   
                var destinationElement = $("select[id='" + elementId + "']").parent('div');

Regards

spevoli

  • Jr. Member
  • **
  • Posts: 83
Re: custom fields
« Reply #16 on: December 30, 2015, 12:27:43 pm »
Hello Teseo,
Thanks for your help!
took something with me but now it works great :-)

I wish you a great start into the New Year!

spevoli

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: custom fields
« Reply #17 on: December 30, 2015, 01:19:32 pm »
Your welcome. :)

Happy New Year, regards.

nadeem

  • Newbie
  • *
  • Posts: 8
Re: custom fields
« Reply #18 on: May 06, 2016, 05:47:57 pm »
Dear
I have tried this code in fucntions.php

But it relocate only one category what about another categories also i want other categories to relocate
<?php
function custom_move_custom_field() {
    $section = Rewrite::newInstance()->get_section();
    if ($section == 'item_add' || $section == 'item_edit') { ?>

    <script type="text/javascript">

        // Variable data, change values here only
        var customFieldsToMove = {
            0 : {   'cfIdentifier' : 'art_der_anzeige', 'destination': 'select_1'},
            1 : {   'cfIdentifier' : 'tuev_1', 'destination': 'select_2'}
        };
        // End Variable data

        $(document).ajaxComplete(function(event, xhr, settings){
           
            if (typeof settings['data'] == 'undefined' || settings['data'].indexOf('hook=item_') == -1) return; // No Category

            for(var i in customFieldsToMove) {
                customFieldIdentifier = customFieldsToMove['cfIdentifier'];
                elementId = customFieldsToMove['destination'];

                if ($("label[for='" + elementId + "']").length) {
                    var destinationElement = $("label[for='" + elementId + "']").parent('div');
                } else {
                    var destinationElement = $("select[id='" + elementId + "']").parent('div');
                }
                moveCustomField(customFieldIdentifier, destinationElement);
            }
        });

        function moveCustomField(customFieldIdentifier, destinationElement) {
            var movedId = 'moved_custom_field_' + customFieldIdentifier;
            var toMoveLabel = $("label[for='meta_" + customFieldIdentifier + "']");
           
            if (toMoveLabel.length && !$('#' + movedId).html()) {
                $("<div></div>").attr('id', movedId).insertBefore(destinationElement);

                if (destinationElement.prop('id').indexOf('meta_') == -1) {
                    toMoveLabel.parent('.meta').find('*').each(function() {
                        $(this).copyCSS($(this));
                    });
                }

                $('#' + movedId).append(toMoveLabel.parent('.meta'));
            } else {
                $('#' + movedId).remove();
            }
            $("<div></div>").attr('class', 'clear').css('padding-bottom', '10px').appendTo($('#' + movedId));
        }
       
        $.fn.copyCSS = function (source) {
            var dom = $(source).get(0);
            var dest = {};
            var style, prop;
            if (window.getComputedStyle) {
                var camelize = function (a, b) {
                    return b.toUpperCase();
                };
                if (style = window.getComputedStyle(dom, null)) {
                    var camel, val;
                    if (style.length) {
                        for (var i = 0, l = style.length; i < l; i++) {
                            prop = style;
                            camel = prop.replace(/\-([a-z])/, camelize);
                            val = style.getPropertyValue(prop);
                            dest[camel] = val;
                        }
                    } else {
                        for (prop in style) {
                            camel = prop.replace(/\-([a-z])/, camelize);
                            val = style.getPropertyValue(prop) || style[prop];
                            dest[camel] = val;
                        }
                    }
                    return this.css(dest);
                }
            }
            if (style = dom.currentStyle) {
                for (prop in style) {
                    dest[prop] = style[prop];
                }
                return this.css(dest);
            }
            if (style = dom.style) {
                for (prop in style) {
                    if (typeof style[prop] != 'function') {
                        dest[prop] = style[prop];
                    }
                }
            }
            return this.css(dest);
        };
    </script>

<?php }
}

osc_add_hook('footer', 'custom_move_custom_field', 1);

function custom_remove_moved_custom_field() { ?>
    <script type="text/javascript">
        $("div [id^='moved_custom_field']").remove();
    </script>
<?php }

osc_add_hook('item_form', 'custom_remove_moved_custom_field', 6);
?>

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: custom fields
« Reply #19 on: May 06, 2016, 07:20:47 pm »
Hi,

But it relocate only one category what about another categories also i want other categories to relocate

What do you mean by "categories"? ??? If you're talking about "custom fields", you can add more here:

Quote
var customFieldsToMove = {
            0 : {   'cfIdentifier' : 'art_der_anzeige', 'destination': 'select_1'},
            1 : {   'cfIdentifier' : 'tuev_1', 'destination': 'select_2'},
          2 : {   'cfIdentifier' : 'another_one', 'destination': 'select_1'}

        };

Regards

beginner

  • Full Member
  • ***
  • Posts: 135
Re: custom fields
« Reply #20 on: June 19, 2016, 10:25:35 am »
Solved, thx.
« Last Edit: June 20, 2016, 04:38:48 pm by beginner »

nadeem

  • Newbie
  • *
  • Posts: 8
Re: custom fields
« Reply #21 on: June 21, 2016, 06:03:05 am »
Thanx a lot Teseo for your help

roland51

  • Newbie
  • *
  • Posts: 2
Re: custom fields
« Reply #22 on: June 21, 2017, 07:58:27 pm »
teseo, Your code works very well, but how to display "CUSTOM FIELDS" under description area?

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: custom fields
« Reply #23 on: June 23, 2017, 04:52:50 pm »
Hi,

What theme are you using?

Regards

roland51

  • Newbie
  • *
  • Posts: 2
Re: custom fields
« Reply #24 on: June 24, 2017, 03:46:00 pm »
Hi!

OsclassWizards...

Regards

teseo

  • Hero Member
  • *****
  • Posts: 6169
Re: custom fields
« Reply #25 on: June 25, 2017, 02:45:39 pm »
Well, "move after Description" has proven to be quite tricky for two reasons:

1.- ID of the Description element includes current user language (same for Title).

Solution: In Variable Data block, 'destination' value must be like this:

For Description: 'destination': 'description[<?php echo osc_current_user_locale(); ?>]'
For Title: 'destination': 'title[<?php echo osc_current_user_locale(); ?>]'

2.- In the previous code, moved custom fields were being placed before (above) some element, but usually the element after Description is "Price" and it might exist or not depending on the settings for the chosen category.

Solution: To add a new field 'position' (with value 'before' or 'after'). That requires to modify the whole code:

Code: [Select]
<?php
function custom_move_custom_field() {
    
$section Rewrite::newInstance()->get_section();
    if (
$section == 'item_add' || $section == 'item_edit') { ?>


        <script type="text/javascript">
            // Variable data, change values here only
            var customFieldsToMove = {
                0 : { 'cfIdentifier' : 'art_der_anzeige', 'destination': 'select_1', 'position': 'before' },
                1 : { 'cfIdentifier' : 'tuev_1', 'destination': 'description[<?php echo osc_current_user_locale(); ?>]', 'position': 'after'}
            };
            // End Variable data

            $(document).ajaxComplete(function(event, xhr, settings) {
                if (typeof settings['data'] == 'undefined' || settings['data'].indexOf('hook=item_') == -1) return; // No Category

                for(var i in customFieldsToMove) {
                    elementId = customFieldsToMove[i]['destination'];

                    if ($("label[for='" + elementId + "']").length) {
                        var destinationElement = $("label[for='" + elementId + "']").parent('div');
                    } else {
                        var destinationElement = $(":input[id='" + elementId + "']").parent('div');
                    }
                   
                    moveCustomField(customFieldsToMove[i]['cfIdentifier'], destinationElement, customFieldsToMove[i]['position']);
                }
            });

            function moveCustomField(customFieldIdentifier, destinationElement, position) {
                var movedId = 'moved_custom_field_' + customFieldIdentifier;
                var toMoveLabel = $("label[for='meta_" + customFieldIdentifier + "']");

                if (toMoveLabel.length && !$('#' + movedId).html()) {
                    if (position == 'before') {
                        $("<div></div>").attr('id', movedId).insertBefore(destinationElement);
                    } else {
                        $("<div></div>").attr('id', movedId).insertAfter(destinationElement);
                    }                   

                    if (destinationElement.prop('id').indexOf('meta_') == -1) {
                        toMoveLabel.parent('.meta').find('*').each(function() {
                            $(this).copyCSS($(this));
                        });
                    }

                    $('#' + movedId).append(toMoveLabel.parent('.meta'));
                    $("<div></div>").attr('class', 'clear').css('padding-bottom', '10px').appendTo($('#' + movedId));
                } else {
                    $('#' + movedId).remove();
                }               
            }

            $.fn.copyCSS = function (source) {
                var dom = $(source).get(0);
                var dest = {};
                var style, prop;
                if (window.getComputedStyle) {
                    var camelize = function (a, b) {
                        return b.toUpperCase();
                    };
                    if (style == window.getComputedStyle(dom, null)) {
                        var camel, val;
                        if (style.length) {
                            for (var i = 0, l = style.length; i < l; i++) {
                                prop = style[i];
                                camel = prop.replace(/\-([a-z])/, camelize);
                                val = style.getPropertyValue(prop);
                                dest[camel] = val;
                            }
                        } else {
                            for (prop in style) {
                                camel = prop.replace(/\-([a-z])/, camelize);
                                val = style.getPropertyValue(prop) || style[prop];
                                dest[camel] = val;
                            }
                        }
                        return this.css(dest);
                    }
                }
                if (style = dom.currentStyle) {
                    for (prop in style) {
                        dest[prop] = style[prop];
                    }
                    return this.css(dest);
                }
                if (style == dom.style) {
                    for (prop in style) {
                        if (typeof style[prop] != 'function') {
                            dest[prop] = style[prop];
                        }
                    }
                }
                return this.css(dest);
            };
        </script>

    <?php }
}

osc_add_hook('footer''custom_move_custom_field'1);

function 
custom_remove_moved_custom_field() { ?>

    <script type="text/javascript">
        $("div [id^='moved_custom_field']").remove();
    </script>
<?php }

osc_add_hook('item_form''custom_remove_moved_custom_field'6);
?>


So, this is what will do the trick for what you want:

[X] : { 'cfIdentifier' : '[custom field identifier]', 'destination': 'description[<?php echo osc_current_user_locale(); ?>]', 'position': 'after'}

And if you're already moving other custom fields, remember to add , 'position': 'before' to them

Regards


« Last Edit: June 25, 2017, 05:12:04 pm by teseo »