$ is not a function – jQuery

Once in a while, as a web developer working on a project that is based on a Drupal, you may come across this issue especially when utilizing the jQuery javascript library:

TypeError: $ is not a function

The simple solution is to create a wrapper so that Javascript is compatible with other libraries other than jQuery. As stated in the drupal.org website:

This wrapper is an anonymous closure that provides state throughout the page’s lifetime and ensures your code does not unintentionally create/override global variables.

It also explicitly imports the global jQuery variable so that your code can use the local $ variable instead of the jQuery global. This is essential because Drupal loads jQuery with noConflict() compatibility so the jQuery library does not setup the normal $ as a global variable.

 

Reference:

  • http://drupal.stackexchange.com/questions/97061/getting-typeerror-is-not-a-function-with-or-without-jquery-update-install

March 13, 2015

Posted In: Information Technology

Tags: ,

Leave a Comment

Undefined Variable: title

Notice: Undefined variable: title in include() (line…..

I came across the aforementioned issue while developing a custom Drupal template from scratch; I will post a link to the final work later on.

The consequence was that the block’s title was not displayed even if the title has been set in the admin panel. This issue normally arises from block.tpl.php and can be solved by using the following Drupal HTML5-based code (original Drupal block template code):

<section id="<?php print $block_html_id; ?>" class="<?php print $classes; ?>"<?php print $attributes; ?>>
  <?php print render($title_prefix); ?>
  <?php if ($block->subject): ?>
    <h2<?php print $title_attributes; ?>><?php print $block->subject ?></h2>
  <?php endif;?>
  <?php print render($title_suffix); ?>
  <section class="content"<?php print $content_attributes; ?>>
    <?php print $content ?>
  </section>
</section>

Reference:
https://www.drupal.org/node/1515344

July 14, 2014

Posted In: Information Technology

Tags: , ,

Leave a Comment

Display $tabs based on Role Type

drupal_branding_2012I came across an issue today whereby questions created by a certain user were editable by other users. The ‘view’ and ‘edit’ tabs were visible but the preference was that they would only be visible to the creator of the question. I checked the permissions page (admin/people/permissions) but everything was okay since each role could edit its own content but not all content.

To solve this problem I made use of the following code:

[php]
global $user;
if ($node->;uid == $user->uid || in_array(‘administrator’,$user->roles)){
if ($tabs = render($tabs)): ?>;

<!–?php endif; } ?–>;
[/php]

This code simple says the tabs will be displayed if the current logged in user is the creator of the question node or is the super administrator. This code can be used for other kinds of content types.

April 28, 2014

Posted In: Information Technology

Tags: ,

Leave a Comment

Create Node Template

drupal_branding_2012By default, all Drupal themes have a default node template. But you can create individual custom node templates based on the content type. Creating one is quite easy. Just duplicate the node.tpl.php file and rename it by adding the machine name of the content type i.e.

node–machinename.tpl.php

For example if your content type is Resources then the machine name will be resources:

node–resources.tpl.php

Please note the two hyphens that precede the machine name.

For further reading:

https://drupal.org/node/17565

https://drupal.org/node/1089656

April 28, 2014

Posted In: Information Technology

Tags: ,

Leave a Comment

Custom page.tpl.php Template

How do you create a custom page template based on content type for a Drupal 7 site? Simply start from the page.tpl.php, the Drupal default page template. The process of creating a custom page template involves:

  1. Renaming the template file
  2. Creating a pre-processor function
  3. Clearing cache

Renaming the template file

In this step, you add the machine name of the content type to the file name i.e. page.tpl.php:

page–content-type-machine-name–.tpl.php

Create Pre-processor Function

This function is placed in template.php as follows:

[php]
function themename_preprocess_page(&$vars, $hook) {
if (isset($vars[‘node’]->type) && !empty($vars[‘node’]->type) && ($vars[‘node’]->type == ‘content_type_machine_name’)) {

$vars[‘theme_hook_suggestions’][] = ‘page__’ . $vars[‘node’]->type;
}
}
[/php]

Replace the ‘themename‘ and ‘content_type_machine_name ‘with your own values i.e. the name of your theme and the content type respectively.

Clear Cache

Clear cache under admin/config/development/performance to allow for the reloading of template file.

April 23, 2014

Posted In: Information Technology

Tags: ,

Leave a Comment

Disable Drupal Admin Overlay

The admin back-end of Drupal 7 usually makes use of an overlay to display administration content. The overlay is an admin page that displays above the main web page, a way of reducing distraction and make it look ‘hip’; administration doesn’t have to be boring. But for my case it was a distraction because at times the overlay does not load after clicking a link. For instance, I was trying to rebuild permissions for node access permission but this action was rendered incomplete during the process because the overlay page did not load fully.

Solution: I simply disabled the overlay module under the modules page. The module is found under the core modules section. Of course I was able to rebuild permissions without a hitch. Some things are just good for aesthetics; but get rid of them if they do not offer any productive value.

March 13, 2014

Posted In: Information Technology

Tags: ,

Leave a Comment

Remove ‘Add Comment’ Custom Module

drupal_branding_2012One of the advantages of using Drupal as a CMS is the ability to extend its core functions using modules that can be either downloaded from the main Drupal site (contributed modules) or created locally/in-house for custom functions. It also allows for the creation of custom themes that can meet the needs of a client or the main users of the site.

I recently created a custom site for a client using Drupal as the foundation. The child theme I made was based on the Twitter Bootstrap theme and along the way I discovered that I needed to make use of the template.php (pre-processor functions) and custom modules to modify some core functions of Drupal. One thumb rule when developing any web project based on a CMS is to never modify the core files. This is because updates for the various CMSs come in every now then and its the core files that need to be updated. If you modify any of the core files two things can happen:

Continue reading Remove ‘Add Comment’ Custom Module

February 6, 2014

Posted In: Information Technology

Tags: ,

Leave a Comment