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. – DrupalͷϒϩοΫ͸ɾɾɾ
 – ϒϩοΫϨΠΞ΢τͷը໘Ͱ೚ҙͷϦʔδϣ ϯʹ഑ஔՄೳ – ϖʔδ୯Ґɺϩʔϧ୯ҐͷӾཡ੍ޚ͕Մೳ – ຋༁͕Մೳ
 –

    ͱ͍͏ɺڞ௨ͷΠϯλʔϑΣʔεΛ͍࣋ͬͯΔ – ϓϥάΠϯγεςϜʹΑͬͯɺ֤ϒϩοΫʹڞ௨ͷ ΠϯλʔϑΣʔεΛ࣋ͨͤͭͭɺܾΊΒΕͨൣғ ͰΧελϚΠζͰ͖Δ ϓϥάΠϯͷྫɿϒϩοΫ
  2. ϓϥάΠϯλΠϓ ϓϥάΠϯ Πϯελϯε Block
 (ίΞͷBlock
 αϒεεςϜͰ ఆٛ) Search form
 (ίΞͷSearchϞδϡʔϧͰఆٛ)

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

    t (ίΞͷImageϞδϡʔϧͰఆٛ) Scale Crop Resize ViewsStyl e (ίΞͷViewsϞδϡʔϧͰఆٛ) Unformatted list HTML list
  4. ϒϩοΫϓϥάΠϯλΠϓͷϓϥάΠϯΛ࡞੒͢Δྫ <?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() ϝιουͰ ϨϯμϦϯά഑ྻ Λฦ٫͢Δ
  5. ԋश ϒϩοΫϓϥάΠϯλΠϓͷϓϥάΠϯΛ࡞੒ͯ͠ΈΑ͏ʂ
 1. ϞδϡʔϧͷσΟϨΫτϦ௚Լʹ
 src/Plugin/Block/MaruchaBlock.php ϑΝΠϧΛ࡞੒͢Δ 2. BlockBaseΛ֦ு͢Δ 3. ΞϊςʔγϣϯͰϒϩοΫͷϝλσʔλΛఆٛ͢Δ

    4. ඞཁͳϝιουΛ࣮૷͢Δ 5. Drupal؅ཧը໘͔Β ϒϩοΫϨΠΞ΢τ ʹҠಈͯ͠ɺ
 ೚ҙͷϦʔδϣϯʹ࡞੒ͨ͠ϒϩοΫϓϥάΠϯΛ഑ஔ͢Δ ίʔυ͸ͪ͜Βˠ https://github.com/hmaruyama/my-drupal9-module/compare/vol7
  6. ΧελϜϞδϡʔϧͰಠࣗͷϓϥάΠϯλΠϓΛ ࡞੒͢Δํ๏ AnnotatedClassDiscoveryͷݕग़ํ๏Λ༻͍ͨྫΛ঺հ͠·͢ɻ 1. ϓϥάΠϯϚωʔδϟΫϥεΛ༻ҙ͠·͢ɻ a. Ϋϥε಺ͰϓϥάΠϯͷݕग़ํ๏΋ఆ͍͖ٛͯ͠·͢ɻ b. ࡞੒ͨ͠ΫϥεΛ*.services.ymlʹొ࿥͠·͢ɻ 2.

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

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

    ϓϥάΠϯͷߏ੒ࡁΈΠϯελϯεΛ࡞੒͢Δ $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']);