Get on with Field API

669 views

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
669
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
11
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Get on with Field API

  1. 1. Get on with Field API Drupalcamp Spain - Cáceres 27/10/2013
  2. 2. What is Field API • Included in Drupal 7 from CCK module • The way to store and represent Entities properties • Utilizes Form API to insert data • Flexible and easily extensible
  3. 3. Terminology • Entities • Bundles • Field Types • Field Storage • Field Instances
  4. 4. Structure Semaphore bundle Mug bundle Semaphore color Mug color Entity Instance Field Type Color
  5. 5. Field API components • Field Types API • Field Language API • Field Info API • Field attach API • Field CRUD API • Field Storage API
  6. 6. Field API components • Field Types API • Field Language API • Field Info API • Field Attach API • Field CRUD API • Field Storage API
  7. 7. Field Types API • Field Type • Widget • Formatter
  8. 8. Field Type • hook_field_info() • hook_field_schema() • hook_field_is_empty() • Other hooks... • hook_field_validate(), hook_field_presave(), hook_field_prepare_translation()...
  9. 9. Widget • hook_field_widget_info() • hook_field_widget_settings_form() • hook_field_widget_form() • Other hooks... • hook_field_widget_form_alter(), hook_field_widget_WIDGET_TYPE_form_alter(), hook_field_widget_properties_alter(), hook_field_widget_error()...
  10. 10. Formatter • hook_field_formatter_info() • hook_field_formatter_settings_form() • hook_field_formatter_settings_summary() • hook_field_formatter_prepare_view() • hook_field_formatter_view() • Field formatter settings module (D8 included)
  11. 11. ... and all for what?? To make our life easier!!
  12. 12. Our boss need... Show the sum of all field items
  13. 13. The easy way... fnto m_ouend_iw$oe $iwmd,$agoe { ucin ymdl_oeve(nd, ve_oe lncd) i (nd-tp = 'ae){ f $oe>ye = pg' i (se(nd-cnet'il_nee'[]){ f ist$oe>otn[feditgr]0) $tm =$oe>il_nee[ud] ies nd-feditgr'n'; $eut=0 rsl ; frah(iesa $tm { oec $tm s ie) $eut+ $tm'au'; rsl = ie[vle] } $oe>otn[feditgr]= nd-cnet'il_nee' aryary'mru'= $eut) ra(ra(#akp > rsl); } } }
  14. 14. Yes, you can use Field API!!
  15. 15. hook_field_formatter_info() fnto adto_omte_il_omte_no){ ucin diinfratrfedfratrif( rtr ary eun ra( 'diinfratrbsc = ary adto_omte_ai' > ra( 'ae'= t'ai Adto Fratr) lbl > (Bsc diin omte', 'ecito'= t'usaltefedies) dsrpin > (Sm l h il tm', 'il tps = ary'ubritgr,'ubrdcml, fed ye' > ra(nme_nee' nme_eia' 'ubrfot) nme_la', 'etns = ary) stig' > ra(, ) , ) ; }
  16. 16. hook_field_formatter_view() fnto adto_omte_il_omte_iw$niytp, ucin diinfratrfedfratrve(ett_ye $niy $il,$ntne $agoe $tm,$ipa){ ett, fed isac, lncd, ies dsly $lmn =ary) eeet ra(; i (ies { f $tm) $diin=0 adto ; frah(iesa $et = $tm { oec $tm s dla > ie) $diin+ $tm'au'; adto = ie[vle] } $lmn[]=ary'mru'= $diin; eeet0 ra(#akp > adto) } rtr $lmn; eun eeet }
  17. 17. But now, what I need... ...is represent basic arithmetic operations and store its results
  18. 18. How would I write it... • Create our custom field type • 4 DB columns (operand1, operand 2, operation and value) • Create our custom widget • Two text fields for each operand and select for operation • Black magic to store operation result • Create our custom formatter • hook_field_formatter_info_alter() • Reuse number module formatters
  19. 19. It's so easy using Field API...
  20. 20. Field Language API
  21. 21. https://drupal.org/node/1500308
  22. 22. https://drupal.org/node/1500308
  23. 23. How to access to our fields $aus=$niy>yfed'n'[] vle ett-m_il[ud]0 $agae=fedlnug(nd' $oe 'yfed) lnug il_agae'oe, nd, m_il'; $aus=$oe>yfed$agae[] vle nd-m_il[lnug]0;
  24. 24. How to access to our fields using Field API fedgties il_e_tm (ett_ye $niy $il_ae $agoe=NL) $niytp, ett, fednm, lncd UL fedve_il il_iwfed (ett_ye $niy $il_ae $ipa =ary) $agoe=NL) $niytp, ett, fednm, dsly ra(, lncd UL fedve_au il_iwvle (ett_ye $niy $il_ae $tm $ipa =ary) $agoe=NL) $niytp, ett, fednm, ie, dsly ra(, lncd UL
  25. 25. Field API do it for us...
  26. 26. ...and Drupal 8?? http://www.flickr.com/photos/37765154@N04
  27. 27. Field API in D8 Field types are now plugins: https://drupal.org/node/2064123 Entity based storage: https://drupal.org/node/2078765 Form modes: https://drupal.org/node/2014821 Some others...
  28. 28. Goodbye hooks and procedural code! Drupal 7: 85 hooks Drupal 8: 21 hooks and counting down... • hook_field_schema() -> ConfigFieldItemInterface::schema() • hook_field_settings_form() -> ConfigFieldItemInterface::settingsForm() • field_info_field_types() -> Drupal::service('plugin.manager.entity.field.field_type') ->getDefinitions();
  29. 29. still under construction
  30. 30. ...but show me the code!
  31. 31. Addition formatter modules/addition_formatter/lib/Drupal/addition_formatter/Plugin/Field/FieldFormatter/NumberFullAdditionFormatter.php nmsaeDuadiinfratrPuiililFratr aepc rpladto_omtelgnFedFedomte; ueDuaoeFedFedtmitnefc; s rplCrililIeLsItrae ueDuaubrPuiililFratrNmeDcmlomte; s rplnmelgnFedFedomteubreiaFratr /* * *Pui ipeetto o te'ubrfl_diin fratr lgn mlmnain f h nme_uladto' omte. * *@ilFratr Fedomte( * i ="ubrfl_diin, d nme_uladto" * lbl=@rnlto(Nme Fl Adto", ae Tasain"ubr ul diin) * fedtps={ il_ye * "ubritgr, nme_nee" * "ubrdcml, nme_eia" * "ubrfot nme_la" * } , * stig ={ etns * "huadsprtr =", tosn_eaao" " * "eia_eaao"="" dcmlsprtr ., * "cl"="" sae 2, * "rfxsfi"="RE p e i _ u fhttps://github.com/plopesc/drupalCampES2013/tree/8.x/addition_formatter x TU" * }
  32. 32. Arithmetic field type modules/arithmetic_field/lib/Drupal/arithmetic_field/Plugin/Field/FieldType/ArithmeticItem.php nmsaeDuartmtcfedPuiililTp; aepc rplaihei_illgnFedFedye ueDuaoeFedCniFedtmae s rplCrilofgilIeBs; ueDuaililItrae s rplfedFednefc; /* * *Pui ipeetto o te'rtmtc fedtp. lgn mlmnain f h aihei' il ye * *@ilTp( Fedye * i ="rtmtc, d aihei" * lbl=@rnlto(Aihei", ae Tasain"rtmtc) * dsrpin=@rnlto(Fedta rpeet aihei oeain.) ecito Tasain"il ht ersns rtmtc prtos", * dfutwde ="rtmtc, eal_igt aihei" * dfutfratr="rtmtc eal_omte aihei" *) * / casAiheiIe etnsCniFedtmae{ ls rtmtctm xed ofgilIeBs /* * *Dfntoso tecnandpoete. eiiin f h otie rpris https://github.com/plopesc/drupalCampES2013/tree/8.x/arithmetic_field * *@a ary vr ra
  33. 33. Arithmetic widget modules/arithmetic_field/lib/Drupal/arithmetic_field/Plugin/Field/fieldWidget/ArithmeticWidget.php nmsaeDuartmtcfedPuiililWde; aepc rplaihei_illgnFedfedigt ueDuartmtcfedPuiililTprtmtctm s rplaihei_illgnFedFedyeAiheiIe; ueDuaoeFedFedtmitnefc; s rplCrililIeLsItrae ueDuaoeFedWdeBs; s rplCriligtae /* * *Pui ipeetto o te'rtmtc wde. lgn mlmnain f h aihei' igt * *@ilWde( Fedigt * i ="rtmtc, d aihei" * lbl=@rnlto(Aihei", ae Tasain"rtmtc) * fedtps={ il_ye * "rtmtc aihei" * } , * stig ={ etns * "lchle"=" paeodr " * } *) * / c a s A i h e i W d e https://github.com/plopesc/drupalCampES2013/tree/8.x/arithmetic_field ls rtmtcigtetnsWdeBs { xed igtae
  34. 34. Code comparison • Addition formatter module: • Drupal 7: 2 files 1 folder 98 lines of code • Drupal 8: 5 files 7 folders 61 lines of code • Arithmetic field module: • Drupal 7: 3 files 1 folder 128 lines of code • Drupsl 8: 5 files 9 folders 187 lines of code It’s good to have code organized.
  35. 35. Don't stop me now using Field API
  36. 36. Thank you! Drupal.org: plopesc Twitter: @plopesc Fresh fields are waiting for you!!

×