SlideShare uma empresa Scribd logo
1 de 40
FAPI
heavy lifting with the drupal 6 forms api
DRUPAL FIT


http://groups.drupal.org/fit
ABOUT ME

•   @stevenator d.o/g.d.o

•   me@stevenrifkin.com

•   in addition to Drupal, RoR

•   codeStubble.com

•   Freelancer, hobbyist, World Traveler
MANY WAYS TO SKIN A CAT
CONTRIB MODULES

•   Webform

•   Rules Module does some of this very well - d.o/project/rules

•   CTools...thanks @mdorrell

•   hook_form_alter(&$form, &$form_state, $form_id)

•   Other modules...comments?
MAKE A DECISION
    WHEN MAKING YOUR OWN FORMS

•   planning is your best friend

•   if you go with contrib...test - test - test.

•   Have a read of the .module file

•   can you get away with just hook_form_alter()?
BUILD A MODULE
http://drupal.org/project/module_builder
     @joachim - Joachim Noreiko
MODULE BUILDER ALSO
•   pulls the hook definitions directly from api.drupal.org

•   for lazy developers - reminds you of the params and whether they are
    referenced

•   noobs/newbs/neubs

•   toggle comments
drush mbdl
   drush mb fapi_demo nodeapi menu form form_alter
Paste the returned code into the MY_MODULE.module file

               fapi_demo.module
                    pass in params
                     --build
                     --write --quiet
                     --parent
                     --add
                     --go
                 drush en fapi_demo
FAPI - PERSONAL THOUGHTS
UNDERSTAND THE PAST
The release of Drupal 4.7 introduced the Form API, a framework for building, displaying,



validating, and processing HTML forms. With it, forms are defined as structured arrays, and



those structured arrays contain all the information necessary to properly handle the form



throughout its life cycle. This approach also makes it possible for modules to customize other



forms (adding additional fields to a signup page, for example), and allows designers to



customize the on-screen display of forms using overridable theming functions. It also makes



validating form input, and avoiding form tampering, much easier
FAPI 1.0 VS 2.0
•   Form API 1.0 handled display and submission with drupal_get_form($form_id, $form)
          •   single function built to process and build, handling a single $form array

          •   This works for static forms but makes dynamic forms harder


•   Form API 2.0 was introduced with Drupal 5
          •   drupal_get_form($form_id)

          •   Dedicated functions for processing and building

          •   Separates unprocessed user values from the outgoing array of user inputs
API.DRUPAL.ORG
•   http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6

•   API Quickstart Guide: http://drupal.org/node/751826
#TREE

•   form array is written as a tree structure with nested elements

•   $form_state[‘values’] returns a flat array that ignores the defined tree

•   unless you set #tree => TRUE

•   #parents is like #tree
#MARKUP
•   an element to wrap elements, insert basic html

•   any element without a #type attribute defaults to a
    markup element

•   #prefix and #suffix may be able to achienve what
    you are after

•   I use this for a lot of js callbacks on the page to
    replace, append, prepend etc...
QUICK DETOUR FOR hook_theme()

•   register your theme functions as you usually would

•   also declare a template key to the function and a tpl.php (or whatever your templating
    flavor) can be made which is useful if you are working with developers

•   this is also something that I have found increasingly more useful with the rise of AJAX
    heavy calls.

•   there is also a #theme key for every element type allowing for custom theming
    through a function or array of functions you want to pass in.
#VALUE
•   internal form element that never gets printed onto the page.

•   not editable by the user

•   Can hold sensitive data that a #hidden form field would be wrong for.

•   Will be joined into the form array upon submission and placed into
    $form_state[‘values’] for future use.

•   form_set_value($form_values[‘key’], $data, $form_state)
OTHER KEYS WORTH MENTIONING

 •   #after_build

 •   #pre-render - you are responsible for returning altered element array

 •   #post-render - you are responsible for returning the results return $html
ADD MENU PATHS
CLEAR
THE
CACHE
CLEAR THE MENU CACHE
       drush cc menu
INITIALIZING THE FORM

•   $_POST and $form_build_id exist?

•   setting a token -- drupal_private_key -- http://drupal.org/node/28420

•   logged in users only anonymous user forms are not unique and are cached

•   prevent injection and xss attacks
hook_elements()

•   this is called from element_info() - collects information on all element properties and
    stores them in cache

•   custom form elements, extend existing elements

•   fivestar module or wysiwyg/tinymce
drupal_get_form($form_id)

•   most called/commonly used of the FAPI functions

•   it returns the output of the rendered form



return drupal_get_form(‘user_register’);
hook_forms($form_id, $args)

•   analogous to hook_theme but for forms

•   validation and submit callbacks are still mapped to the unique form id
drupal_validate_form($form_id)
•   validates token
•   FAPI validation
•   validation callbacks -- form_set_error()
drupal_submit_form($form_id)
•   this is the final step for the developer, i.e. do something
    with all of your new data

•   When a form submits to itself, drupal_get_form will return
    the form to it’s intial page callback unless 1 of two
    conditions exist:

•   $form_state[‘redirect’]

•   $form_state[‘rebuild’] = TRUE
drupal_process_form($form_id, &$form, &$form_state)

•   “the heart of the form API” --api.drupal.org

•   called by drupal_get_form(), drupal_rebuild_form(), and drupal_execute()


               •   form is built

               •   validated

               •   submitted if passes validation or nothing else get in the way
hook_form(&$node, $form_state) vs my_function_form(&$form_state)
         •   hook_form is also used by modules that invoke node forms
             and takes a referenced node object

         •   changes will be seen on the save/edit paths

         •   this is used to add your own form elements to node content

         •   replication of what cck does?

         •   hook_node_info
FORM SCENARIOS

•   Long Form

•   Multistep

•   Wizard/Self Building Form
LONG FORM
•   this is pretty straight forward and comes straight out of the box
    using content types and cck

•   build your form in the browser interface
MULTISTEP
•   save the philosophical debate for the end

•   keys to multistep are in the form_state[‘rebuild’] and
    form_state[‘storage’] settings.

•   Beginning in D6, a form is cached after it is built for the first
    time. So setting $form_state[‘rebuild’] => TRUE will create a
    fresh form and allow you to change the $form array’s
    structure and values.

•   Storing the form’s values into $form_state[‘storage’] will
    cache them for use on the next page
hook_form_alter(&$form, &$form_state, $form_id)

•   remember that you are passing in a reference to the $form array

•   hook_form_FORM_ID_alter will save you a switch or if statement
CAVEATS AND GOTCHAS

•   menu cache

•   $form is an array not an object

•   forms are cached

•   ‘enctype’ => ‘multipart’

•   Drupal.behaviors -to bind functions to page load. Plays nice with other Javascript
    objects acting on the DOM
OTHER FUNCTIONS AND TOPICS

•   good practice to use module key within $form_state, i.e. form_state[‘fapi_demo’][], if
    you are going to use it to pass values

•   $form['destination'] = array('#type' => 'hidden', '#value' => $_GET['q']);
FUTURE

• Advanced AJAX - #ajax - BADCAMP

• http://api.drupal.org/api/drupal/includes--ajax.inc/group/ajax_commands/7

• Ctool by earl miles (@merlinofchaos)

• Open discussion
•   Pro Drupal Development by John K. VanDyk, Apress © 2008

    D7 version is out - Todd Tomlinson

•   http://drupal.org/node/101707

•   API Quickstart Guide: http://drupal.org/node/751826

•   FAPI (The Grail) http://api.drupal.org/api/drupal/developer--
    topics--forms_api_reference.html/6



                       SOURCES
•   http://www.appnovation.com/create-multiple-step-form-drupal-6




                     SOURCES

Mais conteúdo relacionado

Mais procurados

Building and managing java projects with maven part-III
Building and managing java projects with maven part-IIIBuilding and managing java projects with maven part-III
Building and managing java projects with maven part-IIIprinceirfancivil
 
Drupal 8 theming deep dive
Drupal 8 theming deep diveDrupal 8 theming deep dive
Drupal 8 theming deep diveRomain Jarraud
 
Building Potent WordPress Websites
Building Potent WordPress WebsitesBuilding Potent WordPress Websites
Building Potent WordPress WebsitesKyle Cearley
 
Magento mega menu extension
Magento mega menu extensionMagento mega menu extension
Magento mega menu extensionBun Danny
 
Config management
Config managementConfig management
Config managementAlexei Goja
 
Ruby on Rails: Building Web Applications Is Fun Again!
Ruby on Rails: Building Web Applications Is Fun Again!Ruby on Rails: Building Web Applications Is Fun Again!
Ruby on Rails: Building Web Applications Is Fun Again!judofyr
 
Really Rapid Admin Application Development
Really Rapid Admin Application DevelopmentReally Rapid Admin Application Development
Really Rapid Admin Application DevelopmentJose Diaz-Gonzalez
 
Training in Android with Maven
Training in Android with MavenTraining in Android with Maven
Training in Android with MavenArcadian Learning
 
Drupal 8 introduction to theming
Drupal 8  introduction to themingDrupal 8  introduction to theming
Drupal 8 introduction to themingBrahampal Singh
 
Tips and Tricks for LiveWhale Development
Tips and Tricks for LiveWhale DevelopmentTips and Tricks for LiveWhale Development
Tips and Tricks for LiveWhale DevelopmentNaomi Royall
 
How to create a basic template
How to create a basic templateHow to create a basic template
How to create a basic templatevathur
 
T200 portal
T200 portalT200 portal
T200 portalJCK
 
Alchemy CMS Präsentation Rails UG HH 09.2011
Alchemy CMS Präsentation Rails UG HH 09.2011Alchemy CMS Präsentation Rails UG HH 09.2011
Alchemy CMS Präsentation Rails UG HH 09.2011Thomas von Deyen
 
Debugging in drupal 8
Debugging in drupal 8Debugging in drupal 8
Debugging in drupal 8Allie Jones
 
HTML5: the new frontier of the web
HTML5: the new frontier of the webHTML5: the new frontier of the web
HTML5: the new frontier of the webIvano Malavolta
 
Introduction to Module Development (Drupal 7)
Introduction to Module Development (Drupal 7)Introduction to Module Development (Drupal 7)
Introduction to Module Development (Drupal 7)April Sides
 
Your first d8 module
Your first d8 moduleYour first d8 module
Your first d8 moduletedbow
 

Mais procurados (20)

Building and managing java projects with maven part-III
Building and managing java projects with maven part-IIIBuilding and managing java projects with maven part-III
Building and managing java projects with maven part-III
 
Drupal 8 theming deep dive
Drupal 8 theming deep diveDrupal 8 theming deep dive
Drupal 8 theming deep dive
 
Building Potent WordPress Websites
Building Potent WordPress WebsitesBuilding Potent WordPress Websites
Building Potent WordPress Websites
 
WordPress Theming 101
WordPress Theming 101WordPress Theming 101
WordPress Theming 101
 
Maven II
Maven IIMaven II
Maven II
 
Magento mega menu extension
Magento mega menu extensionMagento mega menu extension
Magento mega menu extension
 
Config management
Config managementConfig management
Config management
 
Ruby on Rails: Building Web Applications Is Fun Again!
Ruby on Rails: Building Web Applications Is Fun Again!Ruby on Rails: Building Web Applications Is Fun Again!
Ruby on Rails: Building Web Applications Is Fun Again!
 
Really Rapid Admin Application Development
Really Rapid Admin Application DevelopmentReally Rapid Admin Application Development
Really Rapid Admin Application Development
 
Training in Android with Maven
Training in Android with MavenTraining in Android with Maven
Training in Android with Maven
 
Drupal 8 introduction to theming
Drupal 8  introduction to themingDrupal 8  introduction to theming
Drupal 8 introduction to theming
 
Tips and Tricks for LiveWhale Development
Tips and Tricks for LiveWhale DevelopmentTips and Tricks for LiveWhale Development
Tips and Tricks for LiveWhale Development
 
How to create a basic template
How to create a basic templateHow to create a basic template
How to create a basic template
 
T200 portal
T200 portalT200 portal
T200 portal
 
Alchemy CMS Präsentation Rails UG HH 09.2011
Alchemy CMS Präsentation Rails UG HH 09.2011Alchemy CMS Präsentation Rails UG HH 09.2011
Alchemy CMS Präsentation Rails UG HH 09.2011
 
Debugging in drupal 8
Debugging in drupal 8Debugging in drupal 8
Debugging in drupal 8
 
HTML5: the new frontier of the web
HTML5: the new frontier of the webHTML5: the new frontier of the web
HTML5: the new frontier of the web
 
Introduction to Module Development (Drupal 7)
Introduction to Module Development (Drupal 7)Introduction to Module Development (Drupal 7)
Introduction to Module Development (Drupal 7)
 
Your first d8 module
Your first d8 moduleYour first d8 module
Your first d8 module
 
Maven
MavenMaven
Maven
 

Destaque

December 2012 1ID Fort Riley Monthly News Update
December 2012 1ID Fort Riley Monthly News UpdateDecember 2012 1ID Fort Riley Monthly News Update
December 2012 1ID Fort Riley Monthly News UpdateNoel Waterman
 
The 1st infantry Division Post 13 Jan Edition
The 1st infantry Division Post 13 Jan Edition The 1st infantry Division Post 13 Jan Edition
The 1st infantry Division Post 13 Jan Edition Noel Waterman
 
Festival Research - Ayrton ; Ashleigh
Festival Research - Ayrton ; AshleighFestival Research - Ayrton ; Ashleigh
Festival Research - Ayrton ; Ashleighvegnagun1
 
關鍵字洞察報告 - 秋冬猶如中風季
關鍵字洞察報告 - 秋冬猶如中風季關鍵字洞察報告 - 秋冬猶如中風季
關鍵字洞察報告 - 秋冬猶如中風季iProspect
 
Gyvenimo problemos
Gyvenimo problemosGyvenimo problemos
Gyvenimo problemoszigius
 
Dasar dasar algoritma
Dasar dasar algoritmaDasar dasar algoritma
Dasar dasar algoritmaaliemprabowo
 
More awesome than ea
More awesome than eaMore awesome than ea
More awesome than eaRuth Deller
 
Sept 2012 Devil's Corner Monthly Newsletter
Sept 2012 Devil's Corner Monthly NewsletterSept 2012 Devil's Corner Monthly Newsletter
Sept 2012 Devil's Corner Monthly NewsletterNoel Waterman
 
The 1ST Infantry Division Post Edition 2 March 2012
The 1ST Infantry Division Post Edition 2 March 2012 The 1ST Infantry Division Post Edition 2 March 2012
The 1ST Infantry Division Post Edition 2 March 2012 Noel Waterman
 
What are Those Stripes and Bars?
What are Those Stripes and Bars?  What are Those Stripes and Bars?
What are Those Stripes and Bars? Noel Waterman
 
1ID and Fort Riley Weekly News Update
1ID and Fort Riley Weekly News Update 1ID and Fort Riley Weekly News Update
1ID and Fort Riley Weekly News Update Noel Waterman
 
Larks internationalization presentation
Larks internationalization presentationLarks internationalization presentation
Larks internationalization presentationSteven Rifkin
 
The 1st Infantry Division Post Paper
The 1st Infantry Division Post PaperThe 1st Infantry Division Post Paper
The 1st Infantry Division Post PaperNoel Waterman
 

Destaque (20)

GSDI11 PGIS
GSDI11 PGISGSDI11 PGIS
GSDI11 PGIS
 
working-in-groups-student-handout-strang-2011
working-in-groups-student-handout-strang-2011working-in-groups-student-handout-strang-2011
working-in-groups-student-handout-strang-2011
 
December 2012 1ID Fort Riley Monthly News Update
December 2012 1ID Fort Riley Monthly News UpdateDecember 2012 1ID Fort Riley Monthly News Update
December 2012 1ID Fort Riley Monthly News Update
 
The 1st infantry Division Post 13 Jan Edition
The 1st infantry Division Post 13 Jan Edition The 1st infantry Division Post 13 Jan Edition
The 1st infantry Division Post 13 Jan Edition
 
Festival Research - Ayrton ; Ashleigh
Festival Research - Ayrton ; AshleighFestival Research - Ayrton ; Ashleigh
Festival Research - Ayrton ; Ashleigh
 
關鍵字洞察報告 - 秋冬猶如中風季
關鍵字洞察報告 - 秋冬猶如中風季關鍵字洞察報告 - 秋冬猶如中風季
關鍵字洞察報告 - 秋冬猶如中風季
 
Gyvenimo problemos
Gyvenimo problemosGyvenimo problemos
Gyvenimo problemos
 
Petranews 2010
Petranews 2010Petranews 2010
Petranews 2010
 
Dasar dasar algoritma
Dasar dasar algoritmaDasar dasar algoritma
Dasar dasar algoritma
 
More awesome than ea
More awesome than eaMore awesome than ea
More awesome than ea
 
Sept 2012 Devil's Corner Monthly Newsletter
Sept 2012 Devil's Corner Monthly NewsletterSept 2012 Devil's Corner Monthly Newsletter
Sept 2012 Devil's Corner Monthly Newsletter
 
The 1ST Infantry Division Post Edition 2 March 2012
The 1ST Infantry Division Post Edition 2 March 2012 The 1ST Infantry Division Post Edition 2 March 2012
The 1ST Infantry Division Post Edition 2 March 2012
 
What are Those Stripes and Bars?
What are Those Stripes and Bars?  What are Those Stripes and Bars?
What are Those Stripes and Bars?
 
1ID and Fort Riley Weekly News Update
1ID and Fort Riley Weekly News Update 1ID and Fort Riley Weekly News Update
1ID and Fort Riley Weekly News Update
 
Larks internationalization presentation
Larks internationalization presentationLarks internationalization presentation
Larks internationalization presentation
 
Bbcaudiences
BbcaudiencesBbcaudiences
Bbcaudiences
 
time_management_handout(goerzen_2011)
time_management_handout(goerzen_2011)time_management_handout(goerzen_2011)
time_management_handout(goerzen_2011)
 
Medieval world
Medieval worldMedieval world
Medieval world
 
Viðskiptabladid 19mars2009
Viðskiptabladid 19mars2009Viðskiptabladid 19mars2009
Viðskiptabladid 19mars2009
 
The 1st Infantry Division Post Paper
The 1st Infantry Division Post PaperThe 1st Infantry Division Post Paper
The 1st Infantry Division Post Paper
 

Semelhante a Fapi

Learning the basics of the Drupal API
Learning the basics of the Drupal APILearning the basics of the Drupal API
Learning the basics of the Drupal APIAlexandru Badiu
 
DrupalCon LA 2015 Review
DrupalCon LA 2015 ReviewDrupalCon LA 2015 Review
DrupalCon LA 2015 ReviewlittleMAS
 
Drupal 8 - Core and API Changes
Drupal 8 - Core and API ChangesDrupal 8 - Core and API Changes
Drupal 8 - Core and API ChangesShabir Ahmad
 
Staying Sane with Drupal (A Develper's Survival Guide)
Staying Sane with Drupal (A Develper's Survival Guide)Staying Sane with Drupal (A Develper's Survival Guide)
Staying Sane with Drupal (A Develper's Survival Guide)Oscar Merida
 
Drupal security
Drupal securityDrupal security
Drupal securityJozef Toth
 
13th Sep, Drupal 7 advanced training by TCS
13th Sep, Drupal 7 advanced training by TCS 13th Sep, Drupal 7 advanced training by TCS
13th Sep, Drupal 7 advanced training by TCS DrupalMumbai
 
Drupal Best Practices
Drupal Best PracticesDrupal Best Practices
Drupal Best Practicesmanugoel2003
 
Валентин Мацвейко та Владислав Мойсеєнко — D8: Migrate Yourself: code->module...
Валентин Мацвейко та Владислав Мойсеєнко — D8: Migrate Yourself: code->module...Валентин Мацвейко та Владислав Мойсеєнко — D8: Migrate Yourself: code->module...
Валентин Мацвейко та Владислав Мойсеєнко — D8: Migrate Yourself: code->module...LEDC 2016
 
Migrate yourself. code -> module -> mind
Migrate yourself. code -> module -> mindMigrate yourself. code -> module -> mind
Migrate yourself. code -> module -> mindValentine Matsveiko
 
Advanced moduledevelopment d6_slideshare
Advanced moduledevelopment d6_slideshareAdvanced moduledevelopment d6_slideshare
Advanced moduledevelopment d6_slideshareOpevel
 
What's new in the Drupal 7 API?
What's new in the Drupal 7 API?What's new in the Drupal 7 API?
What's new in the Drupal 7 API?Alexandru Badiu
 
From Procedural to Object-Oriented PHP in Drupal
From Procedural to Object-Oriented PHP in DrupalFrom Procedural to Object-Oriented PHP in Drupal
From Procedural to Object-Oriented PHP in DrupalAmber Matz
 
Drupal Module Development - OSI Days 2010
Drupal Module Development - OSI Days 2010Drupal Module Development - OSI Days 2010
Drupal Module Development - OSI Days 2010Siva Epari
 
Drupal Module Development
Drupal Module DevelopmentDrupal Module Development
Drupal Module Developmentipsitamishra
 
SilverStripe From a Developer's Perspective
SilverStripe From a Developer's PerspectiveSilverStripe From a Developer's Perspective
SilverStripe From a Developer's Perspectiveajshort
 
Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008
Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008
Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008Mir Nazim
 
Drupal Security from Drupalcamp Bratislava
Drupal Security from Drupalcamp BratislavaDrupal Security from Drupalcamp Bratislava
Drupal Security from Drupalcamp BratislavaGábor Hojtsy
 
Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3Drupalcon Paris
 
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)Ranel Padon
 
Doing Drupal security right
Doing Drupal security rightDoing Drupal security right
Doing Drupal security rightGábor Hojtsy
 

Semelhante a Fapi (20)

Learning the basics of the Drupal API
Learning the basics of the Drupal APILearning the basics of the Drupal API
Learning the basics of the Drupal API
 
DrupalCon LA 2015 Review
DrupalCon LA 2015 ReviewDrupalCon LA 2015 Review
DrupalCon LA 2015 Review
 
Drupal 8 - Core and API Changes
Drupal 8 - Core and API ChangesDrupal 8 - Core and API Changes
Drupal 8 - Core and API Changes
 
Staying Sane with Drupal (A Develper's Survival Guide)
Staying Sane with Drupal (A Develper's Survival Guide)Staying Sane with Drupal (A Develper's Survival Guide)
Staying Sane with Drupal (A Develper's Survival Guide)
 
Drupal security
Drupal securityDrupal security
Drupal security
 
13th Sep, Drupal 7 advanced training by TCS
13th Sep, Drupal 7 advanced training by TCS 13th Sep, Drupal 7 advanced training by TCS
13th Sep, Drupal 7 advanced training by TCS
 
Drupal Best Practices
Drupal Best PracticesDrupal Best Practices
Drupal Best Practices
 
Валентин Мацвейко та Владислав Мойсеєнко — D8: Migrate Yourself: code->module...
Валентин Мацвейко та Владислав Мойсеєнко — D8: Migrate Yourself: code->module...Валентин Мацвейко та Владислав Мойсеєнко — D8: Migrate Yourself: code->module...
Валентин Мацвейко та Владислав Мойсеєнко — D8: Migrate Yourself: code->module...
 
Migrate yourself. code -> module -> mind
Migrate yourself. code -> module -> mindMigrate yourself. code -> module -> mind
Migrate yourself. code -> module -> mind
 
Advanced moduledevelopment d6_slideshare
Advanced moduledevelopment d6_slideshareAdvanced moduledevelopment d6_slideshare
Advanced moduledevelopment d6_slideshare
 
What's new in the Drupal 7 API?
What's new in the Drupal 7 API?What's new in the Drupal 7 API?
What's new in the Drupal 7 API?
 
From Procedural to Object-Oriented PHP in Drupal
From Procedural to Object-Oriented PHP in DrupalFrom Procedural to Object-Oriented PHP in Drupal
From Procedural to Object-Oriented PHP in Drupal
 
Drupal Module Development - OSI Days 2010
Drupal Module Development - OSI Days 2010Drupal Module Development - OSI Days 2010
Drupal Module Development - OSI Days 2010
 
Drupal Module Development
Drupal Module DevelopmentDrupal Module Development
Drupal Module Development
 
SilverStripe From a Developer's Perspective
SilverStripe From a Developer's PerspectiveSilverStripe From a Developer's Perspective
SilverStripe From a Developer's Perspective
 
Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008
Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008
Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008
 
Drupal Security from Drupalcamp Bratislava
Drupal Security from Drupalcamp BratislavaDrupal Security from Drupalcamp Bratislava
Drupal Security from Drupalcamp Bratislava
 
Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3
 
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
The Synergy of Drupal Hooks/APIs (Custom Module Development with ChartJS)
 
Doing Drupal security right
Doing Drupal security rightDoing Drupal security right
Doing Drupal security right
 

Último

Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 

Último (20)

Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 

Fapi

  • 1. FAPI heavy lifting with the drupal 6 forms api
  • 3. ABOUT ME • @stevenator d.o/g.d.o • me@stevenrifkin.com • in addition to Drupal, RoR • codeStubble.com • Freelancer, hobbyist, World Traveler
  • 4. MANY WAYS TO SKIN A CAT
  • 5. CONTRIB MODULES • Webform • Rules Module does some of this very well - d.o/project/rules • CTools...thanks @mdorrell • hook_form_alter(&$form, &$form_state, $form_id) • Other modules...comments?
  • 6. MAKE A DECISION WHEN MAKING YOUR OWN FORMS • planning is your best friend • if you go with contrib...test - test - test. • Have a read of the .module file • can you get away with just hook_form_alter()?
  • 8. MODULE BUILDER ALSO • pulls the hook definitions directly from api.drupal.org • for lazy developers - reminds you of the params and whether they are referenced • noobs/newbs/neubs • toggle comments
  • 9. drush mbdl drush mb fapi_demo nodeapi menu form form_alter Paste the returned code into the MY_MODULE.module file fapi_demo.module pass in params --build --write --quiet --parent --add --go drush en fapi_demo
  • 10. FAPI - PERSONAL THOUGHTS
  • 11. UNDERSTAND THE PAST The release of Drupal 4.7 introduced the Form API, a framework for building, displaying, validating, and processing HTML forms. With it, forms are defined as structured arrays, and those structured arrays contain all the information necessary to properly handle the form throughout its life cycle. This approach also makes it possible for modules to customize other forms (adding additional fields to a signup page, for example), and allows designers to customize the on-screen display of forms using overridable theming functions. It also makes validating form input, and avoiding form tampering, much easier
  • 12. FAPI 1.0 VS 2.0 • Form API 1.0 handled display and submission with drupal_get_form($form_id, $form) • single function built to process and build, handling a single $form array • This works for static forms but makes dynamic forms harder • Form API 2.0 was introduced with Drupal 5 • drupal_get_form($form_id) • Dedicated functions for processing and building • Separates unprocessed user values from the outgoing array of user inputs
  • 13. API.DRUPAL.ORG • http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6 • API Quickstart Guide: http://drupal.org/node/751826
  • 14. #TREE • form array is written as a tree structure with nested elements • $form_state[‘values’] returns a flat array that ignores the defined tree • unless you set #tree => TRUE • #parents is like #tree
  • 15. #MARKUP • an element to wrap elements, insert basic html • any element without a #type attribute defaults to a markup element • #prefix and #suffix may be able to achienve what you are after • I use this for a lot of js callbacks on the page to replace, append, prepend etc...
  • 16. QUICK DETOUR FOR hook_theme() • register your theme functions as you usually would • also declare a template key to the function and a tpl.php (or whatever your templating flavor) can be made which is useful if you are working with developers • this is also something that I have found increasingly more useful with the rise of AJAX heavy calls. • there is also a #theme key for every element type allowing for custom theming through a function or array of functions you want to pass in.
  • 17. #VALUE • internal form element that never gets printed onto the page. • not editable by the user • Can hold sensitive data that a #hidden form field would be wrong for. • Will be joined into the form array upon submission and placed into $form_state[‘values’] for future use. • form_set_value($form_values[‘key’], $data, $form_state)
  • 18. OTHER KEYS WORTH MENTIONING • #after_build • #pre-render - you are responsible for returning altered element array • #post-render - you are responsible for returning the results return $html
  • 20. CLEAR
  • 21. THE
  • 22. CACHE
  • 23. CLEAR THE MENU CACHE drush cc menu
  • 24. INITIALIZING THE FORM • $_POST and $form_build_id exist? • setting a token -- drupal_private_key -- http://drupal.org/node/28420 • logged in users only anonymous user forms are not unique and are cached • prevent injection and xss attacks
  • 25. hook_elements() • this is called from element_info() - collects information on all element properties and stores them in cache • custom form elements, extend existing elements • fivestar module or wysiwyg/tinymce
  • 26. drupal_get_form($form_id) • most called/commonly used of the FAPI functions • it returns the output of the rendered form return drupal_get_form(‘user_register’);
  • 27. hook_forms($form_id, $args) • analogous to hook_theme but for forms • validation and submit callbacks are still mapped to the unique form id
  • 28. drupal_validate_form($form_id) • validates token • FAPI validation • validation callbacks -- form_set_error()
  • 29. drupal_submit_form($form_id) • this is the final step for the developer, i.e. do something with all of your new data • When a form submits to itself, drupal_get_form will return the form to it’s intial page callback unless 1 of two conditions exist: • $form_state[‘redirect’] • $form_state[‘rebuild’] = TRUE
  • 30. drupal_process_form($form_id, &$form, &$form_state) • “the heart of the form API” --api.drupal.org • called by drupal_get_form(), drupal_rebuild_form(), and drupal_execute() • form is built • validated • submitted if passes validation or nothing else get in the way
  • 31. hook_form(&$node, $form_state) vs my_function_form(&$form_state) • hook_form is also used by modules that invoke node forms and takes a referenced node object • changes will be seen on the save/edit paths • this is used to add your own form elements to node content • replication of what cck does? • hook_node_info
  • 32. FORM SCENARIOS • Long Form • Multistep • Wizard/Self Building Form
  • 33. LONG FORM • this is pretty straight forward and comes straight out of the box using content types and cck • build your form in the browser interface
  • 34. MULTISTEP • save the philosophical debate for the end • keys to multistep are in the form_state[‘rebuild’] and form_state[‘storage’] settings. • Beginning in D6, a form is cached after it is built for the first time. So setting $form_state[‘rebuild’] => TRUE will create a fresh form and allow you to change the $form array’s structure and values. • Storing the form’s values into $form_state[‘storage’] will cache them for use on the next page
  • 35. hook_form_alter(&$form, &$form_state, $form_id) • remember that you are passing in a reference to the $form array • hook_form_FORM_ID_alter will save you a switch or if statement
  • 36. CAVEATS AND GOTCHAS • menu cache • $form is an array not an object • forms are cached • ‘enctype’ => ‘multipart’ • Drupal.behaviors -to bind functions to page load. Plays nice with other Javascript objects acting on the DOM
  • 37. OTHER FUNCTIONS AND TOPICS • good practice to use module key within $form_state, i.e. form_state[‘fapi_demo’][], if you are going to use it to pass values • $form['destination'] = array('#type' => 'hidden', '#value' => $_GET['q']);
  • 38. FUTURE • Advanced AJAX - #ajax - BADCAMP • http://api.drupal.org/api/drupal/includes--ajax.inc/group/ajax_commands/7 • Ctool by earl miles (@merlinofchaos) • Open discussion
  • 39. Pro Drupal Development by John K. VanDyk, Apress © 2008 D7 version is out - Todd Tomlinson • http://drupal.org/node/101707 • API Quickstart Guide: http://drupal.org/node/751826 • FAPI (The Grail) http://api.drupal.org/api/drupal/developer-- topics--forms_api_reference.html/6 SOURCES
  • 40. http://www.appnovation.com/create-multiple-step-form-drupal-6 SOURCES

Notas do Editor

  1. Welcome\nLet’s wait for everyone to settle\n
  2. \n
  3. \n
  4. \n
  5. \n
  6. I’ve made many a module that only implements hook_form_alter\n
  7. \n
  8. stores the hooks in the default directory default.hooks.inc\n
  9. --add Append hooks to module file. Implies '--write --build=code'. Warning: will not check hooks already exist.\n\ndrush dochooks my_module -- inserts hooks into existing modules\n\nremember to clear out unused BS before you try to run the module...\n\nHigher LEVEL\nTelling module builder about your hooks\nLots of contrib modules provide hooks. You can tell module builder about them, so users can easily generate code for your module's hooks in their own modules.\nTo do this, implement hook_module_builder_info() in a file named YOUR_MODULE.module_builder.inc file in your module folder. See the contents of module_builder.module_builder.inc for a detailed example.\n
  10. so before i delve into the api itself, i thought i’d mention why i wanted to give this presentation.\n-learning curve\n-wished someone had started me here\n-because of the loop that the form api takes us through, we learn a lot about drupal, it’s hooks and, most importantly, it’s caches.\n-does anyone share this feeling?\n
  11. there are a few steps\narrays not objects\n
  12. \n
  13. spend some time here\ncheckbox vs checkboxes\n
  14. \n
  15. \n
  16. outside the scope of this talk\ntpl.php is reminiscent of MVC frameworks\n
  17. \n
  18. pass callback function as well as an array of functions for abstracting common tasks that might repeat themselves in other parts of your module and/or form processing\n\n#pre-render called before drupal_render...receives the #element as the param and must return the altered element\n#post-render 2 params “rendered element and its children”\n\nbe careful of overwriting your existing array...you can do a check to see if the properties you are after are set\n\nrules module hooks into these functions\n
  19. GO TO THE SIMPLE #MARKUP, #VALUE EXAMPLE\n
  20. \n
  21. \n
  22. \n
  23. \n
  24. private key is stored in the variables - best not to touch it\n\nif POST exists then we check the cache to see if there is a copy for the current session\nif YES then retrieve cached form and move on to the check if the form was submitted\nif YES we move to the validate functions\nif PASS validate run submit and process values\nif REDIRECT we are off\nif no REDIRECT then redisplay empty form with a message (hopefully)\nif NO check cace, rebuild, set cache, render form\nif NO cached copy, build form, then move to see form was submitted\nNO Post or form_build_id exists then build\n\npg 222 of Pro Drupal 6\n
  25. then element_info is called\nfivestar is pretty straightforward\ntinymce modifies the default properties of the already existing textarea element - changes the #process => ‘callback function called tinymce_process_textarea’\n
  26. \n
  27. This hook allows modules to build multiple forms from a single form "factory" function but each form will have a different form id for submission, validation, theming or alteration by other modules.\n
  28. these are the default callbacks that Drupal tries to match to your unique form id. You can set the #validate and #submit keys on the form, and form elements to instruct your callback to do something special. Those are also called during the drupal_process_form function\n
  29. these are the default callbacks that Drupal tries to match to your unique form id. You can set the #validate and #submit keys on the form, and form elements to instruct your callback to do something special. Those are also called during the drupal_process_form function\n
  30. show http://api.drupal.org/api/drupal/includes--form.inc/function/drupal_process_form/6\n
  31. I though I’d mention hook_node_info here because it doesn’t really jive elsewhere in the presentation\n name\n module\n description\n help\naccepts array with the usual params:\nhas_title => FALSE\nhas_body\n
  32. \n
  33. show hook_form and the returning of the $form\n
  34. \n
  35. &$form\n
  36. \n
  37. \n
  38. show Form API reference D7\nhttp://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/7\n \n FAPI integration - allows module developers to create new form components using '#type' => 'multiselect'\n\n
  39. \n
  40. \n