[SOLVED] Echoing a contents of a block without being able to edit it

Post Reply
EugeneTheCoder
Posts: 2
Joined: Wed Apr 05, 2017 3:15 am

[SOLVED] Echoing a contents of a block without being able to edit it

Post by EugeneTheCoder »

I have a question: if it's possible to just echo the contents of a block without it being wrapped with editable span even in admin mode?
For now I looked into core index file and found these lines:

Code: Select all

    public static function block($key)
    {
        $blocks = self::get('blocks');

        return isset($blocks->{$key}) ? (self::$loggedIn ? self::editable($key, $blocks->{$key}->content, 'blocks') : $blocks->{$key}->content) : '';
    }
So I've made my own little function:

Code: Select all

    public static function blockEcho($key)
    {
        $blocks = self::get('blocks');

        return $blocks->{$key}->content;
    }
I think there is another way of doing it, and you can manage it through custom functions.php file. There was a link provided by you (wiz) - viewtopic.php?f=27&t=756 - but it's non-working. So, the second question is: where to see and example of working functions.php file, that shows an example on how to plug into the core and add something, without modifying index.php file? That would be a good one for starters.
Last edited by EugeneTheCoder on Tue Apr 11, 2017 3:12 am, edited 1 time in total.
User avatar
wiz
Admin
Posts: 569
Joined: Sat Oct 30, 2010 12:23 am

Re: Echoing a contents of a block

Post by wiz »

Hello Eugene, welcome to the community.

You're completely correct, we need a helpful documentation on this.
A good heads up until we arrange this in the incoming days is yassines post viewtopic.php?f=22&t=791#p1511

Since we don't have a hook for the editable function, which would make it extra easy to override, we're going to suggest another way.

In your functions.php, you can add a listener to the JavaScript, which strips the <span> tags with the subside id.
The functions.php file

Code: Select all

<?php
/**
 * Adding listeren to js
 * stripping all spans with subside id of their wrapping <span>
 */
 
    wCMS::addListener('js', 'hideEditableBlockSubside');

function hideEditableBlockSubside($args) {
    $script = <<<'EOT'
<script>
$(function() {
    $('span#subside').contents().unwrap();
});
</script>
EOT;
    $args[0].=$script;
    return $args;
}
This saves you from editing the index.php, but we agree there should be a better way than solving it with jQuery. This could be solved easier if we made another hook to the editable function, and while we're at it, make an additional hook to the block function, which would enable easy overriding as shown in yassines post.

Hope this is helpful enough for now. Let us know what you think.
EugeneTheCoder
Posts: 2
Joined: Wed Apr 05, 2017 3:15 am

Re: Echoing a contents of a block

Post by EugeneTheCoder »

Yes! Thanks a bunch for that answer - that was just what I was looking for :) I can dig from there.
User avatar
wiz
Admin
Posts: 569
Joined: Sat Oct 30, 2010 12:23 am

Re: Echoing a contents of a block without being able to edit it

Post by wiz »

No problem at all, will marking this thread as solved.

On our TO DO list: add some wiki for adding listeners and possibly add more hooks. Thanks for the feedback!
Post Reply