Category: Drupal

  • Acquia Dev Desktop – Sharing your local site

    If you’re developing a Drupal site locally, using Acquia Dev Desktop 2, and you need to allow your co-workers to see it, you’ll need to make the following change to your vhosts.conf (located in devdesktop\apache\conf\vhosts.conf)

    <VirtualHost *:8443>
      ServerName test.dev.dd
      SSLEngine on
      DocumentRoot "/Users/test/Sites/devdesktop/test-dev/docroot"
      <IfModule fcgid_module>
        FcgidInitialEnv PHPRC "/Applications/DevDesktop/php5_5/bin"
        FcgidWrapper "\"/Applications/DevDesktop/php5_5/bin/php-cgi\""
      </IfModule>
      <Directory "/Users/test/Sites/devdesktop/test-dev/docroot">
        Options Indexes FollowSymLinks ExecCGI
        AllowOverride All
        Require local
      </Directory>
    </VirtualHost>

    Replace “Require local” with “Require all granted”, then stop and restart Apache/MySQL from within Dev Desktop.

     

  • Drupal Block Region in Node Template

    If you want to display a block region in a specific node template, simply add the following:

    <?php
    $myregion = block_get_blocks_by_region('myregion');
    print render($myregion);
    ?>

     

  • Using Drupal Rules Scheduler to Unpublish a Node

    I use the Rules module together with Rules Scheduler (a Rules sub-module) to automatically unpublish content at a specified time, as explained in the steps below. These steps use content type = article, which has an expiry date field which is set by the user. When cron runs, the expiry date field is checked and the content is unpublished.

    Step 1: Create a Rules “Component”

    Here is the Rules Component to be created (in Rules export format):

    { "rules_unpublish_a_node" : {
        "LABEL" : "Unpublish an Article",
        "PLUGIN" : "action set",
        "OWNER" : "rules",
        "REQUIRES" : [ "rules" ],
        "USES VARIABLES" : { "node" : { "label" : "node", "type" : "node" } },
        "ACTION SET" : [ { "node_unpublish" : { "node" : [ "node" ] } } ]
      }
    }

     

    Step 2: Create a “Rule” using the Rules Component

    Here is the Rule to be created, in Rules export format:

    { "rules_unpublish_article" : {
        "LABEL" : "Unpublish Article",
        "PLUGIN" : "reaction rule",
        "OWNER" : "rules",
        "REQUIRES" : [ "rules", "rules_scheduler" ],
        "ON" : {
          "node_insert" : [],
          "node_update--article" : { "bundle" : "article" }
        },
        "IF" : [
          { "node_is_of_type" : {
              "node" : [ "node" ],
              "type" : { "value" : { "article" : "article" } }
            }
          }
        ],
        "DO" : [
          { "schedule" : {
              "component" : "rules_unpublish_a_node",
              "date" : [ "node:field-expiry-date" ],
              "identifier" : "Unpublish Article with node ID [node:nid]",
              "param_node" : [ "node" ]
            }
          }
        ]
      }
    }

    Note that the above rule refers to the Rules Component from the previous step. It’s in Rules export format, just import it in your own environment via copy-paste (after you adapted article in it to fit the machine name of your content type).

  • 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.

  • How to add static HTML pages in Drupal

    Sometimes you just need a static HTML page, or a template-less page, in your CMS. There are many ways to do this with Drupal, like creating node specific page templates, with all of the container HTML chopped out.

    However, the quickest and easiest way is to use the Static Page Module

    After installation, do the following:

    • Create a new Content Type, maybe call it “Static HTML”, make sure it has a “Body” field
    • In /admin/config/content/static_page, select the new content type that you created
    • Now create some content!
  • Include node specific JS/CSS in Drupal

    function YOURTHEME_preprocess_page(&$variables) {
    
      //add assets for specific node 
      if ($variables['node']->nid == 12781) {
        drupal_add_css(drupal_get_path('theme', 'YOURTHEME') . "/css/foo.css");
        drupal_add_js(drupal_get_path('theme', 'YOURTHEME') . "/js/bar.js");
      }  
    }
  • Drupal: How to apply the img-responsive class to all images in your bootstrap subtheme

    UPDATE: This is now included by default and should not be used.

    In your subtheme’s template.php file, add the following:

    function YOURTHEMENAME_preprocess_image(&$vars)
    {
        $vars['attributes']['class'][] = 'img-responsive';
        // http://getbootstrap.com/css/#overview-responsive-images
    }