Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Drupal モジュール開発入門講座 第8回 エンティティ
Search
アクイアジャパン Acquia Japan
November 16, 2022
Programming
0
110
Drupal モジュール開発入門講座 第8回 エンティティ
https://www.acquia.com/jp/events/webinars/drupal-module-development-vol8
アクイアジャパン Acquia Japan
November 16, 2022
Tweet
Share
More Decks by アクイアジャパン Acquia Japan
See All by アクイアジャパン Acquia Japan
Auth0 by OktaとDrupalで実践!認証付きアプリケーションの新しいアプローチ
acquiajp
0
57
Drupal 10.3の新機能紹介ウェビナー
acquiajp
0
110
DrupalCon Portland 2024 総まとめウェビナー
acquiajp
0
39
開発者必見!Acquia Cloud IDEで最高のDrupal開発を始めよう
acquiajp
0
63
触って学ぶ!デジタルアセット管理 Acquia DAM ハンズオンウェビナー
acquiajp
0
18
DrupalCon Lille 2023 総まとめウェビナー
acquiajp
0
41
コンテンツ設計手法で変更に強いアプリケーションを実現する〜ローコードによるWebアプリのバックエンド実装 #CNDF2023
acquiajp
0
330
DrupalCon Pittsburgh 2023 総まとめウェビナー
acquiajp
0
77
Acquia製品概要&Site Studioデモのご紹介
acquiajp
0
75
Other Decks in Programming
See All in Programming
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
みんなでプロポーザルを書いてみた
yuriko1211
0
260
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
600
Figma Dev Modeで変わる!Flutterの開発体験
watanave
0
130
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
110
Remix on Hono on Cloudflare Workers
yusukebe
1
290
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
190
cmp.Or に感動した
otakakot
3
180
ヤプリ新卒SREの オンボーディング
masaki12
0
130
Outline View in SwiftUI
1024jp
1
330
OnlineTestConf: Test Automation Friend or Foe
maaretp
0
110
Featured
See All Featured
Intergalactic Javascript Robots from Outer Space
tanoku
269
27k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
329
21k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
We Have a Design System, Now What?
morganepeng
50
7.2k
It's Worth the Effort
3n
183
27k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
Ruby is Unlike a Banana
tanoku
97
11k
Agile that works and the tools we love
rasmusluckow
327
21k
Why Our Code Smells
bkeepers
PRO
334
57k
Designing for humans not robots
tammielis
250
25k
The Language of Interfaces
destraynor
154
24k
Transcript
ୈ8ճ ΤϯςΟςΟ Drupal Ϟδϡʔϧ ։ൃೖߨ࠲
Drupal Ϟδϡʔϧ։ൃೖߨ࠲γϦʔζ – DrupalͰʮϞδϡʔϧʯͱݺΕΔɺখ͞ͳιϑτΣΞΛ࣮͢Δ͜ͱͰػೳ֦ுΛߦ ͍·͢ɻຊΣϏφʔγϦʔζͰɺDrupalͷϞδϡʔϧ։ൃʹؔ͢ΔجૅࣝΛղઆ͠· ͢ɻ – DrupalͷόοΫΤϯυपΓʹ͍ͭͯମܥతʹֶͼ͍ͨํɺࣗͰΧελϜϞδϡʔϧΛ࡞Ε ΔΑ͏ʹͳΓ͍ͨํɺΞΫΠΞೝఆσϕϩούʔࢼݧΛࢦ͢ํʹ͓͢͢ΊͰ͢ɻ –
DrupalΛҰ௨Γૢ࡞ͨ͜͠ͱ͕͋ΔํΛରͱ͍ͯ͠·͢ɻ·ͩDrupalΛ৮ͬͨ͜ͱ͕ແ͍ ํɺॳ৺ऀ͚DrupalνϡʔτϦΞϧಈըʢຊޠʣΛઌʹݟ͓ͯ͘ͱΑΓཧղ͕ਂ·Γ ·͢ɻ
ΞδΣϯμ 1. ΤϯςΟςΟͱ 2. Entity APIͰΤϯςΟςΟΛૢ࡞͢Δ 3. طଘͷΤϯςΟςΟλΠϓΛݟ͚ͭΔ 4. ಠࣗͷΤϯςΟςΟλΠϓΛ࡞͢Δ
5. ࿅श
ΤϯςΟςΟͱ
DrupalαΠτʹଘࡏ͢Δσʔλ DrupalαΠτʹ ͲͷΑ͏ͳछྨͷ σʔλ͕ଘࡏͯ͠ ͍ΔͩΖ͏͔ʁ
Drupalͷσʔλͷछྨ DrupalαΠτͰѻΘΕΔσʔλओʹ4छྨʹྨ – ContentɿαΠτ๚ऀʹදࣔ͢Δ͜ͱΛతͱͨ͠ςΩ ετɺը૾ͳͲͷใɻ – ConfigurationɿαΠτͷಈ࡞ɺදࣔํ๏ͳͲΛఆٛ͢Δ ใɻ௨ৗখ͞ͳςΩετͰදݱ͞ΕΔɻ – Stateɿcronδϣϒ͕࠷ޙʹ࣮ߦ͞Εͨ࣌ࠁͳͲɺαΠτ
ͷݱࡏͷঢ়ଶʹؔ͢ΔҰ࣌తͳੑ࣭ͷใɻ – SessionɿϩάΠϯ͍ͯ͠Δ͔Ͳ͏͔CookieͳͲɺݸʑ ͷαΠτ๚ऀͷαΠτͱͷΓऔΓʹؔ͢Δใɻ https://api.drupal.org/api/drupal/core%21core.api.php/group/info_types/
Drupalͷσʔλͷछྨ DrupalαΠτͰѻΘΕΔσʔλओʹ4छྨʹྨ – ContentɿαΠτ๚ऀʹදࣔ͢Δ͜ͱΛతͱͨ͠ςΩ ετɺը૾ͳͲͷใɻ – ConfigurationɿαΠτͷಈ࡞ɺදࣔํ๏ͳͲΛఆٛ͢Δ ใɻ௨ৗখ͞ͳςΩετͰදݱ͞ΕΔɻ – Stateɿcronδϣϒ͕࠷ޙʹ࣮ߦ͞Εͨ࣌ࠁͳͲɺαΠτ
ͷݱࡏͷঢ়ଶʹؔ͢ΔҰ࣌తͳੑ࣭ͷใɻ – SessionɿϩάΠϯ͍ͯ͠Δ͔Ͳ͏͔CookieͳͲɺݸʑ ͷαΠτ๚ऀͷαΠτͱͷΓऔΓʹؔ͢Δใɻ https://api.drupal.org/api/drupal/core%21core.api.php/group/info_types/ ΤϯςΟςΟ ※Configurationʹؔͯ͠ɺશͯ Ͱͳ͘Ұ෦ͷσʔλ͕ߏΤϯ ςΟςΟͱͯ͠ѻΘΕΔɻ
ΤϯςΟςΟͱ – ʮίϯςϯπʯ͘͠ʮߏʯͷɺӬଓతͳετϨʔδʹ ༻͞ΕΔΦϒδΣΫτͰ͢ɻ – DrupalͰσʔλΛѻ͏ํ๏ͷҰͭͰ͋Γɺσʔλίϯςϯπ ͷϞσϦϯάΛߦ͍·͢ɻ – ΤϯςΟςΟΛάϧʔϐϯά͢ΔΤϯςΟςΟλΠϓɺ DrupalͷϓϥάΠϯγεςϜʹΑͬͯఆٛ͞Ε·͢ɻ
ΤϯςΟςΟγεςϜʹొ͢Δ༻ޠ – ΤϯςΟςΟɿߏԽ͞Εͨ୯ҰͷΞΠςϜʢΠϯελϯεʣɻ ྫɿ1ͭͷهࣄɺ1ͭͷϢʔβʔ – ΤϯςΟςΟλΠϓɿΤϯςΟςΟΛάϧʔϐϯάͨ͠ͷɻΤϯςΟςΟλΠϓ EntityTypeManagerʹΑͬͯఆٛ͞ΕΔϓϥάΠϯλΠϓͷϓϥάΠϯɻ ྫɿίϯςϯπ(ϊʔυ)ɺλΫιϊϛʔ – όϯυϧɿαϒλΠϓͱݺΕΔɻΤϯςΟςΟλΠϓΛࡉԽͨ͠ͷɻόϯυ
ϧΛ༻ҙ͢Δ͔ҙɻ௨ৗɺόϯυϧߏΤϯςΟςΟʹΑͬͯఆٛ͞ΕΔɻ ྫɿهࣄίϯςϯπλΠϓɺλά
ΤϯςΟςΟͷ֊ ΤϯςΟςΟλΠϓ όϯυϧ ΤϯςΟςΟ ίϯςϯπʢϊʔυʣ هࣄίϯςϯπλΠϓ هࣄ1 هࣄ2 جຊϖʔδίϯςϯπ λΠϓ
جຊϖʔδ1 λΫιϊϛʔλʔϜ λά λά1 λά2 Ϣʔβʔ ʢແ͠ʣ ཧऀ1 ಗ໊Ϣʔβʔ1
– ߏΤϯςΟςΟɿαΠτͷߏՄೳͳઃఆͷใΛѻ͏Φϒ δΣΫτͰ͢ɻߏγεςϜͰ༻͞Ε·͢ɻ༁Λαϙʔτ͠ ͍ͯ·͢ɻϞδϡʔϧͷΠϯετʔϧ࣌ʹಠࣗͷσϑΥϧτઃఆ ΛఏڙͰ͖·͢ɻ – ίϯςϯπΤϯςΟςΟɿઃఆՄೳͳϑΟʔϧυͱجຊϑΟʔϧ υͰߏ͞Ε·͢ɻϦϏδϣϯ༁Λαϙʔτ͍ͯ͠·͢ɻ ߏΤϯςΟςΟͱίϯςϯπΤϯςΟςΟ
ߏΤϯςΟςΟͱίϯςϯπΤϯςΟςΟͷݟ͚ํ ΤϯςΟςΟλΠϓ ΤϯςΟςΟ ίϯςϯπʢϊʔυʣ هࣄ1 هࣄ2 جຊϖʔδ1 λΫιϊϛʔλʔϜ λά1 λά2
Ϣʔβʔ ཧऀ1 ಗ໊Ϣʔβʔ1 ίϯςϯπλΠϓ هࣄίϯςϯπλΠϓ جຊϖʔδίϯςϯπλΠϓ ϘΩϟϒϥϦ λά ͲΕ͕ߏΤϯ ςΟςΟͰɺͲΕ ͕ίϯςϯπΤϯ ςΟςΟʁ
ΤϯςΟςΟλΠϓ ΤϯςΟςΟ ίϯςϯπʢϊʔυʣ هࣄ1 هࣄ2 جຊϖʔδ1 λΫιϊϛʔλʔϜ λά1 λά2 Ϣʔβʔ
ཧऀ1 ಗ໊Ϣʔβʔ1 ίϯςϯπλΠϓ هࣄίϯςϯπλΠϓ جຊϖʔδίϯςϯπλΠϓ ϘΩϟϒϥϦ λά ߏΤϯςΟςΟͱίϯςϯπΤϯςΟςΟͷݟ͚ํ ߏ ΤϯςΟςΟ ۩ମతʹɺΤϯςΟςΟ λΠϓΛఆٛ͢ΔΫϥεͷ ΞϊςʔγϣϯΛݟͯผ Ͱ͖Δ @ContentEntityTyp e o r @ConfigEntityType ίϯςϯπ ΤϯςΟςΟ
ߏΤϯςΟςΟͱίϯςϯπΤϯςΟςΟͷҧ͍ ΤϯςΟςΟλΠϓͷఆٛ ΤϯςΟςΟͷྫ σʔλͷ֨ೲॴ(DB) ߏ ΤϯςΟςΟ ConfigEntityBaseΛ֦ுͨ͠ Ϋϥε • ը૾ελΠϧ
• Ϣʔβʔׂ • ίϯςϯπλΠϓ શͯ config ςʔϒϧʹߦͱ ͯ֨͠ೲ͞Ε·͢ɻ ίϯςϯπ ΤϯςΟςΟ ContentEntityBaseΛ֦ுͨ͠ Ϋϥε • ϊʔυ • λΫιϊϛʔλʔ Ϝ • ίϝϯτ • Ϣʔβʔ ίϯςϯπΤϯςΟςΟλ Πϓ͝ͱʹςʔϒϧ͕༻ҙ ͞Εɺ֤ΤϯςΟςΟߦ ͱͯ֨͠ೲ͞Ε·͢ɻ
ΤϯςΟςΟͱ ·ͱΊ – ΤϯςΟςΟɺʮίϯςϯπʯ͘͠ʮߏʯͷɺӬଓతͳε τϨʔδʹ༻͞ΕΔΦϒδΣΫτͰ͢ɻ – DrupalͰσʔλΛѻ͏ํ๏ͷҰͭͰ͋ΓɺσʔλίϯςϯπͷϞ σϦϯάΛߦ͍·͢ɻ – ΤϯςΟςΟΤϯςΟςΟλΠϓʹΑͬͯɺ߹ʹΑͬͯ͞Β
ʹόϯυϧʹΑͬͯࡉԽ͞Ε·͢ɻ – ΤϯςΟςΟʹߏΤϯςΟςΟͱίϯςϯπΤϯςΟςΟͷ2छ ྨ͕ଘࡏ͠·͢ɻ
Entity APIͰΤϯςΟςΟΛૢ࡞͢Δ
entity_type.managerαʔϏε – entity_type.managerαʔϏεΛհͯ͠ɺΤϯςΟςΟͷಡΈࠐΈɺ࡞ɺ আͳͲͷૢ࡞͕ߦ͑·͢ɻ – entity_type.managerαʔϏεɺΤϯςΟςΟλΠϓʹґଘ͢Δ͜ͱͳ͘ɺ શͯͷΤϯςΟςΟΛѻ͏͜ͱ͕Ͱ͖·͢ɻ
ΤϯςΟςΟͷओͳૢ࡞ – 1ͭͷΤϯςΟςΟΛಡΈࠐΉ – ΤϯςΟςΟIDͷऔಘ – ΤϯςΟςΟͷόϯυϧͷऔಘ –
ΤϯςΟςΟͷ֤ϑΟʔϧυͷऔಘ $node = \Drupal::entityTypeManager()->getStorage('node')->load(1); $node->id(); // "1" $node->bundle(); // "article" $node->get('title')->value; // "Test Article" https://www.drupal.org/docs/drupal-apis/entity-api/working-with-the-entity-api https://www.metaltoad.com/blog/drupal-8-entity-api-cheat-sheet
– ΤϯςΟςΟͷ࡞ – ΤϯςΟςΟͷআ ΤϯςΟςΟͷओͳૢ࡞
$newNode = \Drupal::entityTypeManager()->getStorage('node')->create([ 'type' => 'article', 'title' => 'Entity APIͰ࡞ͨ͠هࣄ', ]); $newNode->save(); $newNode->delete();
ԋश entity_type.managerαʔϏεΛͬͯΤϯςΟςΟΛૢ࡞ͯ͠ΈΑ ͏ʂ 1. ΤϯςΟςΟͷಡΈࠐΈ 2. ΤϯςΟςΟ͕࣋ͭͷऔಘ 3. ΤϯςΟςΟͷ࡞ 4.
ΤϯςΟςΟͷআ ίʔυͪ͜Βˠ https://github.com/hmaruyama/my-drupal9-module/compare/vol8
طଘͷΤϯςΟςΟλΠϓΛݟ͚ͭΔ
ΤϯςΟςΟλΠϓͲ͔͜Βఏڙ͞ΕΔʁ – ίΞ(αϒγεςϜ/ίΞϞδϡʔϧ ) – ίϯτϦϏϡʔτϞδϡʔϧ – ΧελϜϞδϡʔϧ
طଘͷΤϯςΟςΟλΠϓΛݟ͚ͭΔํ๏ – ίʔυϕʔε͔Β@ContentEntityType @ConfigEntityTypeͰจࣈྻݕࡧ͢Δ – DevelϞδϡʔϧΛ༻͢Δ
– ΤϯςΟςΟλΠϓͷఆٛʹɺϓϥάΠϯγεςϜͷAnnotatedDiscoveryݕग़͕༻͍ΒΕ ͍ͯ·͢ɻ – ͳͷͰɺίʔυϕʔεͷPHPϑΝΠϧΛ @ContentEntityType @ConfigEntityType Ͱจ ࣈྻݕࡧ͢Δ͜ͱͰɺίʔυϕʔεʹଘࡏ͢ΔΤϯςΟςΟλΠϓΛݟ͚ͭΔ͜ͱ͕Ͱ͖·
͢ɻ ίʔυϕʔε͔Β@ContentEntityType@ConfigEntityTypeͰ จࣈྻݕࡧ͢Δ
– ίϯτϦϏϡʔτϞδϡʔϧͷʮDevelʯϞδϡʔϧΛΠϯετʔϧ/༗ޮԽͯ͠ɺ/devel/ entity/info ϖʔδʹΞΫηε͠·͢ɻ – αΠτʹଘࡏ͢ΔશͯͷΤϯςΟςΟλΠϓͷҰཡ͕֬ೝͰ͖·͢ɻʢίΞɺίϯτϦ ϏϡʔτϞδϡʔϧɺΧελϜϞδϡʔϧʣ DevelϞδϡʔϧΛ༻͢Δ
ಠࣗͷΤϯςΟςΟλΠϓΛ࡞͢Δ
ಠࣗͷΤϯςΟςΟλΠϓΛ࡞͢Δ – ΤϯςΟςΟλΠϓϓϥάΠϯγεςϜʹΑͬͯߏ͞Εͯ ͍·͢ɻ – ϓϥάΠϯλΠϓ͔ΒϓϥάΠϯΛ࡞͢ΔཁྖͰɺಠࣗͷΤ ϯςΟςΟλΠϓΛఆٛ͢Δ͜ͱ͕Ͱ͖·͢ɻ – ϓϥάΠϯͷݕग़ํ๏AnnotatedDiscovery͕༻͞Ε͍ͯ· ͢ɻܾΊΒΕͨσΟϨΫτϦߏͰPHPϑΝΠϧΛஔ͢Δ͜
ͱͰɺΤϯςΟςΟλΠϓΛఆٛͰ͖·͢ɻ
ΧελϜϞδϡʔϧͰಠࣗͷίϯςϯπΤϯςΟςΟλΠ ϓΛ࡞͢Δํ๏ ࠓճྫͱͯ͠ɺίϯςϯπΤϯςΟςΟλΠϓΛఆٛ͢Δํ๏Λհ͠·͢ɻ 1. ίϯςϯπΤϯςΟςΟλΠϓΛఆٛ͢ΔΫϥεΛ༻ҙ͠·͢ɻ a. src/EntityσΟϨΫτϦʹΫϥεϑΝΠϧΛ࡞͠·͢ɻ b. ContentEntityBaseΛ֦ுɺContentEntityInterfaceΛ࣮ͨ͠ΫϥεΛ ఆٛ͠·͢ɻ
c. ΞϊςʔγϣϯͰϝλσʔλΛهड़͠·͢ɻ d. baseFieldDefinitionsؔͰجຊϑΟʔϧυΛՃ͠·͢ɻ
ԋश ಠࣗͷίϯςϯπΤϯςΟςΟλΠϓΛఆٛͯ͠ɺར༻ͯ͠ΈΑ͏ʂ 1. ίϯςϯπΤϯςΟςΟλΠϓͷఆٛ 2. entity_type.managerαʔϏεΛ༻ͯ͠ɺ࡞ͨ͠ΤϯςΟςΟ λΠϓͷΤϯςΟςΟΛ࡞͢Δ 3. ΤϯςΟςΟ͕࡞͞Ε͍ͯΔ͔֬ೝ͢Δ ίʔυͪ͜Βˠ
https://github.com/hmaruyama/my-drupal9-module/compare/vol8
࿅श
࿅श1 DrupalͷʮΤϯςΟςΟʯʹ͍ͭͯਖ਼͍͠هड़Λબ͍ͯͩ͘͠͞ɻ 1. ʮίϯςϯπʯ͘͠ʮߏʯͷɺӬଓతͳετϨʔδʹ ༻͞ΕΔΦϒδΣΫτɻ 2. αΠτͷݱࡏͷঢ়ଶʹؔ͢ΔҰ࣌తͳੑ࣭ͷใΛѻ͏σʔλɻ 3. Drupalͷಈ࡞Λมߋ͢ΔPHPؔɻ 4.
Doctrine ORMΛհͯ͠σʔλૢ࡞Ͱ͖Δɻ
࿅श1 DrupalͷʮΤϯςΟςΟʯʹ͍ͭͯਖ਼͍͠هड़Λબ͍ͯͩ͘͠͞ɻ 1. ʮίϯςϯπʯ͘͠ʮߏʯͷɺӬଓతͳετϨʔδʹ ༻͞ΕΔΦϒδΣΫτɻ 2. αΠτͷݱࡏͷঢ়ଶʹؔ͢ΔҰ࣌తͳੑ࣭ͷใΛѻ͏σʔλɻ 3. Drupalͷಈ࡞Λมߋ͢ΔPHPؔɻ 4.
Doctrine ORMΛհͯ͠σʔλૢ࡞Ͱ͖Δɻ
࿅श2 DrupalͷʮΤϯςΟςΟλΠϓʯʹ͍ͭͯਖ਼͍͠هड़Λબ͍ͯͩ͘͠͞ɻ 1. Plugin APIͰߏ͞Ε͍ͯΔɻ 2. ϑοΫͰΤϯςΟςΟλΠϓΛఆٛͰ͖Δɻ 3. ΤϯςΟςΟλΠϓʹඞͣόϯυϧΛ༻ҙ͢Δඞཁ͕͋Δɻ 4.
ߏԽ͞Εͨ୯ҰͷΞΠςϜɻ
࿅श2 DrupalͷʮΤϯςΟςΟλΠϓʯʹ͍ͭͯਖ਼͍͠هड़Λબ͍ͯͩ͘͠͞ɻ 1. Plugin APIͰߏ͞Ε͍ͯΔɻ 2. ϑοΫͰΤϯςΟςΟλΠϓΛఆٛͰ͖Δɻ 3. ΤϯςΟςΟλΠϓʹඞͣόϯυϧΛ༻ҙ͢Δඞཁ͕͋Δɻ 4.
ߏԽ͞Εͨ୯ҰͷΞΠςϜɻ
ຊͷΞδΣϯμऴྃ
ΞδΣϯμ 1. ΤϯςΟςΟͱ 2. Entity APIͰΤϯςΟςΟΛૢ࡞͢Δ 3. طଘͷΤϯςΟςΟλΠϓΛݟ͚ͭΔ 4. ಠࣗͷΤϯςΟςΟλΠϓΛ࡞͢Δ
5. ࿅श
None