WebDev

EhPortal => EhPortal Support => Topic started by: Aaron on June 28, 2023, 02:28:36 AM

Title: Block collapsed by Default?
Post by: Aaron on June 28, 2023, 02:28:36 AM
Bit of a strange request but is there a way for having a block collapsed by default rather than expanded?
Title: Re: Block collapsed by Default?
Post by: Chen Zhen on July 01, 2023, 01:02:05 AM
For any PHP block you can add this to the code:
$collapsed = true;
$ids = !empty($id) ? array($id) : array("0");
$ehPortalBlockToggleUp = function($collapsed = true) use ($ids){
    global $context;

    if (!empty($collapsed))
        $context['html_headers'] .= '
        <script>
            $(document).ready(function() {
                var blockIdArray = ' . json_encode($ids) . ';
                for(i=0;i < blockIdArray.length; i++) {
                    if ($("#sp_collapse_" + blockIdArray[i]).length != 0 && $("#sp_collapse_" + blockIdArray[i]).hasClass("toggle_up")) {
                        sp_collapseBlock(String(blockIdArray[i]));
                    }
                }
            });
        </script>';
};
$ehPortalBlockToggleUp($id, $collapsed);



For a HTML block I would have to play around with the DOM to find the ID at the start of the block.




Another option is to use the above code with an array of manually entered block ID's to close any blocks on the page.
For something like this you can create a PHP block to display everywhere plus hide its title & body so the block itself isn't visible.

$collapsed = true;

// enter block id's
$ids = array(2,5,7,8,9);
$ehPortalBlockToggleUp = function($collapsed = true) use ($ids){
    global $context;

    if (!empty($collapsed))
        $context['html_headers'] .= '
        <script>
            $(document).ready(function() {
                var blockIdArray = ' . json_encode($ids) . ';
                for(i=0;i < blockIdArray.length; i++) {
                    if ($("#sp_collapse_" + blockIdArray[i]).length != 0 && $("#sp_collapse_" + blockIdArray[i]).hasClass("toggle_up")) {
                        sp_collapseBlock(String(blockIdArray[i]));
                    }
                }
            });
        </script>';
};
$ehPortalBlockToggleUp($collapsed);

EhPortal 1.39.6 © 2024, WebDev