twig REST API error

Support MB Views twig REST API error

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #29979
    ViktorViktor
    Participant

    Hi, related to my previous topic post the following code works on the front-end but gives 500 Server error through REST API and when updating the post in Gutenberg. Updating through the API or Gutenberg works but the errors are still occurring.

    {% for clone in post.retailer_group | sort %}
        {{ clone.retailer_cpt.title }}
        ID: {{ clone.retailer_cpt.ID }}
        price: {{ clone.retailer_price }}
        .......
        {% set retailer = mb.get_post( clone.retailer_cpt.ID ) %}
        {{ retailer.retailer_shipping }}
    <br>
    {% endfor %}
    

    where retailer_cpt is a post selector field and my guess is that this is causing the problem since the content of the loop is irrelevant and the stock twig sort example here works perfectly.

    Is this a bug or am I doing something wrong?

    #29981
    ViktorViktor
    Participant

    Here is the log of the error:

    
    [06-Aug-2021 20:43:00 UTC] PHP Fatal error:  Uncaught MetaBox\Dependencies\Twig\Error\RuntimeError: The sort filter only works with arrays or "Traversable", got "NULL" in "relationship-view" at line 1. in /var/www/wordpress/wp-content/plugins/meta-box-aio/vendor/meta-box/meta-box-builder/dependencies/Twig/Extension/CoreExtension.php:914
    Stack trace:
    #0 /var/www/wordpress/wp-content/plugins/meta-box-aio/vendor/meta-box/meta-box-builder/dependencies/Twig/Environment.php(358) : eval()'d code(38): twig_sort_filter()
    #1 /var/www/wordpress/wp-content/plugins/meta-box-aio/vendor/meta-box/meta-box-builder/dependencies/Twig/Template.php(394): __TwigTemplate_fe2758630feb60b056deac33be7db367a9a38638a1d420f2e9909dcd82463035->doDisplay()
    #2 /var/www/wordpress/wp-content/plugins/meta-box-aio/vendor/meta-box/meta-box-builder/dependencies/Twig/Template.php(367): MetaBox\Dependencies\Twig\Template->displayWithErrorHandling()
    #3 /var/www/wordpress/wp-content/plugins/meta-box-aio/vendor/meta-box/meta-box-builder/dependencies/Twig/Template.php(379): MetaBox\Dependencies\Tw in /var/www/wordpress/wp-content/plugins/meta-box-aio/vendor/meta-box/meta-box-builder/dependencies/Twig/Extension/CoreExtension.php on line 914
    
    #29982
    ViktorViktor
    Participant

    Here is a var_dump(clone):

    
    array(2) {
        ["retailer_price"]=> string(3) "450"
        ["retailer_cpt"]=> array(9) {
            ["ID"]=> int(14) ["title"]=> string(16) "Some retailer"
            ["excerpt"]=> string(0) ""
            ["content"]=> string(0) ""
            ["url"]=> string(55) "https://example.com/retailer/some-retailer/"
            ["slug"]=> string(16) "some-retailer"
            ["date"]=> string(19) "2021-08-06 07:15:07"
            ["modified_date"]=> string(19) "2021-08-06 07:23:01"
            ["thumbnail"]=> array(8) { ["thumbnail"]=> bool(false) ["medium"]=> bool(false) ["medium_large"]=> bool(false) ["large"]=> bool(false) ["1536x1536"]=> bool(false) ["2048x2048"]=> bool(false) ["post-thumbnail"]=> bool(false) ["full"]=> bool(false) }
        }
    } 
    

    My goal is to sort the clones by retailer_price. Everything is an array, so I am not sure why it shows type "Null" in the logs.

    #30009
    Long NguyenLong Nguyen
    Moderator

    Hi,

    I've tried to reproduce the issue on my end but do not see any error like that. Screen record https://www.loom.com/share/4dd0881196c7487a93e71c5ca3afed3c

    The code that creates meta box and custom fields:

    add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
    function your_prefix_function_name( $meta_boxes ) {
        $meta_boxes[] = [
            'title'  => __( 'Post Meta', 'your-text-domain' ),
            'id'     => 'post-meta',
            'fields' => [
                [
                    'name'   => __( 'Retailer Group', 'your-text-domain' ),
                    'id'     => $prefix . 'retailer_group',
                    'type'   => 'group',
                    'clone'  => true,
                    'fields' => [
                        [
                            'name'       => __( 'Retailer CPT', 'your-text-domain' ),
                            'id'         => $prefix . 'retailer_cpt',
                            'type'       => 'post',
                            'post_type'  => ['page'],
                            'field_type' => 'select_advanced',
                        ],
                        [
                            'name' => __( 'Retailer Price', 'your-text-domain' ),
                            'id'   => $prefix . 'retailer_price',
                            'type' => 'text',
                        ],
                    ],
                ],
            ],
        ];
        return $meta_boxes;
    }

    The code in the View:

    {% for clone in post.retailer_group | sort((a, b) => a.retailer_price <=> b.retailer_price) %}
        {{ clone.retailer_cpt.title }}
        ID: {{ clone.retailer_cpt.ID }}
        price: {{ clone.retailer_price }}
    <br>
    {% endfor %}
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.