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
Ad

Recommended

Drupal Step-by-Step: How We Built Our Training Site, Part 1
Drupal Step-by-Step: How We Built Our Training Site, Part 1
Acquia
 
Your Entity, Your Code
Your Entity, Your Code
Marco Vito Moscaritolo
 
Drupal Development (Part 2)
Drupal Development (Part 2)
Jeff Eaton
 
Desarrollo de módulos en Drupal e integración con dispositivos móviles
Desarrollo de módulos en Drupal e integración con dispositivos móviles
Luis Curo Salvatierra
 
Build your own entity with Drupal
Build your own entity with Drupal
Marco Vito Moscaritolo
 
Virtual Madness @ Etsy
Virtual Madness @ Etsy
Nishan Subedi
 
jQuery secrets
jQuery secrets
Bastian Feder
 
Php update and delet operation
Php update and delet operation
syeda zoya mehdi
 
Jquery In Rails
Jquery In Rails
shen liu
 
15. CodeIgniter editarea inregistrarilor
15. CodeIgniter editarea inregistrarilor
Razvan Raducanu, PhD
 
DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7
chuvainc
 
Magento Dependency Injection
Magento Dependency Injection
Anton Kril
 
Django at the Disco
Django at the Disco
Richard Leland
 
Django at the Disco
Django at the Disco
Richard Leland
 
Building iPhone Web Apps using "classic" Domino
Building iPhone Web Apps using "classic" Domino
Rob Bontekoe
 
8. vederea inregistrarilor
8. vederea inregistrarilor
Razvan Raducanu, PhD
 
Coding website
Coding website
PutuMahendra Wijaya
 
HirshHorn theme: how I created it
HirshHorn theme: how I created it
Paul Bearne
 
Command-Oriented Architecture
Command-Oriented Architecture
Luiz Messias
 
Propel sfugmd
Propel sfugmd
iKlaus
 
Mysocial databasequeries
Mysocial databasequeries
Program in Interdisciplinary Computing
 
Mysocial databasequeries
Mysocial databasequeries
Program in Interdisciplinary Computing
 
Pagination in PHP
Pagination in PHP
Vineet Kumar Saini
 
Add edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHP
Vineet Kumar Saini
 
Daily notes
Daily notes
meghendra168
 
Country State City Dropdown in PHP
Country State City Dropdown in PHP
Vineet Kumar Saini
 
JavaScript the Smart Way - Getting Started with jQuery
JavaScript the Smart Way - Getting Started with jQuery
katbailey
 
Intro to jQuery for Drupal
Intro to jQuery for Drupal
jhamiltoorion
 
Smarter Interfaces with jQuery (and Drupal)
Smarter Interfaces with jQuery (and Drupal)
aasarava
 
Intro To jQuery In Drupal
Intro To jQuery In Drupal
Matthew Farina
 

More Related Content

What's hot (18)

Jquery In Rails
Jquery In Rails
shen liu
 
15. CodeIgniter editarea inregistrarilor
15. CodeIgniter editarea inregistrarilor
Razvan Raducanu, PhD
 
DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7
chuvainc
 
Magento Dependency Injection
Magento Dependency Injection
Anton Kril
 
Django at the Disco
Django at the Disco
Richard Leland
 
Django at the Disco
Django at the Disco
Richard Leland
 
Building iPhone Web Apps using "classic" Domino
Building iPhone Web Apps using "classic" Domino
Rob Bontekoe
 
8. vederea inregistrarilor
8. vederea inregistrarilor
Razvan Raducanu, PhD
 
Coding website
Coding website
PutuMahendra Wijaya
 
HirshHorn theme: how I created it
HirshHorn theme: how I created it
Paul Bearne
 
Command-Oriented Architecture
Command-Oriented Architecture
Luiz Messias
 
Propel sfugmd
Propel sfugmd
iKlaus
 
Mysocial databasequeries
Mysocial databasequeries
Program in Interdisciplinary Computing
 
Mysocial databasequeries
Mysocial databasequeries
Program in Interdisciplinary Computing
 
Pagination in PHP
Pagination in PHP
Vineet Kumar Saini
 
Add edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHP
Vineet Kumar Saini
 
Daily notes
Daily notes
meghendra168
 
Country State City Dropdown in PHP
Country State City Dropdown in PHP
Vineet Kumar Saini
 

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

JavaScript the Smart Way - Getting Started with jQuery
JavaScript the Smart Way - Getting Started with jQuery
katbailey
 
Intro to jQuery for Drupal
Intro to jQuery for Drupal
jhamiltoorion
 
Smarter Interfaces with jQuery (and Drupal)
Smarter Interfaces with jQuery (and Drupal)
aasarava
 
Intro To jQuery In Drupal
Intro To jQuery In Drupal
Matthew Farina
 
jQuery for Drupal
jQuery for Drupal
jhamiltoorion
 
jQuery for Drupal
jQuery for Drupal
jhamiltoorion
 
Drupal Javascript for developers
Drupal Javascript for developers
Dream Production AG
 
Tips for writing Javascript for Drupal
Tips for writing Javascript for Drupal
Sergey Semashko
 
JQuery In Drupal
JQuery In Drupal
katbailey
 
jQuery and_drupal
jQuery and_drupal
BlackCatWeb
 
Fapi
Fapi
Steven Rifkin
 
Drupal & javascript
Drupal & javascript
Almog Baku
 
JavaScript in Drupal 7: What developers need to know
JavaScript in Drupal 7: What developers need to know
katbailey
 
JavaScript in Drupal 7: What developers need to know
JavaScript in Drupal 7: What developers need to know
Work at Play
 
Drupal Camp Porto - Developing with Drupal: First Steps
Drupal Camp Porto - Developing with Drupal: First Steps
Luís Carneiro
 
Drupal training-by-ruchiwebsolutions
Drupal training-by-ruchiwebsolutions
php2ranjan
 
Getting Started with DrupalGap
Getting Started with DrupalGap
Alex S
 
jQuery+Drupal Optimizations
jQuery+Drupal Optimizations
Helior Colorado
 
Drupal Render API
Drupal Render API
Pavel Makhrinsky
 
Ux testing recap
Ux testing recap
Angela Byron
 
JavaScript the Smart Way - Getting Started with jQuery
JavaScript the Smart Way - Getting Started with jQuery
katbailey
 
Intro to jQuery for Drupal
Intro to jQuery for Drupal
jhamiltoorion
 
Smarter Interfaces with jQuery (and Drupal)
Smarter Interfaces with jQuery (and Drupal)
aasarava
 
Intro To jQuery In Drupal
Intro To jQuery In Drupal
Matthew Farina
 
Drupal Javascript for developers
Drupal Javascript for developers
Dream Production AG
 
Tips for writing Javascript for Drupal
Tips for writing Javascript for Drupal
Sergey Semashko
 
JQuery In Drupal
JQuery In Drupal
katbailey
 
jQuery and_drupal
jQuery and_drupal
BlackCatWeb
 
Drupal & javascript
Drupal & javascript
Almog Baku
 
JavaScript in Drupal 7: What developers need to know
JavaScript in Drupal 7: What developers need to know
katbailey
 
JavaScript in Drupal 7: What developers need to know
JavaScript in Drupal 7: What developers need to know
Work at Play
 
Drupal Camp Porto - Developing with Drupal: First Steps
Drupal Camp Porto - Developing with Drupal: First Steps
Luís Carneiro
 
Drupal training-by-ruchiwebsolutions
Drupal training-by-ruchiwebsolutions
php2ranjan
 
Getting Started with DrupalGap
Getting Started with DrupalGap
Alex S
 
jQuery+Drupal Optimizations
jQuery+Drupal Optimizations
Helior Colorado
 
Ad

Recently uploaded (20)

SAP Modernization Strategies for a Successful S/4HANA Journey.pdf
SAP Modernization Strategies for a Successful S/4HANA Journey.pdf
Precisely
 
June Patch Tuesday
June Patch Tuesday
Ivanti
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
Edge AI and Vision Alliance
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Impelsys Inc.
 
Supporting the NextGen 911 Digital Transformation with FME
Supporting the NextGen 911 Digital Transformation with FME
Safe Software
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
Edge AI and Vision Alliance
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
Muhammad Rizwan Akram
 
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
SAP Modernization Strategies for a Successful S/4HANA Journey.pdf
SAP Modernization Strategies for a Successful S/4HANA Journey.pdf
Precisely
 
June Patch Tuesday
June Patch Tuesday
Ivanti
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
Edge AI and Vision Alliance
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Impelsys Inc.
 
Supporting the NextGen 911 Digital Transformation with FME
Supporting the NextGen 911 Digital Transformation with FME
Safe Software
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
Edge AI and Vision Alliance
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
Muhammad Rizwan Akram
 
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Ad

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