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

Drupal モジュール開発入門講座 第9回 構成

Drupal モジュール開発入門講座 第9回 構成

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

Other Decks in Programming

Transcript

  1. Drupalͷσʔλͷछྨ DrupalαΠτͰѻΘΕΔσʔλ͸ओʹ4छྨʹ෼ྨ – ContentɿαΠτ๚໰ऀʹදࣔ͢Δ͜ͱΛ໨తͱͨ͠ςΩ ετɺը૾ͳͲͷ৘ใɻ – ConfigurationɿαΠτͷಈ࡞ɺදࣔํ๏ͳͲΛఆٛ͢Δ ৘ใɻ௨ৗ͸খ͞ͳςΩετͰදݱ͞ΕΔ܏޲ɻ – Stateɿcronδϣϒ͕࠷ޙʹ࣮ߦ͞Εͨ࣌ࠁͳͲɺαΠτ

    ͷݱࡏͷঢ়ଶʹؔ͢ΔҰ࣌తͳੑ࣭ͷ৘ใɻ – SessionɿϩάΠϯ͍ͯ͠Δ͔Ͳ͏͔΍CookieͳͲɺݸʑ ͷαΠτ๚໰ऀͷαΠτͱͷ΍ΓऔΓʹؔ͢Δ৘ใɻ https://api.drupal.org/api/drupal/core%21core.api.php/group/info_types/
  2. Drupalͷσʔλͷछྨ DrupalαΠτͰѻΘΕΔσʔλ͸ओʹ4छྨʹ෼ྨ – ContentɿαΠτ๚໰ऀʹදࣔ͢Δ͜ͱΛ໨తͱͨ͠ςΩ ετɺը૾ͳͲͷ৘ใɻ – ConfigurationɿαΠτͷಈ࡞ɺදࣔํ๏ͳͲΛఆٛ͢Δ ৘ใɻ௨ৗ͸খ͞ͳςΩετͰදݱ͞ΕΔ܏޲ɻ – Stateɿcronδϣϒ͕࠷ޙʹ࣮ߦ͞Εͨ࣌ࠁͳͲɺαΠτ

    ͷݱࡏͷঢ়ଶʹؔ͢ΔҰ࣌తͳੑ࣭ͷ৘ใɻ – SessionɿϩάΠϯ͍ͯ͠Δ͔Ͳ͏͔΍CookieͳͲɺݸʑ ͷαΠτ๚໰ऀͷαΠτͱͷ΍ΓऔΓʹؔ͢Δ৘ใɻ https://api.drupal.org/api/drupal/core%21core.api.php/group/info_types/
  3. ߏ੒ͱ͸ – ߏ੒ͱ͸ɺαΠτͷಈ࡞ɺදࣔํ ๏ͳͲΛఆٛ͢Δ৘ใΛද·͢ɻ – DrupalͰ͸ߏ੒ΛҰ؏ͨ͠ํ๏Ͱ ѻ͏͜ͱ͕Ͱ͖·͢ɻ – ίΞͷConfiguration ManagerϞ

    δϡʔϧ΍DrushίϚϯυΛ࢖༻ ͯ͠ߏ੒ΛΠϯϙʔτɾΤΫε ϙʔτͰ͖·͢ɻ – ͜ΕʹΑΓ؀ڥؒͰߏ੒ΛಉظͰ ͖·͢ɻ _core: default_config_hash: VDJxTZtQR21qB4l langcode: en uuid: 5a62ea13-3cb0-4db7-9 name: 'λϐΦΧ٤஡ళ ·Δ஡' mail: [email protected] slogan: '' page: 403: '' 404: '' front: /node admin_compact_mode: false weight_select_max: 100 default_langcode: en system.site.yml
  4. ߏ੒؅ཧΛ࢖͍࢝ΊΔखॱ 1. ߏ੒ϑΝΠϧΛ഑ஔ͢ΔಉظσΟϨΫτϦͷ࡞੒ – ϓϩδΣΫτϧʔτ௚Լʹ config/default σΟϨΫτϦΛ࡞੒͠· ͢ɻʢσΟϨΫτϦ໊͸ͳΜͰ΋OKʣ
 2. ಉظσΟϨΫτϦΛDrupalଆʹೝࣝͤ͞ΔઃఆΛ௥Ճ

    – settings.phpʹ 
 $settings['config_sync_directory'] = '../config/default'; 
 ͷߦΛ௥Ճ͠·͢ɻ
 3. ؔ࿈Ϟδϡʔϧͷ༗ޮԽ – ؅ཧը໘͔Βߏ੒ΛΠϯϙʔτɾΤΫεϙʔτ͍ͨ͠৔߹͸ ʮConfiguration ManagerʯϞδϡʔϧΛ༗ޮԽ͠·͢ɻ·ͨ͸ DrushίϚϯυ͔Β΋ૢ࡞Ͱ͖·͢ɻ 
 ಉظσΟϨΫτϦ͸Ͳ͜ʹ഑ஔ͢Δ΂͖ʁ • ΢Σϒ͔ΒΞΫηεͰ͖ͳ͍Α͏ʹ
 υΩϡϝϯτϧʔτͷ֎ʹ഑ஔ͢Δ͜ ͱ͕ଟ͍ • υΩϡϝϯτϧʔτҎԼʹ഑ஔ͢Δ৔ ߹͸σΟϨΫτϦ໊ʹϋογϡ஋ͳͲ Λར༻ͯ͠ਪଌ͞Εͳ͍Α͏ʹ͢Δɺ ΋͘͠͸ΞΫηεͰ͖ͳ͍Α͏ʹ apacheͳͲͰઃఆ͢Δ

  5. ߏ੒؅ཧͷओͳૢ࡞ – DrupalαΠτͷߏ੒ΛYAMLϑΝΠϧͱͯ͠ΤΫεϙʔτ͢Δ
 $ drush config:export
 – YAMLϑΝΠϧ͔Βߏ੒ΛDrupalαΠτʹΠϯϙʔτ͢Δ
 $ drush

    config:import
 – YAMLϑΝΠϧͱσʔλϕʔεͷɺߏ੒ઃఆͷࠩ෼֬ೝ
 $ drush config:status
 – ߏ੒ઃఆΛίϚϯυϥΠϯ͔Β௚઀ઃఆ͢Δ
 $ drush config:set system.site name 'λϐΦΧ٤஡ళ ·Δ஡' ؅ཧը໘΋͘͠͸DrushίϚϯυͰૢ࡞͕ߦ͑·͢ɻԼه͸DrushίϚϯυͰͷྫͰ͢ɻ YAML ϑΝΠϧ ίʔυϕʔε σʔλ ϕʔε Πϯϙʔτ ΤΫεϙʔτ DrupalαΠτ
  6. ؆қߏ੒ͱߏ੒ΤϯςΟςΟͷݟ෼͚ํ system.site: type: config_object label: 'Site information' mapping: uuid: type:

    uuid label: 'Site UUID' ...লུ... system.menu.*: type: config_entity label: 'Menu' mapping: id: type: string label: 'ID' ...লུ... – ֤ϞδϡʔϧͰఆٛ͞Ε͍ͯΔεΩʔϚϑΝ Πϧ(*.schema.yml)ͷத਎Λ֬ೝͯ͠ɺτο ϓΩʔͷ௚Լͷtypeͷ஋Λ֬ೝ͠·͢ɻ – typeͷ஋͕
 config_objectͷ৔߹͸؆қߏ੒ɺ config_entityͷ৔߹͸ߏ੒ΤϯςΟςΟ 
 ͱ൑அͰ͖·͢ɻ system.site͸؆ қߏ੒Ͱ͋Δ system.menu.*͸ ߏ੒ΤϯςΟ ςΟͰ͋Δ system.schema.yml
  7. – ߏ੒৘ใͷϑΝΠϧܗࣜ͸YAMLͰ͢ɻ – 1ͭͷYAMLϑΝΠϧ = 1ͭͷߏ੒ΦϒδΣΫτ
 ͱͳΓ·͢ɻ – ߏ੒ϑΝΠϧ໊͸΄ͱΜͲ͕ 


    ϞδϡʔϧͷϚγϯ໊.೚ҙͷจࣈྻ.yml 
 ͱͳ͍ͬͯ·͢ɻʢྫɿsystem.site.ymlʣ – ϑΝΠϧ໊ͷ ϞδϡʔϧͷϚγϯ໊.೚ҙͷจࣈྻ ෦෼͕ߏ੒Φ ϒδΣΫτͷ໊લʹͳΓ·͢ɻ ߏ੒ϑΝΠϧͱߏ੒ΦϒδΣΫτ SystemϞδϡʔϧͷ ߏ੒ϑΝΠϧʢҰ෦ʣ ϑΝΠϧͷ1 ͭ1͕ͭߏ੒ ΦϒδΣΫτ ͱͯ͠ѻΘΕ Δɻ
  8. ߏ੒εΩʔϚ system.site: type: config_object label: 'Site information' mapping: uuid: type:

    uuid label: 'Site UUID' constraints: Uuid: [] NotNull: [] name: type: label ...লུ... system.maintenance: type: config_object label: 'Maintenance mode' mapping: ...লུ... system.cron: type: config_object ...লུ... system.date: type: config_object label: 'Date settings' mapping: first_day: ...লུ... – Ϟδϡʔϧ͸ಠࣗͷߏ੒σʔλΛ༻ҙ͢Δ ͜ͱ͕Ͱ͖ɺσʔλܕΛఆٛ͢ΔͨΊͷ εΩʔϚϑΝΠϧΛ༻ҙ͢Δ͜ͱ͕Ͱ͖· ͢ɻ – ϞδϡʔϧσΟϨΫτϦͷ config/schema/ *.schema.yml ͱ͍͏ϑΝΠϧΛ༻ҙ͠ ͯɺYAMLܗࣜͰϧʔϧΛهड़͠·͢ɻ – 1ͭͷϞδϡʔϧʹෳ਺ͷߏ੒ϑΝΠϧ͕ ༻ҙ͞Ε͍ͯΔ৔߹Ͱ΋ɺ1ͭͷεΩʔϚ ϑΝΠϧʹશͯهड़͠·͢ɻ system.schema.yml SystemϞδϡʔϧ ͸ෳ਺ͷߏ੒ϑΝ ΠϧΛ͕࣋ͭɺε ΩʔϚ৘ใ͸શͯ system.schema.y mlʹهड़͞ΕΔɻ
  9. ϞδϡʔϧΠϯετʔϧ࣌ʹॳظ஋Ληοτ͢Δ – Ϟδϡʔϧઃఆʹ͸ɺॳظ஋Λઃఆ͢ Δ͜ͱ͕ՄೳͰ͢ɻ – ϞδϡʔϧσΟϨΫτϦʹ config/ install/*.೚ҙͷจࣈྻ.yml ϑΝΠϧΛ࡞ ੒͠ɺ͜ͷதͰ௥Ճ͢Δॳظ஋ΛΩʔ

    όϦϡʔܗࣜͰηοτ͠·͢ɻ – ͜ͷॳظ஋͸ϞδϡʔϧͷΠϯετʔϧ ࣌ʹ൓ө͞Ε·͢ɻ langcode: en uuid: '' name: '' mail: '' slogan: '' page: 403: '' 404: '' front: /user/login admin_compact_mode: false weight_select_max: 100 default_langcode: en system/config/install/system.site.yml
  10. ߏ੒ͱ͸ ·ͱΊ – ߏ੒ͱ͸ɺDrupalαΠτͷઃఆ৘ใΛද͢σʔλͰ͢ɻ – YAMLϑΝΠϧͰΠϯϙʔτɾΤΫεϙʔτ͕ՄೳͰɺ؀ڥؒͰߏ੒ΛಉظͰ͖· ͢ɻ – ߏ੒ʹ͸؆қߏ੒ͱߏ੒ΤϯςΟςΟͷ2छྨ͕ଘࡏ͠·͢ɻ –

    جຊతʹߏ੒σʔλ͸σʔλϕʔεʹ֨ೲ͞Ε·͕͢ɺ։ൃऀ͸Configuration API Λհͯ͠ΞΫςΟϒͳߏ੒ʹΞΫηεͰ͖·͢ɻ – 1ͭͷߏ੒ϑΝΠϧ͕1ͭͷߏ੒ΦϒδΣΫτͱͯ͠ѻΘΕ·͢ɻ – ߏ੒ͷεΩʔϚϑΝΠϧΛ༻ҙͨ͠ΓɺϞδϡʔϧΠϯετʔϧ࣌ͷॳظ஋Ληο τͨ͠ΓͰ͖·͢ɻ
  11. ߏ੒ͷओͳૢ࡞ ʔ ಡΈࠐΈ – 1ͭͷߏ੒ΦϒδΣΫτΛಡΈࠐΉ – ΩʔΛࢦఆͯ͠஋Λऔಘ͢Δ $config = \Drupal::service('config.factory')->get('system.site')

    $config = \Drupal::config('system.site') $config = \Drupal::configFactory()->get('system.site') ͜ͷ3ͭͷߦ͸
 ಉ͜͡ͱΛߦͬͯ ͍·͢ʂ͍ͣΕ͔ ͷํ๏Ͱߦͬͯ͘ ͍ͩ͞ɻ $config->get('name'); ɹ// "λϐΦΧ٤஡ళ ·Δ஡" $config->get('page.front'); ɹ// "/node"
  12. ߏ੒ͷओͳૢ࡞ ʔ ॻ͖ࠐΈ – mutableConfigΦϒδΣΫτ͕ฦ٫͞ΕΔΑ͏ʹ
 getEditableؔ਺Λ༻͍ͯ1ͭͷߏ੒ΦϒδΣΫτΛಡΈࠐΉ
 – ஋Ληοτͯ͠อଘ $config =

    \Drupal::service('config.factory')->getEditable('system.site') $config = \Drupal::configFactory()->getEditable('system.site') ͜ͷ2ͭͷߦ͸
 ಉ͜͡ͱΛߦͬͯ ͍·͢ʂ͍ͣΕ͔ ͷํ๏Ͱߦͬͯ͘ ͍ͩ͞ɻ $config->set('name', 'όϒϧςΟʔ ·Δ஡'); $config->save();
  13. – Drushͷconfig:getίϚϯυͷ৔߹ – ্ॻ͖ͳ͠ͷ஋ΛݟΔɿ
 $ drush config:get system.sit e –

    ্ॻ͖͞Εͨ஋ΛݟΔɿ
 $ drush config:get system.site --include-overridden – PHPίʔυͷ৔߹ – ্ॻ͖ͳ͠ͷ஋ΛݟΔɿ
 
 
 – ্ॻ͖͞Εͨ஋ΛݟΔɿ ্ॻ͖͋Γɺͳ͠ͷ஋Λ֬ೝ͢Δํ๏ \Drupal::config('system.site')->get('name'); \Drupal::config('system.site')->getOriginal('name', FALSE); \Drupal::configFactory()->getEditable('system.site')->get('name'); MutableConfigΦ ϒδΣΫτ͸ৗʹ ্ॻ͖ͳ͠ͷ஋͕ ฦ٫͞ΕΔɻ
  14. ࿅श໰୊4 settings.phpͰ࣍ͷߦΛ௥Ճ͠·ͨ͠ɻ $config['system.logging']['error_level'] = 'all' ; ίʔυ͔Β্ॻ͖͞Εͯͳ͍஋ʹΞΫηε͢Δʹ͸Ͳ͏͢Ε͹ྑ͍Ͱ͔͢ʁ2ͭબ୒ͯ͠ ͍ͩ͘͞ɻ 1. \Drupal::configFactory()->get('system.logging')->get('error_level')

    ; 2. \Drupal::config('system.logging')->getOriginal('error_level', FALSE) ; 3. \Drupal::config('system.logging')->get('error_level') ; 4. \Drupal::configFactory()->getEditable('system.logging')->get('error_level');
  15. ࿅श໰୊4 settings.phpͰ࣍ͷߦΛ௥Ճ͠·ͨ͠ɻ $config['system.logging']['error_level'] = 'all' ; ίʔυ͔Β্ॻ͖͞Εͯͳ͍஋ʹΞΫηε͢Δʹ͸Ͳ͏͢Ε͹ྑ͍Ͱ͔͢ʁ2ͭબ୒͠ ͍ͯͩ͘͞ɻ 1. \Drupal::configFactory()->get('system.logging')->get('error_level')

    ; 2. \Drupal::config('system.logging')->getOriginal('error_level', FALSE); 3. \Drupal::config('system.logging')->get('error_level') ; 4. \Drupal::configFactory()->getEditable('system.logging')->get('error_level');