Search code examples
pyrocms

Custom output for session:messages?


In PyroCMS, I'm using the session:messages tag to display messages to the user. This is working well, but I'd like to add a close button to each message which requires placing a span within each message. For example:

<div class="alert success">
  You have logged in successfully. <span class="close">X</span>
</div>

Each message is wrapped in a div, which is given a class by setting an attribute of the session:messages tag. There is no built-in way to specify the output. How can I override the messages() function in session.php, adding a new attribute to append this close button?

So far I have tried:

  1. Copying system/cms/plugins/session.php to *addons/shared_addons/plugins/session.php* and modifying the messages() function. The core function is used, rather than the new plugin as I had hoped.

  2. Copying the plugin as described above, and then changing it's class to My_Plugin_Session extends Plugin_Session in hopes that its functions would then override the core Plugin_Session class. No luck.


Solution

  • It's not possible to extend certain things that are in the core (e.g. libraries and helpers) - I think this applies to plugins too.

    In this case, if I was you (and I may well have to do this for my next project as these closable alerts are typically in Twitter Bootstrap etc.) I'd just edit /system/cms/plugins/session.php directly and add the extra <span> for the close button to the success, notice and error conditionals ('if' statements).

    On a typical site I can't think of a situation where you'd ever need some alerts displayed differently to others (other than different colours depending on the outcome of course, which you can do in the CSS using the class name).

    Providing you're using Git (you've cloned or forked the official PyroCMS repository and made your own branch) you'll be absolutely fine with future updates - if in a future version the session plugin changes, any changes will just be merged into your code automatically, or if Git can't figure it out, it'll show you the differences and prompt you to fix it by hand.

    Note - there are a couple of other solutions for this specific problem based on the admin interface (you may have noticed the flash messages there are closable).

    You could create a partial - which can contain PHP, not just Lex tags - e.g. see /system/cms/themes/pyrocms/views/admin/partials/notices.php with something like this (edit as needed and duplicate for notice and error):

    <?php if ($this->session->flashdata('success')): ?>
    <div class="alert success">
        <?php echo $this->session->flashdata('success'); ?>
    </div>
    <?php endif; ?>
    

    PyroCMS admin actually uses liveQuery to append the close button <span> in the browser
    Source: /system/cms/modules/wysiwyg/js/wysiwyg.js:

    // Add the close link to all alert boxes
    $('.alert').livequery(function(){
        $(this).prepend('<a href="#" class="close">x</a>');
    });