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

Drupal8 State API と Configuration API

Drupal8 State API と Configuration API

Drupal8のVariables~State API と Configuration API ~

29c60cc9d6ccf002660af74d52800435?s=128

kazukomurata

January 26, 2017
Tweet

Transcript

  1. Drupal8ͷVariables ~State API ͱ Configuration API ~ MURATA Kazuko

  2. Variables ͱ͸ Drupalͷઃఆ͸keyͱvalueͰఆٛ͞ΕΔ Variablesʹू໿͞Ε͍ͯΔɻ

  3. Drupal8ͰVariables ͸ແ͍ɾɾɾʂ Drupal8ͰҎԼͷ2ͭʹ෼཭͠·ͨ͠ɻ ɾঢ়ଶΛอ࣋͢ΔState ɾઃఆΛอ࣋͢ΔConfiguration

  4. State API ؆қతʹอ࣋͢Δ஋͸State APIΛར༻͢Δɻ ɾCSS,JSѹॖϑΝΠϧύεʹͭ͘จࣈྻ →/sites/default/files/css/css_lqq6XNouNM84HMTnOnigWo9qb1k.css?ok2ccv ɾCron࣮ߦͷ࠷ऴ೔࣌ ɾΠϯετʔϧ೔࣌

  5. Configuration API ΞϓϦέʔγϣϯͱͯ͠ఆ͍ٛͨ͠΋ͷ͢΂ ͯ͸Configuration APIΛར༻͢Δɻ ɾ͋Γͱ͋ΒΏΔઃఆ஋ ɾCIMͰग़ྗ͞ΕΔͷͰ؀ڥݻ༗ͷid஋Λอ ଘ͢Δͱ௧͍໨͋͏ɻ

  6. ෮श : D7ͷVariables // Set. variable_set('key', value); // Get. variable_get('key',

    σϑΥϧτ஋); // Delete. variable_del(‘key');
  7. State API ίʔυྫ // Set. \Drupal::state()->set('key', value); ※key͸Ϟδϡʔϧ໊ͱkey໊শΛ ʮ.ʯͰͭͳ͛ͯࢦఆ͢Δ(ex. system.css_js_query_string)

    // Get. \Drupal::state()->get('key'); // Delete. \Drupal::state()->delete(‘key'); Service ͰDI͢Δͱ͖͸ʮStateInterfaceʯͰड͚ࢭΊΔͷ͕ਖ਼ղ(ͨͿΜ)
  8. Configuration API ίʔυྫ // Set. $config = \Drupal::service(‘config.factory') ->getEditable('system.performance'); $config->set('cache.page.enabled',

    1); // Get. $config = \Drupal::config('system.maintenance'); $message = $config->get('message'); // Delete. \Drupal::service(‘config.factory’)->getEditable('key')->delete();
  9. Configuration API ิ଍ ɾઃఆ஋ͷొ࿥ը໘͸ConfigFormBaseΛܧঝָͯ͠͠·͠ΐ ͏ɻ ྫ : core/modules/system/src/Form/SiteInformationForm.php ɾServiceར༻ͷ࣌͸ɺState APIಉ༷ɺConfigFactoryInterfaceͰ

    ड͚ࢭΊͭͭɺconstractͷதͰɺඞཁͳ෼Λget͠ͱ͘ͷ͕҆શ ͔΋ɻ public function __construct(ConfigFactoryInterface $config { $this->config = $config->get('key'); }
  10. Thank you!! ͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ɻ