Get on with Field API

36
Get on with Field API Drupalcamp Spain - Cáceres 27/10/2013

Transcript of Get on with Field API

  • 1. Get on with Field API Drupalcamp Spain - Cceres 27/10/2013

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. Terminology Entities Bundles Field Types Field Storage Field Instances 4. Structure Semaphore bundleMug bundleSemaphore colorMug colorEntityInstanceField TypeColor 5. Field API components Field Types API Field Language API Field Info API Field attach API Field CRUD API Field Storage API 6. Field API components Field Types API Field Language API Field Info API Field Attach API Field CRUD API Field Storage API 7. Field Types API Field Type Widget Formatter 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. 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. 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. ... and all for what?? To make our life easier!! 12. Our boss need... Show the sum of all field items 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. Yes, you can use Field API!! 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. 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. But now, what I need... ...is represent basic arithmetic operations and store its results 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. It's so easy using Field API... 20. Field Language API 21. https://drupal.org/node/1500308 22. https://drupal.org/node/1500308 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. How to access to our fields using Field API fedgties il_e_tm (ett_ye $niy $il_ae $agoe=NL) $niytp, ett, fednm, lncd ULfedve_il il_iwfed (ett_ye $niy $il_ae $ipa =ary) $agoe=NL) $niytp, ett, fednm, dsly ra(, lncd ULfedve_au il_iwvle (ett_ye $niy $il_ae $tm $ipa =ary) $agoe=NL) $niytp, ett, fednm, ie, dsly ra(, lncd UL 25. Field API do it for us... 26. ...and Drupal 8?? http://www.flickr.com/photos/37765154@N04 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. 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. still under construction 30. ...but show me the code! 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. 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. 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. 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 codeIts good to have code organized. 35. Don't stop me now using Field API 36. Thank you! Drupal.org: plopesc Twitter: @plopescFresh fields are waiting for you!!