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

Druapl テーマ開発入門講座 第6回 アセットライブラリ

Druapl テーマ開発入門講座 第6回 アセットライブラリ

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

Other Decks in Programming

Transcript

  1. ΞηοτϥΠϒϥϦΛಡΈࠐΉͨΊͷखॱ 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
  2. γϯϓϧͳྫ global-styling: version: 1.x css: theme: css/styles.css: {} global-scripts: version:

    1.x js: js/script.js: {} ςʔϚσΟϨΫτϦ௚Լʹ*.libraries.yml ϑΝΠϧΛ࡞੒͠ɺΞηοτϥΠϒϥϦΛఆٛ͠·͢ɻ͜ ͷྫͰ͸زͭͷϥΠϒϥϦ͕ఆٛ͞Ε͍ͯΔͰ͠ΐ͏ʁ
  3. γϯϓϧͳྫ global-styling: version: 1.x css: theme: css/styles.css: {} global-scripts: version:

    1.x js: js/script.js: {} Aɿglobal-styling ͱ global-scripts ͷ2ͭͷϥΠϒϥϦ͕ఆٛ͞Ε͍ͯΔ͜ͱ͕Θ͔Γ·͢ɻ͜ͷఆٛ͞Εͨϥ ΠϒϥϦΛ࢖༻ͯ͠ɺαΠτͷ͞·͟·ͳ৔ॴͰಡΈࠐΉ͜ͱ͕Ͱ͖·͢ɻ
  4. ࢀߟ) 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ͷॱ൪ͰಡΈࠐΉΑ͏ʹͳ͍ͬͯ·͢ɻ
  5. CSSͱJSϑΝΠϧͷू໿ [؀ڥઃఆ]ʼ[ύϑΥʔϚϯε]ʼ[ଳҬ෯࠷దԽ] ͷνΣοΫϘοΫεʹνΣο ΫΛೖΕɺߏ੒Λอଘ 
 ஫ҙ CSSͱJSϑΝΠϧΛू໿ͷର৅ͱ͢ΔͨΊʹ͸ɺϑΝΠϧΛςʔϚ·ͨ͸Ϟ δϡʔϧͷ *.libraries.yml Ͱఆٛ͠ɺಡΈࠐ·ͤΔඞཁ͕͋Γ·͢ɻςϯϓ

    Ϩʔτʹ௚઀ϑΝΠϧ΁ͷϦϯΫΛهड़͢Δͷ͸όουϓϥΫςΟεͰ͢ɻ ࢀߟ) CSSͱJSͷू໿ʹ͍ͭͯ DrupalͰ͸ଟ਺ͷCSSϑΝΠϧͱJSϑΝΠϧΛऔΓѻ͍·͢ɻ 
 ͜ΕΒ͸ू໿͢Δ͜ͱͰ1ͭͷʢ·ͨ͸গ਺ͷʣϑΝΠϧͱͳΓɺϦΫΤετճ਺Λ࡟ݮͰ͖·͢ɻ ू໿લ 
 ໿100ϑΝΠϧ ू໿ޙ 
 3ϑΝΠϧ
  6. JavaScript͕ಡΈࠐ·ΕΔ৔ॴ σϑΥϧτͰ͸ɺ͢΂ͯͷJSΞηοτ͸ϑολͰಡΈࠐ·Ε·͢ɻheaderϓϩύςΟΛ༻͍ͯϔομͰಡΈࠐ ·ͤΔ͜ͱ͕Ͱ͖·͢ɻʢ͜ͷํ๏Ͱએݴ͞ΕͨϥΠϒϥϦͷ௚઀·ͨ͸ؒ઀తͳґଘؔ܎΋ɺࣗಈతʹϔο μ͔Βϩʔυ͞Ε·͢ɻʣ js-header: header: true js: header.js: {}

    js-footer: js: footer.js: {} <html> <head> <js-placeholder token="{{ placeholder_token }}"> </head> <body> {{ page_top }} {{ page }} {{ page_bottom }} <js-bottom-placeholder token="{{ placeholder_token }}"> </body> </html> html.html.twig marucha.libraries.yml
  7. ֎෦ΞηοτͷಡΈࠐΈ type: external Ͱ֎෦ϥΠϒϥϦͰ͋Δ͜ͱΛએݴͰ͖·͢ɻఆٛʹ֎෦ϥΠ ϒϥϦʹؔ͢Δ৘ใΛؚΊΔ͜ͱ΋͓קΊ͠·͢ɻ angular.angularjs: remote: https://github.com/angular version: 1.4.4

    license: name: MIT url: https://github.com/angular/angular.js/blob/master/LICENSE gpl-compatible: true js: https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js: { type: external, minified: true }
  8. શͯͷϖʔδͰಡΈࠐΉ - *.info.ymlʹهड़ name: Marucha type: theme description: '·Δ஡ͷͨΊͷΧελϜςʔϚ' core_version_requirement:

    ^8 || ^9 base theme: false libraries: - marucha/global-styling - marucha/global-scripts *.info.ymlͷlibrariesΩʔͰΞηοτϥΠϒϥϦΛࢦఆ͢ΔͱɺαΠτશମͰಡΈ ࠐ·Ε·͢ɻ
  9. ϥΠϒϥϦΛΦʔόʔϥΠυ͢Δ *.info.ymlͰ libraries-override ΩʔΛ ࢖༻ͯ͠ɺίΞ΍ϞδϡʔϧͰఏڙ ͞ΕΔϥΠϒϥϦΛΦʔόʔϥΠυ Ͱ͖·͢ɻ libraries-override: # ϥΠϒϥϦશମΛஔ͖׵͑Δ

    core/drupal.collapse: marucha/collapse # ϑΝΠϧΛผͷϑΝΠϧʹஔ͖׵͑Δ subtheme/library: css: theme: css/layout.css: css/marucha-layout.css # ϑΝΠϧΛ࡟আ͢Δ drupal/dialog: css: theme: dialog.theme.css: false # ϥΠϒϥϦશମΛ࡟আ͢Δ core/modernizr: false marucha.info.yml
  10. drupalSettingsΛ࢖༻͢ΔྲྀΕ ᶃ PHPͷ஋Λ࢖༻͍ͨ͠JavaScriptϑΝΠϧΛΞηοτϥΠϒϥϦʹఆٛ ͢Δ cool-slider: version: 1.x js: js/cool-slider.js: {}

    dependencies: - core/jquery - core/drupalSettings JavaSriptϑΝΠϧͰ drupalSettingsΛར༻͢Δ ͨΊʹґଘؔ܎ͷએݴ͕ ඞཁɻ marucha.libraries.yml
  11. drupalSettingsΛ࢖༻͢ΔྲྀΕ ᶅ JavaScriptϑΝΠϧͰ஋ʹΞΫηε͢Δɻ (function ($, Drupal, drupalSettings) { 'use strict';

    Drupal.behaviors.mybehavior = { attach: function (context, settings) { console.log(settings.marucha.coolSlider.foo); } }; })(jQuery, Drupal, drupalSettings); cool-slider.js
  12. ͋ΔJavaScriptϑΝΠϧΛBodyͷ࠷Լ෦ͰಡΈࠐΈ͍ͨͰ͢ɻͲ͏͢Ε͹ྑ͍Ͱ͢ ͔ʁ 1. *.libraries.ymlͰର৅ͷJavaScriptϑΝΠϧʹ { head: false } Λࢦఆ͢Δɻ 2.

    *.libraries.ymlͰର৅ͷΞηοτϥΠϒϥϦͷheaderϓϩύςΟʹ true Λࢦఆ ͢Δɻ 3. html.html.twigͰattach_libraryؔ਺Λ࢖༻ͯ͠ಡΈࠐΉɻ 4. Drupal͸σϑΥϧτͰBodyͷด͡λά௚ޙͰಡΈࠐΉΑ͏ʹͳ͍ͬͯΔͷ Ͱɺಛʹઃఆ͸ඞཁͳ͍ɻ ࿅श໰୊2
  13. ͋ΔJavaScriptϑΝΠϧΛBodyͷ࠷Լ෦ͰಡΈࠐΈ͍ͨͰ͢ɻͲ͏͢Ε͹ྑ͍Ͱ͢ ͔ʁ 1. *.libraries.ymlͰର৅ͷJavaScriptϑΝΠϧʹ { head: false } Λࢦఆ͢Δɻ 2.

    *.libraries.ymlͰର৅ͷΞηοτϥΠϒϥϦͷheaderϓϩύςΟʹ true Λࢦఆ ͢Δɻ 3. html.html.twigͰattach_libraryؔ਺Λ࢖༻ͯ͠ಡΈࠐΉɻ 4. Drupal͸σϑΥϧτͰBodyͷด͡λά௚ޙͰಡΈࠐΉΑ͏ʹͳ͍ͬͯΔͷ Ͱɺಛʹઃఆ͸ඞཁͳ͍ɻ ࿅श໰୊2 ਖ਼ղɿ4