Drupal 7; How to assign custom CSS and JS by node

If you need to have custom CSS or JS for a node, it is easy to set up.   This can be setup via the template.php file.   To customize available CSS or JS via template.php, modify the THEME_preprocess_node.  For example, if you have a special node type called “directory” in a theme called “boulder,” add the following code to your boulder_preprocess_node function.



  function boulder_preprocess_node(&$variables) {
    if ($variables['view_mode'] == 'full') {
      $node =& $variables['node'];
      if ($node->type == 'directory') {
        $path = drupal_get_path('theme', 'boulder');
        drupal_add_css($path . '/CSS/directory.css');
        drupal_add_js($path . '/js/directory.js', 
            array('type' => 'file', 'scope' => 'header'));
      }
    }
  }

I love this. It’s quick, easy and very useful.

Magic: How do you get Drupal 7 Views to expose filters in blocks?

Have you found that you created a view that needs a block with exposed filter, but, the exposed filter don’t show up?? There is magic that makes the “exposed” filter display. Exposed filters automatically display when selected on view pages, but, not on blocks.

Recently I needed to create a view block (via Views 3) with exposed filter. I defined a view page with an exposed filter. It worked great and I moved on to the Block. The exposed filter did not display in the block … What’s going on???? Well, I found how to create the block with exposed filter.

Solution:

To create view block in Views 3 … start fresh

  • Select to create a NEW view
  • Unclick the “Create a Page.” Currently, View 7x-3.7 requires you not create a Page when you require a Block to expose a filter in the block.
  • Click “Create a Block”
  • Define you block, select to expose your filter.
  • Voila!!

5 Steps to Adding Sidebar Block in Zen-Cart

You may find that you want to add a block to the sidebar in Zen-Cart. The content may be text, image, or other feature to extend the content on the cart. The generic concept of adding a block involves 5 steps. This article explains the steps. For simplicity, we will only add a text block.

Step 1; Create Sidebox Template

Add a new file to the server in the following directory: /includes/templates/<template-name>/sideboxes/. Name the file something relating to the contents of the sidebox. We will be adding a support phone number to call in a sidebox, so, we might name the file tpl_support_number.php. The file will include what you want to display in the box and will be assigned to the variable $content variable. Below is an example of the contents of the file.


  <?php
     /* Side Box Template  */
     $content = "For information about products or services, call (888) 555-1212" ;
  ?>

Step 2; Create Sidebox

Now, to create the actual box; add a new file to the server in the following directory: /includes/modules<template-name>/sideboxes. Name the file something relating to the contents of the sidebox. In our sample, we will call it support_number.php. The contents of this file will look like the sample below, but, modify the variables to relate to your template.


  <?php
  /**
   *  Support Number Sidebox - used to display a support message 
   *  in a sidebox.
   *
   */
   $show_message = true ;
   if ($show_message == true) {
       require(
         $template->get_template_dir('tpl_support_number.php', 
            DIR_WS_TEMPLATE, $current_page_base,
           'sideboxes') . '/tpl_support_number.php');
       $title = BOX_HEADING_SUPPORT_NUMBER ;
       $title_link = false;
       require($template->get_template_dir($column_box_default, 
           DIR_WS_TEMPLATE, $current_page_base,'common') . '/' . 
           $column_box_default);
   }
  ?>

Step 3; Add Language Variables

Modify the english.php language file in /includes/languages/ to include the definitions for any text variables you have used in either file. In this example we created a new variable called BOX_HEADING_SUPPORT_NUMBER. So, we will add the following to the english.php file.


  // welcome box text in sideboxes/welcome.php
  define('BOX_HEADING_SUPPORT_NUMBER', 'SUPPORT INFO');

Step 4; Enable Sidebox:

Activate the sidebox as follows:

  • Login into Zen-Cart administration.
  • Hover over Tools.
  • Select “Layout Boxes Controller” from the drop down, a list of boxes will appear …  including your new box
  • Double-click the name of the new box you have created, in this sample, it will say …  sideboxes//support_number.php. In the upper right of the window you will see fields that you can edit.
  • Select to edit the information, an editable form will appear
  • Fill in the form, by:
    • Selecting the radio button next to “on” in “Left/Right Column Status”
    • Selecting the radio button to place the box on the “left” or “right” sidebar
    • Typing the correct number is the sort order field to display the box
    • and then clicking the Update button.

The sidebox is now enabled on the site

Step 5; Verify Sidebox:

Now, visit your store in a browser and make certain the sidebox displays.

 

Drupal 7: stop insertion of <br/> in formated text

Folks,

If you are using Drupal 7 with full HTML enabled, you may notice that you begin having <br/> tags inserted in your HTML in locations that you do not want these tags. To prevent this, go to:

— admin/config/content/formats/full_html

Under “Enabled filters,” unclick “Convert line breaks into HTML (i.e. <br> and <p>).

Hopefully, this will resolve your problem.

Drupal 7 filter on change

There are several location that I offer to filter lists in Drupal 7. These locations tend to require that you select an option from a dropdown list, then, click a button that says “Filter”, “Apply” or other text. This is not a problem, but, some interfaces should just execute when the filter is selected, without clicking an apply button.

As an example, you may create a view that exposes a filter to the page a viewer will see. The visitor may want to select a filter and have the filter applied without having to do a second step of “applying” the filter. This may be accomplished by a quick chunk of jQuery. This chunk may be added as a module, drupal_add_js or simply added to an invisible block on the page.

For demonstration purposes, assume that you have a select filter with the id=edit-field-business-category-tid. To activate this filter on change of the selection use:


 jQuery("#edit-field-business-category-tid").change(function() {
     jQuery(this).parents("form").submit() ;
 }) ;