SlideShare a Scribd company logo
jQuery UI in Drupal 7
              Darren Mothersele @mothersele
             http://www.darrenmothersele.com




Ivan Sutherland's Sketchpad system is demonstrated on the console of the TX-2 at MIT (1963)
jQuery UI in Drupal 7

• Using Javascript to improve UI
• Javascript in Drupal 7
• jQuery UI widgets
• Building complex interactions
HTML + Javascript

• HTML defines a set of standard form elements
• Javascript allows us to build new interactive widgets
• jQuery UI provides widgets, interactions and effects
Javascript widgets

• Reduce errors in data entry
• Quicker/more efficient
• More engaging/fun
• ...
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
Progressive Enhancement

 • Create widget using jQuery
 • Hide old widget
 • Fill in value in the background
 • Transparent to Drupal, nice and safe
 • Non-js friendly
Before Active Tags


• Multiple tagging methodologies
• Some people just expect to use spaces
• http://drupal.org/node/91074
• Character-delimited system
Active Tags


•   Action-delimited system
•   Autocomplete
•   Original widget hidden
•   http://drupal.org/project/active_tags
Javascript in Drupal 7

• Theme or Module?
• Scope?
• Module specific or reuseable?
  (Javascript Library)
• How and where to include code?
theme.info
name = My theme
description = Theme developed by me.
core = 7.x
engine = phptemplate
scripts[] = my_script.js
drupal_add_js($data, $options)

 $data is either:
  • path to Javascript file to include
  • Javascript code to include ‘inline’
  • absolute path to external JS code
  • array of settings for Javascript
 $options includes type, location, caching, ...
hook_preprocess_page()
function mytheme_preprocess_page(&$vars, $hook) {

    if (true) {

        drupal_add_js(
          drupal_get_path('theme', 'mytheme') . '/scriptname.js',
          'theme');

        $vars['scripts'] = drupal_get_js();

    }

}
hook_library()

• Used in Core for jQuery UI
• Use to include other third-party plugins
• Define your own reusable Javascript
jQuery UI Buttons
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
Dialog Example
function dproj_form_user_login_block_alter(&$form, $form_state) {

    drupal_add_library('system', 'ui.dialog');

    $dialog_js =
      '$("#block-user-login").dialog({title: "User login"});';

    $dialog_js =
      'jQuery(document).ready(function () { (function ($) {' .
      $dialog_js . '}(jQuery)); });';

    drupal_add_js($dialog_js,
      array('type' => 'inline', 'scope' => 'footer',
        'weight' => 5));

}
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
id="edit-field-issue-type-und"



$("#edit-field-issue-type-und").buttonset();
drupal_add_library()
function dproj_form_issue_node_form_alter(&$form, $form_state,
$form_id) {
    $language = $form['#node']->language;
    $form['field_issue_type'][$language]['#after_build'][] =
                                         '_dproj_button_helper';
}

function _dproj_button_helper($element, &$form_state) {
    $button_js = '$("#'. $element['#id'] .'").buttonset();';
    $button_js = JS_PREFIX . $button_js . JS_SUFFIX;
    drupal_add_library('system', 'ui.button');
  drupal_add_js($button_js, array('type' => 'inline', 'scope' =>
'footer', 'weight' => 5));
    return $element;
}
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
https://github.com/padolsey/jQuery.fn.autoResize
hook_library()
function dproj_library() {

    $path = drupal_get_path('module', 'dproj');

    return array('autoresize' =>     array(

      'title' => 'AutoResize',

      'website' => 'https://github.com/...'

      'version' => '1.14',

      'js' => array(

           $path .'/jquery.autoresize.js' => array(),

      ),

    ));

}
id="edit-field-project-desc-und-0-value"




$("#edit-field-project-desc-und-0-value").autoResize();
function dproj_form_project_node_form_alter(&$form,
$form_state, $form_id) {

    $language = $form['#node']->language;

    $form['field_project_desc'][$language]['#after_build'][]

      = '_dproj_autoresize_helper';

}

function _dproj_autoresize_helper($element, &$form_state) {

    $id = $element[0]['value']['#id']

    $autoresize_js = '$("#'. $id .'").autoResize();';

    $autoresize_js = JS_PREFIX . $autoresize_js . JS_SUFFIX;

    drupal_add_library('dproj', 'autoresize');

    drupal_add_js($autoresize_js, array('type' => 'inline',
      'scope' => 'footer', 'weight' => 5));

    return $element;

}
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
Building a more
  complex interaction
• Drupal Behaviors
• jQuery ui.draggable
• jQuery ui.droppable
• jQuery AJAX
• Contrib modules:
  Page manager (ctools), Rules, Relation
Behaviors
• Replacement for $(document).ready();
• Use attach function:
   (function ($) {

     Drupal.behaviors.exampleModule = {
       attach: function (context, settings) {

              $('.dproj', context).draggable();

          }
     };

   }(jQuery));

• AJAX safe
• Detachable
Drag and Drop
Draggable


                          Droppable




               $.ajax()
    Callback
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
.dproj-draggable




              .dproj-droppable



$('.dproj-draggable').draggable();
$('.dproj-droppable').droppable();
path1 =
      add-attendee/[uid]




           path2 =
            /[nid]



     callback = path1 + path2
callback = add-attendee/[uid]/[nid]
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
Callback argument
inserted into header
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
<span class='callback'>
  add-attendee/1
</span>




   <span class='callback'>/20</span>




callback = add-attendee/1/20
Drupal.behaviors.dprojdrag = {
  attach: function (context, settings) {
    $('.dproj-draggable', context).draggable({revert: 'invalid'});
    $('.dproj-droppable', context).droppable({
      hoverClass: 'drop-hover',      accept: '.dproj-draggable',
      drop: function (e, ui) {
        $(ui.draggable).hide();
        $(e.target).fadeTo('fast', 0.5);
        var view_id = '.' +
          $(e.target).attr('class').match(/view-dom-id-d+/)[0];
        var href = Drupal.settings.basePath +
          $('.callback', ui.draggable).html()
          + $('.callback', e.target).html();
        $.ajax({ url: href,
                 success: function (data) {
                 $(view_id).html($(view_id, $(data)));
                  $(view_id).fadeTo('fast', 1);
}});}});}};
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
Resources
•   jQuery UI                 •   Contrib Modules
    http://jqueryui.com/
    demos                         •   Views

•   Managing Javascript in        •   Relation
    Drupal 7
    http://drupal.org/node/
    756722
                                  •   Page manager

                                  •   Rules

More Related Content

PDF
Drupal Step-by-Step: How We Built Our Training Site, Part 1
PDF
Your Entity, Your Code
ZIP
Drupal Development (Part 2)
PDF
Desarrollo de módulos en Drupal e integración con dispositivos móviles
PPTX
Build your own entity with Drupal
PDF
Virtual Madness @ Etsy
PDF
jQuery secrets
DOCX
Php update and delet operation
Drupal Step-by-Step: How We Built Our Training Site, Part 1
Your Entity, Your Code
Drupal Development (Part 2)
Desarrollo de módulos en Drupal e integración con dispositivos móviles
Build your own entity with Drupal
Virtual Madness @ Etsy
jQuery secrets
Php update and delet operation

What's hot (18)

PDF
Jquery In Rails
PPTX
15. CodeIgniter editarea inregistrarilor
PPTX
DrupalCamp Foz - Novas APIs Drupal 7
PPTX
Magento Dependency Injection
ZIP
Django at the Disco
PDF
Django at the Disco
PDF
Building iPhone Web Apps using "classic" Domino
PPTX
8. vederea inregistrarilor
PDF
Coding website
PPTX
HirshHorn theme: how I created it
PDF
Command-Oriented Architecture
PPT
Propel sfugmd
PPT
Mysocial databasequeries
PPT
Mysocial databasequeries
PDF
Pagination in PHP
PDF
Add edit delete in Codeigniter in PHP
TXT
Daily notes
PDF
Country State City Dropdown in PHP
Jquery In Rails
15. CodeIgniter editarea inregistrarilor
DrupalCamp Foz - Novas APIs Drupal 7
Magento Dependency Injection
Django at the Disco
Django at the Disco
Building iPhone Web Apps using "classic" Domino
8. vederea inregistrarilor
Coding website
HirshHorn theme: how I created it
Command-Oriented Architecture
Propel sfugmd
Mysocial databasequeries
Mysocial databasequeries
Pagination in PHP
Add edit delete in Codeigniter in PHP
Daily notes
Country State City Dropdown in PHP
Ad

Similar to jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript (20)

PDF
JQuery In Drupal
ZIP
What's new in the Drupal 7 API?
PPTX
Drupal 8 migrate!
PPT
Drupal Javascript for developers
PPTX
Taming that client side mess with Backbone.js
PDF
Your Entity, Your Code
ZIP
Learning the basics of the Drupal API
PDF
Drupal Module Development
PDF
Drupal Module Development - OSI Days 2010
PPTX
Debugging in drupal 8
PPTX
Routing in Drupal 8
PDF
Drupal & javascript
PPTX
Javascript first-class citizenery
PDF
international PHP2011_Bastian Feder_jQuery's Secrets
PPTX
Jquery fundamentals
PPTX
Php on the desktop and php gtk2
PDF
Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011
PDF
Building Large jQuery Applications
PPTX
How I Learned to Stop Worrying and Love jQuery (Jan 2013)
PDF
jQuery secrets
JQuery In Drupal
What's new in the Drupal 7 API?
Drupal 8 migrate!
Drupal Javascript for developers
Taming that client side mess with Backbone.js
Your Entity, Your Code
Learning the basics of the Drupal API
Drupal Module Development
Drupal Module Development - OSI Days 2010
Debugging in drupal 8
Routing in Drupal 8
Drupal & javascript
Javascript first-class citizenery
international PHP2011_Bastian Feder_jQuery's Secrets
Jquery fundamentals
Php on the desktop and php gtk2
Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011
Building Large jQuery Applications
How I Learned to Stop Worrying and Love jQuery (Jan 2013)
jQuery secrets
Ad

Recently uploaded (20)

PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
KodekX | Application Modernization Development
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Electronic commerce courselecture one. Pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Modernizing your data center with Dell and AMD
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Cloud computing and distributed systems.
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Network Security Unit 5.pdf for BCA BBA.
KodekX | Application Modernization Development
MYSQL Presentation for SQL database connectivity
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Electronic commerce courselecture one. Pdf
The AUB Centre for AI in Media Proposal.docx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
cuic standard and advanced reporting.pdf
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Reach Out and Touch Someone: Haptics and Empathic Computing
Advanced methodologies resolving dimensionality complications for autism neur...
Modernizing your data center with Dell and AMD
“AI and Expert System Decision Support & Business Intelligence Systems”
Per capita expenditure prediction using model stacking based on satellite ima...
Chapter 3 Spatial Domain Image Processing.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Spectral efficient network and resource selection model in 5G networks
Cloud computing and distributed systems.
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript

  • 1. jQuery UI in Drupal 7 Darren Mothersele @mothersele http://www.darrenmothersele.com Ivan Sutherland's Sketchpad system is demonstrated on the console of the TX-2 at MIT (1963)
  • 2. jQuery UI in Drupal 7 • Using Javascript to improve UI • Javascript in Drupal 7 • jQuery UI widgets • Building complex interactions
  • 3. HTML + Javascript • HTML defines a set of standard form elements • Javascript allows us to build new interactive widgets • jQuery UI provides widgets, interactions and effects
  • 4. Javascript widgets • Reduce errors in data entry • Quicker/more efficient • More engaging/fun • ...
  • 6. Progressive Enhancement • Create widget using jQuery • Hide old widget • Fill in value in the background • Transparent to Drupal, nice and safe • Non-js friendly
  • 7. Before Active Tags • Multiple tagging methodologies • Some people just expect to use spaces • http://drupal.org/node/91074 • Character-delimited system
  • 8. Active Tags • Action-delimited system • Autocomplete • Original widget hidden • http://drupal.org/project/active_tags
  • 9. Javascript in Drupal 7 • Theme or Module? • Scope? • Module specific or reuseable? (Javascript Library) • How and where to include code?
  • 10. theme.info name = My theme description = Theme developed by me. core = 7.x engine = phptemplate scripts[] = my_script.js
  • 11. drupal_add_js($data, $options) $data is either: • path to Javascript file to include • Javascript code to include ‘inline’ • absolute path to external JS code • array of settings for Javascript $options includes type, location, caching, ...
  • 12. hook_preprocess_page() function mytheme_preprocess_page(&$vars, $hook) { if (true) { drupal_add_js( drupal_get_path('theme', 'mytheme') . '/scriptname.js', 'theme'); $vars['scripts'] = drupal_get_js(); } }
  • 13. hook_library() • Used in Core for jQuery UI • Use to include other third-party plugins • Define your own reusable Javascript
  • 16. Dialog Example function dproj_form_user_login_block_alter(&$form, $form_state) { drupal_add_library('system', 'ui.dialog'); $dialog_js = '$("#block-user-login").dialog({title: "User login"});'; $dialog_js = 'jQuery(document).ready(function () { (function ($) {' . $dialog_js . '}(jQuery)); });'; drupal_add_js($dialog_js, array('type' => 'inline', 'scope' => 'footer', 'weight' => 5)); }
  • 19. drupal_add_library() function dproj_form_issue_node_form_alter(&$form, $form_state, $form_id) { $language = $form['#node']->language; $form['field_issue_type'][$language]['#after_build'][] = '_dproj_button_helper'; } function _dproj_button_helper($element, &$form_state) { $button_js = '$("#'. $element['#id'] .'").buttonset();'; $button_js = JS_PREFIX . $button_js . JS_SUFFIX; drupal_add_library('system', 'ui.button'); drupal_add_js($button_js, array('type' => 'inline', 'scope' => 'footer', 'weight' => 5)); return $element; }
  • 22. hook_library() function dproj_library() { $path = drupal_get_path('module', 'dproj'); return array('autoresize' => array( 'title' => 'AutoResize', 'website' => 'https://github.com/...' 'version' => '1.14', 'js' => array( $path .'/jquery.autoresize.js' => array(), ), )); }
  • 24. function dproj_form_project_node_form_alter(&$form, $form_state, $form_id) { $language = $form['#node']->language; $form['field_project_desc'][$language]['#after_build'][] = '_dproj_autoresize_helper'; } function _dproj_autoresize_helper($element, &$form_state) { $id = $element[0]['value']['#id'] $autoresize_js = '$("#'. $id .'").autoResize();'; $autoresize_js = JS_PREFIX . $autoresize_js . JS_SUFFIX; drupal_add_library('dproj', 'autoresize'); drupal_add_js($autoresize_js, array('type' => 'inline', 'scope' => 'footer', 'weight' => 5)); return $element; }
  • 26. Building a more complex interaction • Drupal Behaviors • jQuery ui.draggable • jQuery ui.droppable • jQuery AJAX • Contrib modules: Page manager (ctools), Rules, Relation
  • 27. Behaviors • Replacement for $(document).ready(); • Use attach function: (function ($) { Drupal.behaviors.exampleModule = { attach: function (context, settings) { $('.dproj', context).draggable(); } }; }(jQuery)); • AJAX safe • Detachable
  • 28. Drag and Drop Draggable Droppable $.ajax() Callback
  • 30. .dproj-draggable .dproj-droppable $('.dproj-draggable').draggable(); $('.dproj-droppable').droppable();
  • 31. path1 = add-attendee/[uid] path2 = /[nid] callback = path1 + path2 callback = add-attendee/[uid]/[nid]
  • 35. <span class='callback'> add-attendee/1 </span> <span class='callback'>/20</span> callback = add-attendee/1/20
  • 36. Drupal.behaviors.dprojdrag = { attach: function (context, settings) { $('.dproj-draggable', context).draggable({revert: 'invalid'}); $('.dproj-droppable', context).droppable({ hoverClass: 'drop-hover', accept: '.dproj-draggable', drop: function (e, ui) { $(ui.draggable).hide(); $(e.target).fadeTo('fast', 0.5); var view_id = '.' + $(e.target).attr('class').match(/view-dom-id-d+/)[0]; var href = Drupal.settings.basePath + $('.callback', ui.draggable).html() + $('.callback', e.target).html(); $.ajax({ url: href, success: function (data) { $(view_id).html($(view_id, $(data))); $(view_id).fadeTo('fast', 1); }});}});}};
  • 45. Resources • jQuery UI • Contrib Modules http://jqueryui.com/ demos • Views • Managing Javascript in • Relation Drupal 7 http://drupal.org/node/ 756722 • Page manager • Rules