Home>Support>Sorting prebuilt layouts results into js error when importing the 1st shown

Sorting prebuilt layouts results into js error when importing the 1st shown

Notice: This thread is over two years old; the information may be outdated. Please consider creating a new thread if you require free support. If you have an active SiteOrigin Premium license, you can email our premium support desk at [email protected].

Hi there,

I’ve tried the sorting snippet here: https://siteorigin.com/docs/page-builder/bundling-prebuilt/

To alphabetically sort the theme saved prebuilt layout for a website of mine. The snippet works, as the layouts are displayed according to their name, but unfortunately sometimes I cannot import them first shown (aka the one flagged with

data-layout-id="0"
)

The UI just freezes and I get the following error in console: https://postimg.cc/KkyBvpJj

Changing the function priority doesn’t change behaviour, unless you go below 10, in which case the layout won’t be sorted.

It is not layout-related, because if I rename the layout not to appear in 1st position, then I can import it anyway.

I guess the issue is “0” being wrongly evaluated in js: this does not happen without the sorting filter function because in that case the data-layout-id is not a number.

In fact, I have created a dummy layout so that the true layouts will start with 1 as data-layout-id, but it’s a raw workaround.

This is our free support forum. Replies can take several days. If you need fast email support, please purchase a SiteOrigin Premium license.

  1. 2 years, 6 months ago frafor

    I would like to add that after more testings, sometimes the selected layout is not the imported one.

    For example, If I select the layout with data-id=11, then it imports the layout with data-id=4

  2. 2 years, 6 months ago Alex S
    Hi, I Work Here

    Hi Frafor,

    I’ve identified an oversight that will prevent a layout from being imported if its id is 0. This will be fixed in an upcoming SiteOrigin Page Builder update. This issue is on the PHP side of things rather than JavaScript.

    Regarding the other raised issue with the incorrect layouts being imported, I’m having trouble replicating that result. Is this happening consistently, or infrequently? Can it happen directly after loading the editor, or is it only after a specific action?

    Kind regards,
    Alex

  3. 2 years, 6 months ago frafor

    Hi Alex

    I’ve checked the second issue and it was template-related.

    Happy to know that you’ll fix the first one :) Anyway, as a temporary workaround, I applied the following fix yesterday:

    /*
    * Sort SOPB Prebuilt Layouts by Alphabetical ID
    */

    add_filter( 'siteorigin_panels_prebuilt_layouts', 'reorder_sopb_layouts', 20, 1);
    function reorder_sopb_layouts($layouts) {

    // Sort layouts alphabetically.
    usort( $layouts, function( $a, $b ) {
    return strcmp( $a['id'], $b['id'] );
    } );

    // It seems SiteOrigin messes with numbers
    $reordered = array();
    foreach($layouts as $data) {
    $reordered[$data['id']] = $data;
    }

    return $reordered;
    };

Replies on this thread are closed. Please create a new thread if you have a question, or purchase a SiteOrigin Premium license if you need one-on-one email support.

Get The Most Out of SiteOrigin with SiteOrigin Premium

Find Out More