Drupal 8 override: services and plugins

  • Published on
    21-Mar-2017

  • View
    36

  • Download
    3

Embed Size (px)

Transcript

<ul><li><p>Drupal 8 override:services and pluginsAnton Ivanov</p></li><li><p>Services override</p></li><li><p>Easiest way #1Copy: /sites/default/default.services.ymlTo: /sites/default/services.ymlPut service definition:</p><p>services: cache_tags.invalidator.checksum: class: Drupal\my_module\Cache\MyModuleCacheTagsChecksum tags: - { name: cache_tags_invalidator }</p></li><li><p>It works becauseIn /sites/default/settings.php available:</p><p>$settings['container_yamls'][] = $app_root . '/' . $site_path . '/services.yml';</p><p>Means: /sites/default/services.yml</p></li><li><p>Easiest way #1.5$settings['container_yamls'][] = 'modules/my_module/example.services.yml';To: /sites/default/settings.phpPut service definition to:example.services.yml</p></li><li><p>Specific of usageAdvantages:Not overridable</p><p>Disadvantages:Not overridableShould update settings.php</p></li><li><p>Way #2If module name is my_moduleCreate /src/MyModuleServiceProvider.php</p><p>class MyModuleServiceProvider extends ServiceProviderBase { public function alter(ContainerBuilder $container) { $definition = $container-&gt;getDefinition('language_manager'); $definition -&gt;setClass('Drupal\my_module\MyModuleLanguageManager') -&gt;setArguments([ new Reference('language.default'), new Reference('config.factory'), new Reference('module_handler'), new Reference('language.config_factory_override'), new Reference('request_stack'), new Reference('my_module.handler'), ]); }}</p></li><li><p>Way #2 possible issueCode is correct but not worksAny ideas ?</p></li><li><p>Way #2 issue solutionCode is correct but not worksOverride by another module is lastModule weight is the clue !To hook_install() add: module_set_weight('my_module', 10);</p></li><li><p>Plugins override</p></li><li><p>How to override pluginsFind plugin manager firstIf extends DefaultPluginManagerAnd call alterInfo() in __construct()Use hook_TYPE_alter()</p><p>function my_module_alterhook_alter(&amp;$definitions) { // Do alter here.}</p></li><li><p>Useful linkshttps://www.drupal.org/docs/8/api/services-and-dependency-injection/altering-existing-services-providing-dynamic-services</p><p>https://www.previousnext.com.au/blog/overriding-services-drupal-8-advanced-cases</p><p>https://api.drupal.org/api/drupal/core!core.api.php/group/plugin_api</p><p>https://www.drupal.org/docs/8/api/plugin-api/plugin-api-overview</p></li><li><p>Thank you! Questions?Anton Ivanov</p></li></ul>

Recommended

View more >