Month: July 2016

  • Drupal: Get full image URL including style name and itok

    $imageURL = image_style_url('YOUR_STYLE_NAME', $content['field_image']['#items'][0]['uri']);
  • Drupal remove white-space from HTML

    Add this simple function into your theme’s template.php in order to strip out unnecessary whitespace and line breaks, saving a bit of file size where possible (I saved 17kb from my homepage). If a page contains a <pre> or <textarea> tag, it is left alone, as to not affect code blocks or text typed in a form.

    // Minify HTML output.
    function sonspring_process_html(&$vars) {
    $before = array(
    "/>\s\s+/",
    "/\s\s+</",
    "/>\t+</",
    "/\s\s+(?=\w)/",
    "/(?<=\w)\s\s+/"
    );

    $after = array('> ', ' <', '> <', ' ', ' ');

    // Page top.
    $page_top = $vars['page_top'];
    $page_top = preg_replace($before, $after, $page_top);
    $vars['page_top'] = $page_top;

    // Page content.
    if (!preg_match('/<pre|<textarea/', $vars['page'])) {
    $page = $vars['page'];
    $page = preg_replace($before, $after, $page);
    $vars['page'] = $page;
    }

    // Page bottom.
    $page_bottom = $vars['page_bottom'];
    $page_bottom = preg_replace($before, $after, $page_bottom);
    $vars['page_bottom'] = $page_bottom . drupal_get_js('footer');
    }

  • Exclude Drupal system CSS for logged-out users

    In your template.php, you can exclude Drupal’s system CSS files, or any CSS file that wasnt added by your theme (this includes CSS added by modules) using this PHP snippet:

    function YOURTHEME_css_alter(&$css) {
      if (!user_is_logged_in()) {
        foreach ($css as $key => $value) {
          if ($value['group'] != CSS_THEME) {
            $exclude[$key] = FALSE;
          }
        }
        $css = array_diff_key($css, $exclude);
      }
    }

     

  • Drupal homepage only CSS

    So you want to have a base.css file on all pages, then a homepage.css file just for the homepage, and non-homepage.css for all other pages? Here’s how:

    In your themes’ .info file, include the base.css file:

    stylesheets[all][] = css/base.css

    In your themes’ template.php file:

    function YOURTHEME_preprocess_page(&$vars) {
      //
      // home/non-home specific css
      //
      $homepage_css_options = array(
        'type' => 'inline',
        'scope' => 'header',
        'group' => CSS_THEME,
        'weight' => 9999,
        'preprocess' => FALSE,
      ); 
      $nothomepage_css_options = array(
        'type' => 'file',
        'scope' => 'header',
        'group' => CSS_THEME,
        'weight' => 9999,
        'preprocess' => TRUE,
      );      
    
      if ($vars['is_front'] == TRUE) {
        //inline the homepage css, minified by Gulp
        drupal_add_css(file_get_contents(drupal_get_path('theme', 'YOURTHEME') . '/css/homepage.min.css'), $homepage_css_options);
      } else {
        //inline css file for all pages except homepage
        drupal_add_css(drupal_get_path('theme', 'YOURTHEME') . '/css/not-homepage.css', $nothomepage_css_options);
      } 
    
    }

    Note: In the code above I have chosen to inline the homepage only CSS.