Quantcast
Viewing all articles
Browse latest Browse all 294759

theme_disable() can disable the wrong theme

While writing an update function for a Drupal distribution I found that calling theme_disable() on a list of themes caused unexpected results.

The update function should disable every theme except for the default theme:

<?php
/**
* Disable unnecessary themes.
*/
function distribution_update_xxxx() {
 
$theme_list = array_keys(list_themes());
 
// theme_disable() ensures that the default theme will not be disabled.
 
theme_disable($theme_list);
  return array();
}
?>

However, instead of the non-default themes being disabled I found that all themes were disabled!

Internally theme_disable() checks to see if the default theme is in the array of themes to be disabled and, if it is, removes it. It does this by searching through the array of provided themes, finds the array index of the default theme and then unsets the array element at that index.

<?php
 
// Don't disable the default theme.
 
if ($pos = array_search(config('system.theme')->get('default'), $theme_list) !== FALSE) {
    unset(
$theme_list[$pos]);
    if (empty(
$theme_list)) {
      return;
    }
  }
?>

But I found that $pos wasn't getting the correct position:

<?php
The
default theme is currently: commons_origins

Array
(
    [
0] => adaptivetheme
   
[1] => adaptivetheme_admin
   
[2] => adaptivetheme_subtheme
   
[3] => bartik
   
[4] => commons_origins
   
[5] => garland
   
[6] => seven
   
[7] => sky
   
[8] => stark
)

commons_origins was located at position 1 and was removed

Array
(
    [
0] => adaptivetheme
   
[2] => adaptivetheme_subtheme
   
[3] => bartik
   
[4] => commons_origins
   
[5] => garland
   
[6] => seven
   
[7] => sky
   
[8] => stark
)
?>

It seems that the conditional gives execution order priority to the !== operator. !== has higher precedence than = so the variable assignment should either occur separately or parentheses should be added around the assignment.


Viewing all articles
Browse latest Browse all 294759

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>