1

I'm using drupal 7 to develop a module.

This module has a menu with a page callback 'page callback' => 'my_function' and the function has to return HTML.

In the function I'm returning array('#markup' => $markup); where $markup has HTML with shortcodes: [button path="node/2" (class="additional class")]Order now[/button].

The shortcodes are appearing directly in the browser without transformation. I created a page on the web administration page with the same content and the shortcodes are transformed.

How can I return HTML that has shortcodes from a module?

  • 3
    Shortcodes are an input filter I think, so run the string through check_markup with a formatter that’s configured to process shortcodes, eg: check_markup($markup, ‘full_html’) – Clive Mar 16 '18 at 18:14
  • It doesn't work with "full_html". I tried with "filtered_html" and it translates the shortcode but it breaks other html (a <table> in the case). – paf.goncalves Mar 16 '18 at 21:29
0

Shortcodes are processed with a filter, so the most reliable way to get what you need would be:

  1. Create a new input format (shortcodes_only or something)
  2. Enable and configure the shortcodes filter for that format, but disable any HTML or other processing that you don't want to apply to your string
  3. Process the string before adding it to the render array like so:

    $markup = check_markup($markup, 'shortcodes_only');
    

If you need to do it programatically it's a bit of a hack, as the shortcode processing code isn't completely decoupled from the filter concept. You need to mock a filter object and give it a couple of properties (a dummy format ID and an array of shortcodes keyed by ID, with a boolean value indicating each shortcode's status).

This is untested but should get you close:

$filter = new stdClass();
$filter->format = 'not_a_real_format';
$filter->settings = array_fill_keys(array_keys(shortcode_list_all()), 1);
$markup = _shortcode_process($markup, $filter);

If you choose the programmatic method, there's something else to bear in mind: by convention, any function whose name begins with an underscore is considered to be private to the module, and its implementation is subject to change in future releases without warning (potentially breaking your code in unpredictable ways). It's probably not that likely in this case, but you never know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.