• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Get on with Field API
 

Get on with Field API

on

  • 414 views

 

Statistics

Views

Total Views
414
Views on SlideShare
404
Embed Views
10

Actions

Likes
2
Downloads
3
Comments
0

2 Embeds 10

https://twitter.com 8
https://www.linkedin.com 2

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Get on with Field API Get on with Field API Presentation Transcript

    • Get on with Field API Drupalcamp Spain - Cáceres 27/10/2013
    • 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
    • Terminology • Entities • Bundles • Field Types • Field Storage • Field Instances
    • Structure Semaphore bundle Mug bundle Semaphore color Mug color Entity Instance Field Type Color
    • Field API components • Field Types API • Field Language API • Field Info API • Field attach API • Field CRUD API • Field Storage API
    • Field API components • Field Types API • Field Language API • Field Info API • Field Attach API • Field CRUD API • Field Storage API
    • Field Types API • Field Type • Widget • Formatter
    • Field Type • hook_field_info() • hook_field_schema() • hook_field_is_empty() • Other hooks... • hook_field_validate(), hook_field_presave(), hook_field_prepare_translation()...
    • 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()...
    • 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)
    • ... and all for what?? To make our life easier!!
    • Our boss need... Show the sum of all field items
    • 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); } } }
    • Yes, you can use Field API!!
    • 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(, ) , ) ; }
    • 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 }
    • But now, what I need... ...is represent basic arithmetic operations and store its results
    • 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
    • It's so easy using Field API...
    • Field Language API
    • https://drupal.org/node/1500308
    • https://drupal.org/node/1500308
    • 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;
    • 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
    • Field API do it for us...
    • ...and Drupal 8?? http://www.flickr.com/photos/37765154@N04
    • 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...
    • 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();
    • still under construction
    • ...but show me the code!
    • 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" * }
    • 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
    • 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
    • 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.
    • Don't stop me now using Field API
    • Thank you! Drupal.org: plopesc Twitter: @plopesc Fresh fields are waiting for you!!