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. CSSɺJSͷ௥Ճ 3. ςϯϓϨʔτϑΝΠϧͷ௥Ճ 4. Ϧʔδϣϯͷఆٛ ΧελϜςʔϚͷ࡞੒εςοϓ

    Drupal͕ςʔϚΛͲͷΑ͏ʹѻ͏͔Λֶश͢ΔͨΊʹɺΧελϜςʔϚΛ࡞੒͢ΔͨΊͷ େ·͔ͳྲྀΕΛ঺հ͠·͢ɻ υΩϡϝϯτϧʔτ௚Լʹɺthemes/custom/marucha/ ͱ͍͏σΟϨΫτϦΛ࡞੒͠ɺ͜͜ ͰςʔϚͷ࡞੒Λߦ͍ͬͯ͘͜ͱͱ͠·͢ɻ ιʔείʔυ͸ͪ͜Β→ https://github.com/hmaruyama/my-drupal9-theming/tree/vol2
  2. marucha.info.ymlΛ࡞੒͠Α͏ name: Marucha type: theme description: '·Δ஡ͷͨΊͷΧελϜςʔϚ' core_version_requirement: ^8 ||

    ^9 base theme: false ඞਢ ςʔϚͷϥϕϧ໊ ඞਢ ֦ுػೳͷछผΛࣔ͢ ςʔϚͷઆ໌Λࣔ͢ ରԠ͢ΔίΞόʔδϣϯ ඞਢ ϕʔεςʔϚͷࢦఆ
  3. ΧελϜςʔϚΛσϑΥϧτʹઃఆ – *.info.ymlϑΝΠϧΛద੾ʹ༻ҙ͢Δ͚ͩͰɺ Drupal͸ςʔϚΛೝࣝͯ͘͠Ε·͢ɻ – Drupal؅ཧϝχϡʔ > ςʔϚ > ΞϯΠϯε

    τʔϧࡁΈςʔϚ ʹɺΧελϜςʔϚ͕ද ࣔ͞Ε͍ͯΔ͔֬ೝ͠·͢ɻ – දࣔ͞Ε͍ͯͨΒɺαΠτͷσϑΥϧτςʔ Ϛʹઃఆ͓͖ͯ͠·͢ɻ
  4. ΞηοτϥΠϒϥϦͷ௥Ճํ๏ 1. ಡΈࠐΈ͍ͨCSS/JSϑΝΠϧΛ४උ͠·͢ɻ 2. *.libraries.yml ͰΞηοτϥΠϒϥϦΛఆٛ͠·͢ɻ 3. ΞηοτϥΠϒϥϦΛಡΈࠐΈ·͢ɻ a. શͯͷϖʔδͰݺͼग़͢

    - *.info.ymlʹఆٛ b. ಛఆͷTwigςϯϓϨʔτ - *.html.twigʹఆٛ c. ಛఆͷϒϩοΫɺϊʔυ - *.themeʹఆٛ https://www.drupal.org/docs/theming-drupal/adding- stylesheets-css-and-javascript-js-to-a-drupal-theme
  5. CSSɾJSϑΝΠϧͷ४උ maruch a ɹ ᵓ── cs s ɹ │ └──

    styles.cs s ɹ ᵓ── j s ɹ │ └── script.j s ɹ └── marucha.info.yml – ςʔϚσΟϨΫτϦ௚Լʹ 
 cssɺjsσΟϨΫτϦΛ࡞੒͠·͢ɻ – ֤σΟϨΫτϦʹϑΝΠϧΛ഑ஔ͠ ͓͖ͯ·͢ɻ a { color: red; } h1 { color: blue; } console.log('JS͕ಡΈࠐ·Ε·ͨ͠ɻ'); marucha/css/styles.css marucha/js/script.js
  6. ఆٛͨ͠ΞηοτϥΠϒϥϦΛςʔϚʹ௥Ճ͢Δ name: Marucha type: theme description: '·Δ஡ͷͨΊͷΧελϜςʔϚ' core_version_requirement: ^8 ||

    ^9 base theme: false libraries: - marucha/global-styling - marucha/global-scripts – ࣍ʹɺ*.libraries.ymlͰఆٛͨ͠ΞηοτϥΠϒϥϦΛͲ͜Ͱݺͼग़͔͢Λࢦఆ͠·͢ɻ – *.info.ymlͷlibrariesΩʔͰΞηοτϥΠϒϥϦΛࢦఆ͢ΔͱɺαΠτશମͰಡΈࠐ·Ε Δ͜ͱʹͳΓ·͢ɻ
  7. ࢀߟ) SMACSSͱ͸ – Scalable and Modular Architecture for CSS –

    CSSઃܭख๏ͷҰͭɻCSSΛ5ͭͷΧςΰϦʹ෼ྨͯ͠هड़͍ͯ͘͜͠ͱͰɺίʔυͷϝϯςφϯεੑΛ޲্ͤ͞·͢ɻ CSSઃܭख๏͸ଞʹ΋BEM΍OOCSS͕༗໊Ͱ͢ɻ 
 – 5ͭͷ෼ྨ – Base — HTMLཁૉͷελΠϦϯά – Layout — άϦουγεςϜΛؚΉϖʔδϨΠΞ΢τͷελΠϦϯά – Component — ࠶ར༻ՄೳͳUIཁૉͷελΠϦϯά ※ SMACSSͰ͸Moduleͱݺ͹ΕΔ – State — Ϛ΢εΦʔόʔͳͲͷΫϥΠΞϯτଆͷίϯϙʔωϯτมߋͷελΠϦϯά – Theme — ίϯϙʔωϯτͷࢹ֮తͳελΠϦϯά 
 – DrupalͰ͸SMACSSΛ༻͍ͯCSSΛ෼ྨ͠·͢ɻ – Base, Layout, Component, State, Themeͷॱ൪ͰಡΈࠐΉΑ͏ʹͳ͍ͬͯ·͢ɻ
  8. ࢀߟ) ςϯϓϨʔτΤϯδϯͱ͸ – ςϯϓϨʔτͱݺ͹ΕΔ਽ܗͱɺ͋ΔσʔλϞσϧͰදݱ͞ΕΔೖྗσʔλΛ߹੒͠ɺ੒ՌυΩϡϝϯτΛग़ྗ͢Διϑτ ΢ΣΞ - wikipedia 
 – PHPք۾ͰΑ͘ར༻͞ΕΔςϯϓϨʔτΤϯδϯ

    – Twig (Symfonyඪ४) – Blade (Laravelඪ४) – Smarty 
 – ͳͥςϯϓϨʔτΤϯδϯΛ࢖༻͢Δͷʁ – ViewͱϩδοΫͷ෼཭ – XSS߈ܸͷճආʢࣗಈΤεέʔϓॲཧʣ - શͯͷHTMLΛTwigςϯϓϨʔτͰग़ྗ͢Δ͜ͱ͕๬·͍͠Ͱ͢ɻWriting secure code for Drupal ࢀর 
 – Drupal 8Ҏલ(ver 4.7 ~ 7.x)Ͱ͸ʮPHPTemplateʯ͕ςϯϓϨʔτΤϯδϯͱͯ͠࠾༻͞Ε͍ͯ·ͨ͠ɻ
  9. $ cp web/core/modules/block/templates/block.html.twig web/themes/custom/marucha/ templates/block--marucha-page-title.html.twig ..(লུ).. <div{{ attributes }}> <div{{

    attributes.addClass('my-class') }}> # my-classΫϥεΛ௥Ճ {{ title_prefix }} {% if label %} ..(লུ).. ςϯϓϨʔτΛ্ॻ͖͠Α͏ web/themes/custom/marucha/templates/block--marucha-page-title.html.twig
  10. – ςʔϚͷ*.info.ymlͰϦʔδϣϯ͕એݴ͞Ε͍ͯͳ͍৔߹ɺDrupal͸࣍ͷϦʔδϣϯΛσ ϑΥϧτͰηοτ͠·͢ɻ – Left sidebar (sidebar_first) – Right sidebar

    (sidebar_second) – Content (content) – Header (header) – Primary menu (primary_menu) – Secondary menu (secondary_menu) – Footer (footer) – Highlighted (highlighted) – Help (help) – Breadcrumb (breadcrumb) – Page top (page_top) – Page bottom (page_bottom) σϑΥϧτϦʔδϣϯ https://www.drupal.org/docs/theming-drupal/adding- regions-to-a-theme#default_regions
  11. – ςʔϚͷ*.info.ymlͰϦʔδϣϯ͕એݴ͞Ε͍ͯͳ͍৔߹ɺDrupal͸࣍ͷϦʔδϣϯΛσ ϑΥϧτͰηοτ͠·͢ɻ – Left sidebar (sidebar_first) – Right sidebar

    (sidebar_second) – Content (content) – Header (header) – Primary menu (primary_menu) – Secondary menu (secondary_menu) – Footer (footer) – Highlighted (highlighted) – Help (help) – Breadcrumb (breadcrumb) – Page top (page_top) – Page bottom (page_bottom) σϑΥϧτϦʔδϣϯ https://www.drupal.org/docs/theming-drupal/adding- regions-to-a-theme#default_regions ςʔϚଆͰҰͭͰ΋ϦʔδϣϯΛ ఆٛͨ͠ΒɺσϑΥϧτϦʔδϣ ϯ͸ແޮʹͳΔʂ 
 ʢ˞ඇදࣔϦʔδϣϯΛআ͘ʣ
  12. ඇදࣔϦʔδϣϯ(Hidden regions)ͱ͸ – ࣍ͷೋͭͷϦʔδϣϯ͸ʮHidden regionsʯͱݺ͹ΕɺϒϩοΫϨΠΞ΢τϖʔ δͰදࣔ͞Ε·ͤΜɻ – Page top (page_top)

    – Page bottom (page_bottom) 
 – ඇදࣔϦʔδϣϯ͸ page.html.twig Ͱ͸ͳ͘ html.html.twig Ͱݺͼͩ͞Ε·͢ɻ – *.info.ymlͰఆٛ΋Ͱ͖·͕͢ɺఆٛ͠ͳͯ͘΋DrupalଆͰ༻ҙ͞Ε·͢ɻ – Ϟδϡʔϧ͕ඇදࣔϦʔδϣϯʹґଘ͍ͯ͠Δ৔߹͕͋ΔͷͰɺhtml.html.twig ΛΧελϚΠζ͢ΔࡍʹɺඇදࣔϦʔδϣϯΛ࡟আ͠ͳ͍Α͏ʹ͍ͯͩ͘͠͞ɻ
  13. ςʔϚʹϦʔδϣϯΛఆٛͯ͠ΈΑ͏ – *.info.yml ʹ regions ΩʔͰ3ͭͷϦʔ δϣϯΛఆٛ͠·͢ɻ – My Header

    (my_header) – My Content (my_content) – My Footer (my_footer) 
 – ϦʔδϣϯͷϚγϯ໊͸ӳ਺ࣈͰ͋ Δඞཁ͕͋Γ·͢ɻΞϯμʔείΞΛ ؚΊΒΕ·͢ɻ name: Marucha type: theme description: '·Δ஡ͷͨΊͷΧελϜςʔϚ' core_version_requirement: ^8 || ^9 base theme: false libraries: - marucha/global-styling - marucha/global-scripts regions: my_header: 'My Header' my_content: 'My Content' my_footer: 'My Footer'
  14. ఆٛͨ͠ϦʔδϣϯΛςϯϓϨʔτʹ௥Ճ͠Α͏ $ cp web/core/modules/system/templates/page.html.twig web/themes/custom/marucha/ templates/ <header> {{ page.my_header }}

    </header> <main> {{ page.my_content }} </main> <footer> {{ page.my_footer }} </footer> web/themes/custom/marucha/templates/page.html.twig
  15. ΧελϜςʔϚͰDrupal͕༻ҙ͢ΔσϑΥϧτϦʔδϣϯ͕࢖༻͞ΕΔͷ͸ͲΜͳ৔໘Ͱ͔͢ɻ 1. *.info.ymlʹϦʔδϣϯ͕ఆٛ͞Ε͍ͯͳ͍৔߹ɻ 2. *.info.ymlͷbase themeΩʔͷ஋͕falseͷ৔߹ɻ 3. *.info.ymlͷbase themeΩʔʹ೚ҙͷϕʔεςʔϚ͕ࢦఆ͞Ε͍ͯΔ৔߹ɻ 4.

    *.info.ymlͷregionsΩʔʹtrueΛࢦఆͨ͠৔߹ɻ 5. ৗʹσϑΥϧτϦʔδϣϯ͕࢖༻͞ΕΔɻ 6. Drupal؅ཧը໘ͷςʔϚݻ༗ͷઃఆϖʔδͷઃఆ߲໨ͰɺʮσϑΥϧτϦʔδϣϯΛ࢖༻ ͢ΔʯʹνΣοΫ͕ೖ͍ͬͯΔ৔߹ɻ ࿅श໰୊4
  16. ΧελϜςʔϚͰDrupal͕༻ҙ͢ΔσϑΥϧτϦʔδϣϯ͕࢖༻͞ΕΔͷ͸ͲΜͳ৔໘Ͱ͔͢ɻ 1. *.info.ymlʹϦʔδϣϯ͕ఆٛ͞Ε͍ͯͳ͍৔߹ɻ 2. *.info.ymlͷbase themeΩʔͷ஋͕falseͷ৔߹ɻ 3. *.info.ymlͷbase themeΩʔʹ೚ҙͷϕʔεςʔϚ͕ࢦఆ͞Ε͍ͯΔ৔߹ɻ 4.

    *.info.ymlͷregionsΩʔʹtrueΛࢦఆͨ͠৔߹ɻ 5. ৗʹσϑΥϧτϦʔδϣϯ͕࢖༻͞ΕΔɻ 6. Drupal؅ཧը໘ͷςʔϚݻ༗ͷઃఆϖʔδͷઃఆ߲໨ͰɺʮσϑΥϧτϦʔδϣϯΛ࢖༻ ͢ΔʯʹνΣοΫ͕ೖ͍ͬͯΔ৔߹ɻ ࿅श໰୊4 1