Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Drupal モジュール開発入門講座 第8回 エンティティ

Drupal モジュール開発入門講座 第8回 エンティティ

More Decks by アクイアジャパン Acquia Japan

Other Decks in Programming

Transcript

  1. Drupalͷσʔλͷछྨ DrupalαΠτͰѻΘΕΔσʔλ͸ओʹ4छྨʹ෼ྨ – ContentɿαΠτ๚໰ऀʹදࣔ͢Δ͜ͱΛ໨తͱͨ͠ςΩ ετɺը૾ͳͲͷ৘ใɻ – ConfigurationɿαΠτͷಈ࡞ɺදࣔํ๏ͳͲΛఆٛ͢Δ ৘ใɻ௨ৗ͸খ͞ͳςΩετͰදݱ͞ΕΔ܏޲ɻ – Stateɿcronδϣϒ͕࠷ޙʹ࣮ߦ͞Εͨ࣌ࠁͳͲɺαΠτ

    ͷݱࡏͷঢ়ଶʹؔ͢ΔҰ࣌తͳੑ࣭ͷ৘ใɻ – SessionɿϩάΠϯ͍ͯ͠Δ͔Ͳ͏͔΍CookieͳͲɺݸʑ ͷαΠτ๚໰ऀͷαΠτͱͷ΍ΓऔΓʹؔ͢Δ৘ใɻ https://api.drupal.org/api/drupal/core%21core.api.php/group/info_types/
  2. Drupalͷσʔλͷछྨ DrupalαΠτͰѻΘΕΔσʔλ͸ओʹ4छྨʹ෼ྨ – ContentɿαΠτ๚໰ऀʹදࣔ͢Δ͜ͱΛ໨తͱͨ͠ςΩ ετɺը૾ͳͲͷ৘ใɻ – ConfigurationɿαΠτͷಈ࡞ɺදࣔํ๏ͳͲΛఆٛ͢Δ ৘ใɻ௨ৗ͸খ͞ͳςΩετͰදݱ͞ΕΔ܏޲ɻ – Stateɿcronδϣϒ͕࠷ޙʹ࣮ߦ͞Εͨ࣌ࠁͳͲɺαΠτ

    ͷݱࡏͷঢ়ଶʹؔ͢ΔҰ࣌తͳੑ࣭ͷ৘ใɻ – SessionɿϩάΠϯ͍ͯ͠Δ͔Ͳ͏͔΍CookieͳͲɺݸʑ ͷαΠτ๚໰ऀͷαΠτͱͷ΍ΓऔΓʹؔ͢Δ৘ใɻ https://api.drupal.org/api/drupal/core%21core.api.php/group/info_types/ ΤϯςΟςΟ ※Configurationʹؔͯ͠͸ɺશͯ Ͱ͸ͳ͘Ұ෦ͷσʔλ͕ߏ੒Τϯ ςΟςΟͱͯ͠ѻΘΕΔɻ
  3. ߏ੒ΤϯςΟςΟͱίϯςϯπΤϯςΟςΟͷݟ෼͚ํ ΤϯςΟςΟλΠϓ ΤϯςΟςΟ ίϯςϯπʢϊʔυʣ هࣄ1 هࣄ2 جຊϖʔδ1 λΫιϊϛʔλʔϜ λά1 λά2

    Ϣʔβʔ ؅ཧऀ1 ಗ໊Ϣʔβʔ1 ίϯςϯπλΠϓ هࣄίϯςϯπλΠϓ جຊϖʔδίϯςϯπλΠϓ ϘΩϟϒϥϦ λά ͲΕ͕ߏ੒Τϯ ςΟςΟͰɺͲΕ ͕ίϯςϯπΤϯ ςΟςΟʁ
  4. ΤϯςΟςΟλΠϓ ΤϯςΟςΟ ίϯςϯπʢϊʔυʣ هࣄ1 هࣄ2 جຊϖʔδ1 λΫιϊϛʔλʔϜ λά1 λά2 Ϣʔβʔ

    ؅ཧऀ1 ಗ໊Ϣʔβʔ1 ίϯςϯπλΠϓ هࣄίϯςϯπλΠϓ جຊϖʔδίϯςϯπλΠϓ ϘΩϟϒϥϦ λά ߏ੒ΤϯςΟςΟͱίϯςϯπΤϯςΟςΟͷݟ෼͚ํ ߏ੒ ΤϯςΟςΟ ۩ମతʹ͸ɺΤϯςΟςΟ λΠϓΛఆٛ͢ΔΫϥεͷ ΞϊςʔγϣϯΛݟͯ൑ผ Ͱ͖Δ @ContentEntityTyp e o r @ConfigEntityType ίϯςϯπ ΤϯςΟςΟ
  5. ߏ੒ΤϯςΟςΟͱίϯςϯπΤϯςΟςΟͷҧ͍ ΤϯςΟςΟλΠϓͷఆٛ ΤϯςΟςΟͷྫ σʔλͷ֨ೲ৔ॴ(DB) ߏ੒
 ΤϯςΟςΟ ConfigEntityBaseΛ֦ுͨ͠ Ϋϥε • ը૾ελΠϧ

    • Ϣʔβʔ໾ׂ • ίϯςϯπλΠϓ શͯ config ςʔϒϧʹߦͱ ͯ֨͠ೲ͞Ε·͢ɻ ίϯςϯπ
 ΤϯςΟςΟ ContentEntityBaseΛ֦ுͨ͠ Ϋϥε • ϊʔυ • λΫιϊϛʔλʔ Ϝ • ίϝϯτ • Ϣʔβʔ ίϯςϯπΤϯςΟςΟλ Πϓ͝ͱʹςʔϒϧ͕༻ҙ ͞Εɺ֤ΤϯςΟςΟ͸ߦ ͱͯ֨͠ೲ͞Ε·͢ɻ
  6. ΤϯςΟςΟͷओͳૢ࡞ – 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
  7. – ΤϯςΟςΟͷ࡞੒
 
 
 
 
 
 – ΤϯςΟςΟͷ࡟আ ΤϯςΟςΟͷओͳૢ࡞

    $newNode = \Drupal::entityTypeManager()->getStorage('node')->create([ 'type' => 'article', 'title' => 'Entity APIͰ࡞੒ͨ͠هࣄ', ]); $newNode->save(); $newNode->delete();