The benefit of drupal_is_front_page function an example

Submitted by Nicola Rainiero on 2015-06-21 (last updated on 2015-06-22)

Thanks to this function I could re-activate the last version of MathJax module that gave me a lot of problem only in the front page of my Drupal 7 website. In the post I will describe how I have fixed this issue using the drupal_is_front_page() function with a few lines of PHP.

When I discovered the MathJax module1 and started to use it in my site, it worked very well, in the settings page (../admin/config/content/mathjax) I chose only the nodes on which this module would work or not. The procedure was completed in one click in the PAGE SPECIFIC ACTIVATION SETTINGS, as following:

Enable MathJax on specific pages:

  • Enable on every page except the listed pages.
  • Enable on the listed pages only.

For more information I suggest you to read my article: MathJax and MathML in my Drupal site. Maybe it was a little complex the adding every time a new node in the admin page, but so I avoided to load in every page another JavaScript library, above all if I don't have maths formulas inside.

The use of drupal_is_front_page function with an example

Case history

However starting from the 2.0 release, this feature has been removed and the library is loaded in every pages as a filter2, front page included, with the following result:

  • Before MathJax 2.0
    Before MathJax 2.0
  • After
    After

Hence I was forced to deactivate it, removing this filter in the text format page (../admin/config/content/formats), until I would have been able to find a solution. And for a while my beautiful formulas were returned at this original LaTeX format:

\[\begin{aligned}
\epsilon &= 3.94 - 8.21 n + 6.23 n^2 \\
K_c &= \bigl( {{10 \phi a} \over {3.6^n \epsilon}} \bigr)^{1 \over {1-n}} {{1} \over {ln {\epsilon \over {\epsilon - 1}}}} \\
u &= \Bigl( {K_c \over {v_0}} \Bigr)^{{1 - n} \over {n}}
\\ Q &= u S
\end{aligned}\]

Instead of:

\[\begin{aligned} \epsilon &= 3.94 - 8.21 n + 6.23 n^2 \\ K_c &= \bigl( {{10 \phi a} \over {3.6^n \epsilon}} \bigr)^{1 \over {1-n}} {{1} \over {ln {\epsilon \over {\epsilon - 1}}}} \\ u &= \Bigl( {K_c \over {v_0}} \Bigr)^{{1 - n} \over {n}} \\ Q &= u S \end{aligned} \]

Therapy

Recently I have found a simple solution to fix quickly this problem: drupal_is_front_page(). It is a PHP function that checks if the current page is the front page, returning a boolean value: TRUE if the current page is the front page; FALSE if otherwise3.

In these cases pragmatic people look the HTML output code of the problematic page and then they decide what to do, but in my case I was sure that the problem had to be localized in the loaded JavaScript library and so I wanted to instruct the ../sites/all/modules/mathjax/mathjax.module file on how to do not load it in the front page, this is the code:

function mathjax_init() {
/** MY CODE **/
if (!drupal_is_front_page()) { // Don't load the JavaScript library on front page
/** MY CODE **/ 
  if(variable_get('mathjax_config_type', 0) == 0) {
    $config_string = mathjax_default('config string');
  }
  else {
    $config_string = variable_get('mathjax_config_string', mathjax_default('config string'));
  }
  // Load Mathjax.
  $config = array(
    '#type' => 'markup',
    '#markup' => '<script type="text/x-mathjax-config">' . $config_string . '</script>',
  );
  drupal_add_html_head($config, 'mathjax-config');
  drupal_add_js(drupal_get_path('module', 'mathjax') . '/mathjax.js');
  if (variable_get('mathjax_use_cdn', TRUE)) {
    $cdn_url = variable_get('mathjax_cdn_url', mathjax_default('cdn url'));
    drupal_add_js($cdn_url, 'external');
  }
  else {
    if (function_exists('libraries_get_path')) {
      drupal_add_js(libraries_get_path('mathjax', TRUE) . '/MathJax.js?config=TeX-AMS-MML_HTMLorMML', 'external');
    }
  }
}
/** MY CODE **/
}
/** MY CODE **/

Obviously it did not work, because looking the HTML of the front page there are a lot of <div> tags without the relative closure </div>4, with MathJax activated:

[...]<div class="tex2jax"><p>It happens that you have to print for job or personal pleasure, one or more posters in a big format and like me you don't use famous commercial software and you aren't a professional illustrator. But how can the color palette of your document fit the request of the typography? This post tries to show a simple conversion of a pdf from RGB to CMYK, using ghostscript library...</span>  </span></div>[...]

With MathJax turned off:

[...]<p>It happens that you have to print for job or personal pleasure, one or more posters in a big format and like me you don't use famous commercial software and you aren't a professional illustrator. But how can the color palette of your document fit the request of the typography? This post tries to show a simple conversion of a pdf from RGB to CMYK, using ghostscript library and imagemagick in a...</span>  </span></div>[...]

''Oh, this is elementary, my dear Watson'' - By omitting the analysis of <p> tag5, I have to instruct the ../sites/all/modules/mathjax/mathjax.module file on how to not add <div class="tex2jax"> only in the front page of my site. Here is the correct code that fixes the issue:

/**
 * Mathjax filter process callback.
 */
function mathjax_filter_process($text, $filter, $format) {
       // MY CODE STARTS
       if (drupal_is_front_page()) {
              return $text;
       }
       else {
       // MY CODE ENDS
              return '<div class="tex2jax">' . $text . '</div>';
       }
// MY CODE STARTS
}
// MY CODE ENDS

So I have discovered one time more that a good practice is to look at the code before making mistakes!

  • 1. MathJax: LaTeX for Drupal is a fast way to integrate the MathJax library into your Drupal site. MathJax is the modern JavaScript-based LaTeX rendering solution for the Internet.
  • 2. Applying filters to text
    Each text format uses filters to manipulate text, and most formats apply several different filters to text in a specific order. Each filter is designed for a specific purpose, and generally either adds, removes, or transforms elements within user-entered text before it is displayed. A filter does not change the actual content, but instead, modifies it temporarily before it is displayed. One filter may remove unapproved HTML tags, while another automatically adds HTML to make URLs display as clickable links. Source: Drupal help page (../admin/help/filter)
  • 3. Source: drupal_is_front_page | path.inc | Drupal 7 | DrupalContrib
  • 4. OK, I didn't read line per line the HTML, I used Meld, a software similar to Diffuse, to highlight the changes between two files: one with the MathJax enabled and the other without it.
  • 5. Probably I will have to check the item "Field can contain HTML": if checked, HTML corrector will be run to ensure tags are properly closed after trimming, in the proper Views admin panel.



Related Content:

Nicola Rainiero

A civil geotechnical engineer with the ambition to facilitate own work with free software for a knowledge and collective sharing. Also, I deal with green energy and in particular shallow geothermal energy. I have always been involved in web design and 3D modelling.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.