It's good practice to put external libraries in the libraries folder and use hook_library to fetch it. If people want to upgrade the plugin they don't have to update the module.

Comments

Anonymous’s picture

I was confused about how to include the library with the module and still use the hook_library functionality. Is this possible? The reason why is because I think it would definitely be needed to make the Categories be useful, so making it optional, while possible, is not what I'd want by default (I don't want everyone to have to download the separate library file and upload it separately).

In Drupal 8 this won't be an issue, since it would hopefully be in Core anyways.

What are your thoughts?

Sincerely,
Leighton Whiting

aspilicious’s picture

Yes you can use hook_library when the files are included. See http://api.drupal.org/api/drupal/modules--system--system.api.php/functio... for more examples.

You can than load all the js and css code with: drupal_add_library(your-module, library-name);

I don't like big inline js blocks:

  $js = "
(function ($) {
  $('#edit-categories').multiselect({
    noneSelectedText: '" . t('Choose') . "...',
    selectedList: 99,
    minWidth: 500,
    position: {
      my: 'right top',
      at: 'right bottom'
    }
  });
})(jQuery);
";
  drupal_add_js($js, array('type' => 'inline', 'scope' => 'footer'));

I think you should put this into a seperate js file. This is a perfect example for drupal jquery behvaiours. This also should be part of a library.

aspilicious’s picture

You can read more info on this page: http://drupal.org/node/756722

Anonymous’s picture

But using hook_library, while good, would still require the code to be stored in the module folder. I'll change it now so that it uses hook_library and drupal_add_library.

I was thinking you wanted the code to be stored in the libraries folder.

Sincerely,
Leighton Whiting

Anonymous’s picture

Also, I wanted to put the block of js in it's own file, but it needs the t() function. Not sure how to do that only with js. Is that possible?

Sincerely,
Leighton Whiting

aspilicious’s picture

You can use Drupal.t in javascript see http://zoo2rock.wordpress.com/2010/06/01/the-drupal-t-function-client-si...
Yes I was thinking of moving it outside the project but making a hook_library is also good.

Anonymous’s picture

Okay, I've moved the code to a library and also moved the js block to it's own file and used Drupal.t. Thanks for your help :)

Sincerely,
Leighton Whiting

aspilicious’s picture

Hmm...

drupal_add_library('project_browser', 'jquery.multiselect');
drupal_add_js(drupal_get_path('module', 'project_browser') . '/js/project_browser_more_link.js', array('scope' => 'footer'));
drupal_add_js(drupal_get_path('module', 'project_browser') . '/js/jquery.multiselect.min.js');
drupal_add_js(drupal_get_path('module', 'project_browser') . '/js/project_browser_multiselect.js', array('scope' => 'footer'));

1) You don't need the add_js for jquery.multiselect anymore.
2) Is there a reason for the 'scope' => 'footer' ?

3) You could merge these two files in a seperated library in hook_library or you could merge the files. (if you like to cut down the drupal_add_js calles

4) You should look at how core handles js files. You could do the folowing


(function ($) {

// First jquery code
Drupal.behaviors.projectBrowserMoreLink = {
  attach: function (context, settings) {
    // jquery code
  }
}

// Second jquery code
Drupal.behaviors.projectBrowserMultiselect = {
  attach: function (context, settings) {
    // jquery code
  }
}

})(jQuery);

Anonymous’s picture

Aspilicious,

1) You are right, I've removed this now.

2) Not anymore, it was something I did earlier because I didn't use the document.ready function. I've switched to that now.

3) I think I will merge the two scripts into one file since it is only used on the one page. Should I then add it in hook_library and just use hook_library on it, using the other library as a dependent? I was under the impression that libraries were mostly meant for when code could be reused other places. In this case, the code is pretty specific to just this page, so would it warrant a new library?

4) I've never done anything with Drupal Behaviors, so I'm a bit of a noob here in that regard. Can you explain what the benefits to using this would be?

Thanks!

Sincerely,
Leighton Whiting

Anonymous’s picture

Issue summary: View changes

Drupal behaviors is definitely the way to go in my opinion. Also, I'm not entirely sure we need to even use this special library. It would be nice to get rid of it so we don't have as many moving parts. I'll look into that.

Additionally, most of the icons could probably be replaced with the core jQuery UI ones.

rlnorthcutt’s picture

Status: Active » Closed (outdated)

7.x branch is deprecated and archived. Closing.