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

Drupal モジュール開発入門講座 第7回 プラグイン

Drupal モジュール開発入門講座 第7回 プラグイン

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

Other Decks in Programming

Transcript

  1. ୈ7ճ ϓϥάΠϯ Drupal Ϟδϡʔϧ ։ൃೖ໳ߨ࠲

  2. Drupal Ϟδϡʔϧ։ൃೖ໳ߨ࠲γϦʔζ – DrupalͰ͸ʮϞδϡʔϧʯͱݺ͹ΕΔɺখ͞ͳιϑτ΢ΣΞΛ࣮૷͢Δ͜ͱͰػೳ֦ுΛ ߦ͍·͢ɻຊ΢ΣϏφʔγϦʔζͰ͸ɺDrupalͷϞδϡʔϧ։ൃʹؔ͢Δجૅ஌ࣝΛղઆ ͠·͢ɻ – DrupalͷόοΫΤϯυपΓʹ͍ͭͯମܥతʹֶͼ͍ͨํɺࣗ෼ͰΧελϜϞδϡʔϧΛ࡞ ΕΔΑ͏ʹͳΓ͍ͨํɺΞΫΠΞೝఆσϕϩούʔࢼݧΛ໨ࢦ͢ํʹ͓͢͢ΊͰ͢ɻ –

    DrupalΛҰ௨Γૢ࡞ͨ͜͠ͱ͕͋ΔํΛର৅ͱ͍ͯ͠·͢ɻ·ͩDrupalΛ৮ͬͨ͜ͱ͕ແ ͍ํ͸ɺॳ৺ऀ޲͚DrupalνϡʔτϦΞϧಈըʢ೔ຊޠʣΛઌʹݟ͓ͯ͘ͱΑΓཧղ͕ਂ ·Γ·͢ɻ
  3. ΞδΣϯμ 1. ϓϥάΠϯͱ͸ 2. ϓϥάΠϯλΠϓΛར༻ͯ͠ϓϥάΠϯΛ࡞੒͢Δ 3. Drupal͕ϓϥάΠϯΛݕग़͢Δํ๏ 4. طଘͷϓϥάΠϯλΠϓΛݟ͚ͭΔ 5.

    ಠࣗͷϓϥάΠϯλΠϓΛ࡞੒͢Δ 6. ࿅श໰୊
  4. ϓϥάΠϯͱ͸

  5. ϓϥάΠϯͷ֓ཁ – DrupalͷϓϥάΠϯγεςϜͱ͸ɺಛఆͷจ຺Ͱ܁Γฦ͠ൃੜ͢Δ ࣄฑʹରԠ͢ΔͨΊͷɺ࠶ར༻Մೳͳ࢓૊ΈͰ͢ɻ – Ϟδϡʔϧ/αϒγεςϜʹΑͬͯɺػೳͷ਽ܗ͕ఆٛ͞ΕɺଞͷϞ δϡʔϧͰɺಛఆͷಈ࡞Λ࣋ͭϓϥάΠϯΛ࡞੒Ͱ͖·͢ɻ – ࣅͨΑ͏ͳػೳΛ࣋ͭϓϥάΠϯ͸ɺಉ͡ϓϥάΠϯλΠϓʹଐ͠ ·͢ɻ

    – DrupalͷϓϥάΠϯγεςϜ͸ɺDrupalݻ༗ͷσβΠϯύλʔϯͱݴ ͑·͢ɻ
  6. – DrupalͷϒϩοΫ͸ɾɾɾ
 – ϒϩοΫϨΠΞ΢τͷը໘Ͱ೚ҙͷϦʔδϣ ϯʹ഑ஔՄೳ – ϖʔδ୯Ґɺϩʔϧ୯ҐͷӾཡ੍ޚ͕Մೳ – ຋༁͕Մೳ
 –

    ͱ͍͏ɺڞ௨ͷΠϯλʔϑΣʔεΛ͍࣋ͬͯΔ – ϓϥάΠϯγεςϜʹΑͬͯɺ֤ϒϩοΫʹڞ௨ͷ ΠϯλʔϑΣʔεΛ࣋ͨͤͭͭɺܾΊΒΕͨൣғ ͰΧελϚΠζͰ͖Δ ϓϥάΠϯͷྫɿϒϩοΫ
  7. ϓϥάΠϯγεςϜʹొ৔͢Δ༻ޠ – ϓϥάΠϯɿަ׵Մೳͳখ͞ͳػೳɻΧελϜϞδϡʔϧ಺Ͱ ೚ҙͷϓϥάΠϯλΠϓͷϓϥάΠϯΛ࡞੒Ͱ͖ΔɻϓϥάΠϯ ͸ɺDrupalίʔυͷ೚ҙͷ৔ॴͰݕग़ɺΠϯελϯεԽ͞Ε Δɻ – ϓϥάΠϯλΠϓɿϓϥάΠϯΛάϧʔϐϯάͨ͠΋ͷɻϓϥά ΠϯϚωʔδϟ(ޙड़)ʹΑͬͯϓϥάΠϯλΠϓ͕ఆٛ͞ΕΔɻ

  8. ϓϥάΠϯλΠϓ ϓϥάΠϯ Πϯελϯε Block
 (ίΞͷBlock
 αϒεεςϜͰ ఆٛ) Search form
 (ίΞͷSearchϞδϡʔϧͰఆٛ)

    Search form (narrow) Search form (wide) Language switche r (ίΞͷLanguageϞδϡʔϧͰఆٛ) Language switcher ϓϥάΠϯλΠϓͱϓϥάΠϯ
  9. ଞʹ΋৭ʑɺϓϥάΠϯλΠϓͱϓϥάΠϯ ϓϥάΠϯλΠϓ ϓϥάΠϯ FieldFormatte r (ίΞͷFieldαϒγεςϜͰఆٛ) Plain text Trimmed ImageEffec

    t (ίΞͷImageϞδϡʔϧͰఆٛ) Scale Crop Resize ViewsStyl e (ίΞͷViewsϞδϡʔϧͰఆٛ) Unformatted list HTML list
  10. ϓϥάΠϯͱ͸ ·ͱΊ – ϓϥάΠϯγεςϜͱ͸ɺಛఆͷจ຺Ͱ܁Γฦ͠ൃੜ͢Δࣄฑʹ ରԠ͢ΔͨΊͷɺ࠶ར༻Մೳͳ࢓૊ΈͰ͢ɻ – ϓϥάΠϯͱ͸ɺަ׵Մೳͳখ͞ͳػೳͰ͢ɻ – ϓϥάΠϯλΠϓͱ͸ɺϓϥάΠϯΛάϧʔϐϯάͨ͠΋ͷͰ͢ɻ

  11. ϓϥάΠϯλΠϓΛར༻ͯ͠ ϓϥάΠϯΛ࡞੒͢Δ

  12. ϓϥάΠϯλΠϓΛར༻ͯ͠ϓϥάΠϯΛ࡞੒͢Δํ๏ ϓϥάΠϯλΠϓʹΑͬͯํ๏͸ଟগҟͳΓ·͕͢ɺ͜͜Ͱ͸ AnnotatedClassDiscoveryͷݕग़ํ๏(ޙड़)͕ར༻͞Ε͍ͯΔྫΛ঺հ͠· ͢ɻ
 1. ద੾ͳσΟϨΫτϦʹphpϑΝΠϧΛ഑ஔͯ͠ɾɾɾ a. ඞཁͳϝιου͕ఆٛ͞ΕΔΑ͏ʹɺϓϥάΠϯΠϯλʔϑΣʔε Λ࣮૷͠·͢ɻجຊΫϥε͕ఏڙ͞Ε͍ͯΔ৔߹͸ͦΕΛextends ͠·͢ɻ

    b. ΫϥεϑΝΠϧʹద੾ͳΞϊςʔγϣϯΛهड़͠·͢ɻ c. ඞཁͳϝιουΛهड़͠·͢ɻ
  13. ϒϩοΫϓϥάΠϯλΠϓͷϓϥάΠϯΛ࡞੒͢Δྫ <?php namespace Drupal\marucha\Plugin\Block; use Drupal\Core\Block\BlockBase; /** * Provide a

    custom block. * * @Block( * id = "marucha_block", * admin_label = @Translation("Marucha Block"), * ) */ class MaruchaBlock extends BlockBase { /** * {@inheritDoc} */ public function build() { return ['#markup' => $this->t('This is a Block!'),]; } } src/Plugin/Block/MaruchaBlock.php ΞϊςʔγϣϯͰ ϒϩοΫID΍ ؅ཧ༻ϥϕϧΛ ఆٛ͢Δ ࡞੒ͨ͠ϒϩοΫ͸ ؅ཧը໘ͷϒϩοΫϨΠΞ΢τ ϖʔδͰαΠτʹ഑ஔͰ͖ΔΑ ͏ʹͳΔ build() ϝιουͰ ϨϯμϦϯά഑ྻ Λฦ٫͢Δ
  14. ԋश ϒϩοΫϓϥάΠϯλΠϓͷϓϥάΠϯΛ࡞੒ͯ͠ΈΑ͏ʂ
 1. ϞδϡʔϧͷσΟϨΫτϦ௚Լʹ
 src/Plugin/Block/MaruchaBlock.php ϑΝΠϧΛ࡞੒͢Δ 2. BlockBaseΛ֦ு͢Δ 3. ΞϊςʔγϣϯͰϒϩοΫͷϝλσʔλΛఆٛ͢Δ

    4. ඞཁͳϝιουΛ࣮૷͢Δ 5. Drupal؅ཧը໘͔Β ϒϩοΫϨΠΞ΢τ ʹҠಈͯ͠ɺ
 ೚ҙͷϦʔδϣϯʹ࡞੒ͨ͠ϒϩοΫϓϥάΠϯΛ഑ஔ͢Δ ίʔυ͸ͪ͜Βˠ https://github.com/hmaruyama/my-drupal9-module/compare/vol7
  15. Drupal͕ϓϥάΠϯΛݕग़͢Δํ๏

  16. ϓϥάΠϯσΟεΧόϦ ϓϥάΠϯσΟεΧόϦ(Plugin Discovery)ͱݺ͹ΕΔɺίʔυϕʔε্Ͱϓ ϥάΠϯΛݕग़͢Δํ๏͕4ͭଘࡏ͠·͢ɻ 
 ୭͕ɺ͍ͭɺϓϥάΠϯσΟεΧόϦΛؾʹ͢Δඞཁ͕͋Δ͔ʁ – ಠࣗͷϓϥάΠϯλΠϓΛ࡞੒͢Δ৔໘ɿϓϥάΠϯϚωʔδϟΫϥε ͷఆٛͷࡍʹɺ͍ͣΕ͔ͷݕग़ํ๏Λબ୒͢Δඞཁ͕͋Γ·͢ɻ –

    ϓϥάΠϯΛ࡞੒͢Δ৔໘ɿϓϥάΠϯλΠϓ͕ར༻͢Δݕग़ํ๏ʹ ଇ࣮ͬͯ૷͢Δඞཁ͕͋Γ·͢ɻ
  17. ϓϥάΠϯσΟεΧόϦ 4ͭͷҟͳΔํ๏ – AnnotatedClassDiscoveryɿdocblockͰϝλσʔλΛಡΈࠐΉํ๏Ͱ͢ɻϓϥάΠϯΛ࡞ ੒͢Δଆ͸ɺܾΊΒΕͨσΟϨΫτϦߏ੒ͰϓϥάΠϯΫϥεΛ഑ஔͯ͠ɺΞϊςʔ γϣϯͰϝλσʔλΛఆٛ͠·͢ɻ΄ͱΜͲͷϓϥάΠϯλΠϓ͕͜ͷݕग़ํ๏Λར༻ ͍ͯ͠·͢ɻ – YamlDiscoveryɿYMLϑΝΠϧͰϝλσʔλΛಡΈࠐΉํ๏Ͱ͢ɻϓϥάΠϯΛ࡞੒͢Δ ଆ͸ɺܾΊΒΕͨϑΝΠϧ໊ͰYMLϑΝΠϧΛ഑ஔͯ͠ɺYMLܗࣜͰϝλσʔλΛఆٛ

    ͠·͢ɻMenuLink΍BreakpointsͳͲͷҰ෦ͷϓϥάΠϯλΠϓͰར༻͞Ε͍ͯ·͢ɻ – StaticDiscovery – HookDiscovery ΄ͱΜͲར༻͞Ε͍ͯͳ͍ʁ
  18. طଘͷϓϥάΠϯλΠϓΛݟ͚ͭΔ

  19. ϓϥάΠϯλΠϓ͸Ͳ͔͜Βఏڙ͞ΕΔʁ – ίΞ(αϒγεςϜ/ίΞϞδϡʔϧ ) – ίϯτϦϏϡʔτϞδϡʔϧ – ΧελϜϞδϡʔϧ

  20. طଘͷϓϥάΠϯλΠϓΛݟ͚ͭΔํ๏ – ΞϊςʔγϣϯΫϥεΛ୳͢ – *.services.yml͔Β୳͢

  21. ΞϊςʔγϣϯΫϥεΛ୳͢ – ݱ࣌఺Ͱ͸΄ͱΜͲͷϓϥάΠϯλΠϓ͕ʮAnnotatedClassDiscoveryʯΛ࢖༻ͯ͠ ϓϥάΠϯΛݕग़͠·͢ɻ – ͔ͭɺݱ࣌఺ͰΞϊςʔγϣϯ͸ϓϥάΠϯγεςϜͰͷΈ࢖༻͞Ε͍ͯ·͢ɻ – ͭ·ΓΞϊςʔγϣϯΫϥεΛ୳͢͜ͱͰɺ΄ͱΜͲͷϓϥάΠϯλΠϓΛݟ͚ͭΔ ͜ͱ͕Ͱ͖·͢ɻ –

    api.drupal.orgͰ͸ɺAnnotationsϖʔδ͔ΒDrupalίΞʹଘࡏ͢ΔΞϊςʔγϣϯΫ ϥεͷҰཡΛݟΔ͜ͱ͕Ͱ͖·͢ɻ – Drupalιʔείʔυ্͔Β΋ϑΝΠϧݕࡧͰݟ͚ͭΔ͜ͱ͕Ͱ͖·͢ɻ
  22. *.services.yml͔Β୳͢ – ϓϥάΠϯλΠϓͷ֤ϓϥάΠϯ͸ɺϓϥάΠϯϚωʔδϟʢαʔϏεʣΛհ͠ ͯݺͼग़͞Ε·͢ɻ – *.services.ymlʹొ࿥͞ΕͨϓϥάΠϯϚωʔδϟͷϚγϯ໊͸ plugin.manager.* ͱ͍͏໊લʹͳ͍ͬͯΔ΋ͷ͕ଟ͍Ͱ͢ɻ※શͯͰ͸ແ͍ – ࣮֬ͳํ๏Ͱ͸ແ͍Ͱ͕͢ɺ*services.ymlϑΝΠϧ಺Λ

    plugin.manager. Ͱจࣈ ྻݕࡧ͢Δ͜ͱͰҰ෦ͷϓϥάΠϯλΠϓΛݟ͚ͭΔ͜ͱ͕Ͱ͖·͢ɻ
  23. ࢀߟ) ViewsϞδϡʔϧͷ৔߹ – ίΞͷViewsϞδϡʔϧ͔Βɺ͍͔ͭ͘ͷ ϓϥάΠϯλΠϓ͕ఏڙ͞Ε͍ͯ·͢ɻ – Viewsͷ৔߹͸ɺDrupal؅ཧը໘ͷϨϙʔ τ > Views

    ϓϥάΠϯ ͷϖʔδ͔Βɺ ViewsϞδϡʔϧͰఏڙ͞Ε͍ͯΔϓϥά ΠϯλΠϓҰཡΛݟΔ͜ͱ͕Ͱ͖·͢ɻ
  24. ಠࣗͷϓϥάΠϯλΠϓΛ࡞੒͢Δ

  25. ΧελϜϞδϡʔϧͰಠࣗͷϓϥάΠϯλΠϓΛ ࡞੒͢Δํ๏ AnnotatedClassDiscoveryͷݕग़ํ๏Λ༻͍ͨྫΛ঺հ͠·͢ɻ 1. ϓϥάΠϯϚωʔδϟΫϥεΛ༻ҙ͠·͢ɻ a. Ϋϥε಺ͰϓϥάΠϯͷݕग़ํ๏΋ఆ͍͖ٛͯ͠·͢ɻ b. ࡞੒ͨ͠ΫϥεΛ*.services.ymlʹొ࿥͠·͢ɻ 2.

    ΞϊςʔγϣϯΫϥεΛ༻ҙ͠·͢ɻ a. ϓϥάΠϯͰར༻͍ͨ͠ϝλσʔλΛఆٛ͠·͢ɻ 3. ΠϯλʔϑΣʔεΛ༻ҙ͠·͢ɻ a. ϓϥάΠϯͰ࣮૷ͯ͠΄͍͠ϝιουΛఆٛ͠·͢ɻ 4. جຊΫϥεΛ༻ҙ͠·͢ɻʢΦϓγϣϯʣ a. ΠϯλʔϑΣʔεΛ࣮૷ͯ͠جຊΫϥεΛ༻ҙͯ͠ɺϝιουͷσϑΥϧτͷॲཧΛهड़ ͠·͢ɻ
  26. ϓϥάΠϯϚωʔδϟͱ͸ – ϓϥάΠϯϚωʔδϟ͸ɺ࣍ͷ͜ͱΛߦ͍·͢ɻ – ৽͍͠ϓϥάΠϯλΠϓͷఆٛ – ϓϥάΠϯͷݕग़ํ๏ͷఆٛ – ϓϥάΠϯͷݕग़ͱΠϯελϯεԽ
 –

    ϓϥάΠϯϚωʔδϟ͸αʔϏεͱͯ͠ఆٛ͞Ε·͢ɻ
 αʔϏεͷϚγϯ໊ʹɺplugin.manager.* ͷ໋໊نଇ͕Α͘ར ༻͞Ε·͢ɻ
  27. ϓϥάΠϯϚωʔδϟΛ࢖༻͢Δ ϓϥάΠϯϚωʔδϟαʔϏεΛݺͼग़ͯ͠ɾɾ
 
 – ར༻ՄೳͳϓϥάΠϯͷҰཡΛऔಘ͢Δ
 
 – ಛఆͷϓϥάΠϯΛऔಘ͢Δ
 
 –

    ϓϥάΠϯͷߏ੒ࡁΈΠϯελϯεΛ࡞੒͢Δ $type = \Drupal::service('plugin.manager.tapioca'); $plugin_definitions = $type->getDefinitions(); $plugin_definition = $type->getDefinition('tapioca_milk_tea'); $plugin = $type->createInstance('tapioca_milk_tea', ['of' => 'configuration values']);
  28. ԋश ಠࣗͷϓϥάΠϯλΠϓΛఆٛͯ͠ɺར༻ͯ͠ΈΑ͏ʂ 1. ϓϥάΠϯλΠϓͷఆٛ 2. ϓϥάΠϯϚωʔδϟΛ࢖༻ͯ͠ɺ࡞੒ͨ͠ϓϥάΠϯλΠϓͷϓϥά ΠϯΛݺͼग़͢ 3. ࡞੒ͨ͠ϓϥάΠϯλΠϓͰϓϥάΠϯΛ࡞੒͢Δ ίʔυ͸ͪ͜Βˠ

    https://github.com/hmaruyama/my-drupal9-module/compare/vol7
  29. ࿅श໰୊

  30. ࿅श໰୊1 DrupalͷʮϓϥάΠϯʯʹ͍ͭͯਖ਼͍͠هड़Λબ୒͍ͯͩ͘͠͞ɻ 1. Drupalͷಈ࡞Λมߋ͢ΔPHPؔ਺ɻ 2. ʮϞδϡʔϧʯͷผ໊ɻ 3. ϓϥάΠϯλΠϓʹάϧʔϐϯά͞ΕΔɺަ׵Մೳͳػೳɻ

  31. ࿅श໰୊1 DrupalͷʮϓϥάΠϯʯʹ͍ͭͯਖ਼͍͠هड़Λબ୒͍ͯͩ͘͠͞ɻ 1. Drupalͷಈ࡞Λมߋ͢ΔPHPؔ਺ɻ 2. ʮϞδϡʔϧʯͷผ໊ɻ 3. ϓϥάΠϯλΠϓʹάϧʔϐϯά͞ΕΔɺަ׵Մೳͳػೳɻ

  32. ࿅श໰୊2 ϓϥάΠϯσΟεΧόϦʹ͍ͭͯਖ਼͘͠ͳ͍هड़Λબ୒͍ͯͩ͘͠͞ɻ 1. DrupalίΞͰ͸AnnotatedClassDiscoveryͱݺ͹ΕΔݕग़ํ๏ͷΈ ఏڙ͞Ε͍ͯΔɻ 2. ϓϥάΠϯϚωʔδϟͰɺϓϥάΠϯλΠϓ͕࢖༻͢Δݕग़ํ๏Λ ఆٛ͢Δɻ 3. ಠࣗʹݕग़ํ๏Λఆٛ͢Δ͜ͱ͕Ͱ͖Δɻ

  33. ࿅श໰୊2 ϓϥάΠϯσΟεΧόϦʹ͍ͭͯਖ਼͘͠ͳ͍هड़Λબ୒͍ͯͩ͘͠͞ɻ 1. DrupalίΞͰ͸AnnotatedClassDiscoveryͱݺ͹ΕΔݕग़ํ๏ͷΈ ఏڙ͞Ε͍ͯΔɻ 2. ϓϥάΠϯϚωʔδϟͰɺϓϥάΠϯλΠϓ͕࢖༻͢Δݕग़ํ๏Λ ఆٛ͢Δɻ 3. ಠࣗʹݕग़ํ๏Λఆٛ͢Δ͜ͱ͕Ͱ͖Δɻ

  34. ຊ೔ͷΞδΣϯμ͸ऴྃ

  35. ΞδΣϯμ 1. ϓϥάΠϯͱ͸ 2. ϓϥάΠϯλΠϓΛར༻ͯ͠ϓϥάΠϯΛ࡞੒͢Δ 3. Drupal͕ϓϥάΠϯΛݕग़͢Δํ๏ 4. طଘͷϓϥάΠϯλΠϓΛݟ͚ͭΔ 5.

    ಠࣗͷϓϥάΠϯλΠϓΛ࡞੒͢Δ 6. ࿅श໰୊
  36. None