Daily Lucky Numbers:
8
11
19
29
40
42

Block collapsed by Default?

Started by Aaron, June 28, 2023, 02:28:36 AM

Previous topic - Next topic

Aaron

Bit of a strange request but is there a way for having a block collapsed by default rather than expanded?

Chen Zhen

#1
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);