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

Drupal モジュール開発入門講座 第2回 シンプルなカスタムモジュールを作ってみよう

Drupal モジュール開発入門講座 第2回 シンプルなカスタムモジュールを作ってみよう

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

Other Decks in Programming

Transcript

  1. खॱ 1. *.info.ymlͷ࡞੒ 2. ϑοΫΛ࣮૷ 3. ϧʔςΟϯάͱίϯτϩʔϥʔΛ௥Ճ 4. ϑΥʔϜΛ࡞੒ 5.

    ϒϩοΫΛ࡞੒ ΧελϜϞδϡʔϧͷ࡞੒εςοϓ Drupal͕ϞδϡʔϧΛͲͷΑ͏ʹѻ͏͔Λֶश͢ΔͨΊʹɺΧελϜϞδϡʔϧΛ࡞੒͢ΔҰͭͷྫΛ঺ հ͠·͢ɻ υΩϡϝϯτϧʔτ௚Լʹɺmodules/custom/marucha/ ͱ͍͏σΟϨΫτϦΛ࡞੒͠ɺ͜͜ͰΧελϜϞ δϡʔϧͷ࡞੒Λߦ͍ͬͯ͘͜ͱͱ͠·͢ɻ ιʔείʔυ͸ͪ͜Β→ https://github.com/hmaruyama/my-drupal9-module/compare/master...vol2
  2. marucha.info.ymlΛ࡞੒͠Α͏ name: Marucha type: module description: 'This is the first

    custom module.' core_version_requirement: ^8 || ^9 ඞਢ Ϟδϡʔϧͷϥϕϧ໊ ඞਢ ֦ுػೳͷछผΛࣔ͢ ೚ҙ Ϟδϡʔϧͷઆ໌Λࣔ͢ ඞਢ ରԠ͢ΔίΞόʔδϣϯ
  3. ΧελϜϞδϡʔϧΛ༗ޮԽ͢Δ – *.info.ymlϑΝΠϧΛద੾ʹ༻ҙ͢Δ͚ͩͰɺDrupal͸ϞδϡʔϧΛೝࣝͯ͘͠Ε·͢ɻ – Drupal؅ཧϝχϡʔ > ػೳ֦ு ͷϞδϡʔϧҰཡͷதʹʮMarucha Moduleʯ͕දࣔ͞Ε͍ͯΔ͔֬ೝ͠ ·͢ɻ

    – νΣοΫϘοΫεʹνΣοΫΛೖΕͯɺʮΠϯετʔϧʯϘλϯΛΫϦοΫ͠·͢ɻ *.info.ymlϑΝΠϧͷΈͷঢ়ଶͩ ͕ɺσΟϨΫτϦͱϑΝΠϧΛ ਖ਼͘͠഑ஔ͢Δ͜ͱͰϞδϡʔ ϧͱͯ͠ೝࣝ͞ΕΔʂ
  4. ϑοΫͱ͸ʁ – ϑοΫ(Hooks)ͱ͸ɺDrupalͷಈ࡞Λมߋ͢Δؔ਺Ͱ͢ɻ – ϑοΫΛ࢖༻͢Δ͜ͱͰɺطଘͷίʔυΛॻ͖׵͑Δ͜ͱͳ͘ɺDrupalίΞ΍ଞϞ δϡʔϧͷಈ࡞Λมߋɾ֦ுͰ͖·͢ɻ – ֤ϑοΫ͸ hook_batch_alter() ͷΑ͏ʹ

    hook_ Ͱ࢝·Δ໊લ͕͚ͭΒΕ͍ͯ·͢ɻ – ֤ϑοΫʹ͸ɺఆٛࡁΈͷҰ࿈ͷύϥϝʔλʔͱɺఆٛࡁΈͷ໭Γ஋͕༻ҙ͞Εͯ ͍·͢ɻ – جຊతʹ͸ɺΧελϜϞδϡʔϧͷ *.moduleϑΝΠϧͰϑοΫΛ࣮૷͠·͢ɻ – ΧελϜϞδϡʔϧͰΦϦδφϧͷϑοΫΛఏڙ͢Δ͜ͱ΋ՄೳͰ͢ɻ 
 https://www.drupal.org/docs/creating-custom- modules/understanding-hooks
  5. Drupalͷϖʔδίʔϧϓϩηε https://www.drupal.org/docs/drupal-apis/ routing-system/routing-system-overview ᶃ શͯͷϦΫΤετ͸ index.php(ϑϩϯτί ϯτϩʔϥʔ)Λ௨Δ ᶄ index.phpͰɺSymfonyͷ HTTPKernelΛجͱͨ͠

    DrupalKernel͕࣮ߦ͞ΕΔ ᶅ ϦΫΤετͷϧʔτΛಛ ఆ͠ɺίϯτϩʔϥʔΛ࣮ ߦɺςϯϓϨʔτͷϨϯμϦ ϯά͕ߦΘΕΔ ᶆ ίϯτϩʔϥʔͷॲཧ݁ ՌΛHTTPϨεϙϯεͱͯ͠ ฦ٫͢Δ
  6. ϧʔςΟϯάͷએݴ Ϟδϡʔϧͷ *.routing.yml ϑΝΠϧͰϧʔςΟϯάΛఆٛͰ͖·͢ɻ marucha.page: path: '/marucha' defaults: _controller: '\Drupal\marucha\Controller\MaruchaController::index'

    _title: 'Marucha First Page' requirements: _permission: 'access content' https://www.drupal.org/docs/drupal-apis/routing-system/introductory-drupal-routes- and-controllers-example#s-routing-in-routingyml-files
  7. ίϯτϩʔϥʔͷ௥Ճ *.routing.yml ϑΝΠϧͷ _controller Ωʔ ʹ͸ɺ࣍ͷ஋Λࢦఆ͍ͯ͠·ͨ͠ɻ '\Drupal\marucha\Controller\MaruchaCon troller::index' 
 


    ϧʔςΟϯάͰએݴ໊ͨ͠લͱҰக͢Δ Α͏ʹɺίϯτϩʔϥʔΫϥεΛ༻ҙ͠ ͯΞΫγϣϯϝιουΛ࣮૷͠·͢ɻ <?php namespace Drupal\marucha\Controller; use Drupal\Core\Controller\ControllerBase; /** * Provides route responses for marucha module. */ class MaruchaController extends ControllerBase { /** * Returns a simple page. * * @return array * A simple renderable array. */ public function index() { return ['#markup' => 'Hello world!']; } } src/Controller/MaruchaController.php https://www.drupal.org/docs/drupal-apis/routing-system/introductory-drupal- routes-and-controllers-example#s-building-a-page-controller
  8. DrupalͷForm API – DrupalͷForm API͸ɺHTMLϑΥʔϜΛૢ࡞͢ΔͨΊͷந৅ԽϨΠϠͰ͢ɻ – ϑΥʔϜͷఆٛɺදࣔɺݕূɺૹ৴ɺͱ͍͏ϥΠϑαΠΫϧΛ੍ޚ͠·͢ɻ – \Drupal\Core\Form\FormInterfaceΛ࣮૷ͯ͠ϑΥʔϜΫϥεΛ࡞੒͢Δ͜ͱͰɺΧελ ϜϞδϡʔϧͰϑΥʔϜΛ੍ޚͰ͖·͢ɻ

    – ϑΥʔϜ͕ཁٻ͞ΕΔͱɺϑΥʔϜ͸ϨϯμϦϯάՄೳͳ$form഑ྻͱͯ͠ఆٛ͞Ε· ͢ɻ – $form഑ྻ͸ϨϯμϦϯάͷաఔͰHTMLʹม׵͞ΕɺΤϯυϢʔβʔʹදࣔ͞Ε·͢ɻ https://www.drupal.org/docs/drupal-apis/form- api/introduction-to-form-api
  9. ϑΥʔϜΫϥεΛ࡞੒͢Δ <?php namespace Drupal\marucha\Form; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; class MaruchaForm

    extends FormBase { public function getFormId() { /** ϑΥʔϜΛࣝผ͢ΔͨΊͷIDΛฦ٫͢Δ */ } public function buildForm(array $form, FormStateInterface $form_state) { /** ϑΥʔϜΛߏ੒͢Δ֤ཁૉΛఆٛ͢Δ$form഑ྻΛฦ٫͢Δ */ } public function submitForm(array &$form, FormStateInterface $form_state) { /** ϑΥʔϜͷૹ৴΍ϑΥʔϜσʔλͷॲཧΛߦ͏ */ } } ϑΥʔϜΫϥε͸ • getFormID() • buildForm() • submitForm() ͷ3ͭͷϝιου͕ඞཁ src/Form/MaruchaForm.php ࡞੒ͨ͠ϑΥʔϜ͸ 
 • ϧʔςΟϯάʹొ࿥ͯ͠ϖʔδ ͱͯ͠දࣔͨ͠Γ • FormBuilderαʔϏεΛར༻ͯ͠ ϒϩοΫ౳Ͱදࣔͨ͠Γ 
 ͢Δ͜ͱ͕Ͱ͖Δ
  10. – Block API͸ɺ࣮ࡍʹ͸2ͭͷAPIͰߏ੒͞Ε͍ͯ·͢ɻ – Plugin APIɿϒϩοΫͷίϯςϯπΛϨϯμϦϯά – Entity APIɿϒϩοΫͷ഑ஔ৘ใ΍ϒϩοΫݻ༗ͷߏ੒Λอଘ –

    \Drupal\Core\Block\BlockPluginInterfaceΛ࣮૷͢ΔϒϩοΫΫϥεΛ࡞੒ͯ͠ɺϒϩοΫΛఆٛ Ͱ͖·͢ɻ – Ͱ͕͢ɺ௨ৗ͸BlockPluginInterfaceͰ͸ͳ͘\Drupal\Core\Block\BlockBaseΫϥεΛ֦ுͯ͠Ϋ ϥεΛ࡞੒͠·͢ɻ – ͜Ε͸ɺϒϩοΫߏ੒ΤϯςΟςΟͰߏ੒Λऔಘɾઃఆ͢ΔͨΊͷڞ௨ͷߏ੒ϑΥʔϜͱ ϢʔςΟϦςΟϝιουΛఏڙ͢ΔΫϥεͰ͢ɻ Block APIͱ͸
  11. ϒϩοΫΫϥεΛ࡞੒͢Δ <?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() ϝιουͰ ϨϯμϦϯά഑ྻ Λฦ٫͢Δ
  12. ԋश Block APIΛར༻ͯ͠ಠࣗͷϒϩοΫΛ࡞੒͠Α͏ʂ 1. ϞδϡʔϧͷσΟϨΫτϦ௚Լʹ 
 src/Plugin/Block/MaruchaBlock.php ϑΝΠϧΛ࡞੒͢Δ 2. ΞϊςʔγϣϯͰϒϩοΫΛఆٛ͢Δ

    3. ඞཁͳϝιουΛ࣮૷͢Δ 4. Drupal؅ཧը໘͔Β ϒϩοΫϨΠΞ΢τ ʹҠಈͯ͠ 
 ೚ҙͷϦʔδϣϯʹ࡞੒ͨ͠ϒϩοΫΛ഑ஔ͢Δ