Keeping Kittens Safe
While Altering Drupal Core
Drupalcon Nashville
my_module.my_service:
class: Drupal\my_module\MyService
arguments: [ '@config.factory' ]
class MyService {
/**
* The Config Factory Service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
private $configFactory;
/**
* Constructs the Example service.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
*/
public function __construct(ConfigFactoryInterface $configFactory) {
$this->configFactory = $configFactory;
}
}
function my_module_example() {
$myService = \Drupal::service('my_module.my_service');
}
namespace Drupal\my_module;
class MyModuleServiceProvider extends ServiceProviderBase {
/**
* {@inheritdoc}
*/
public function alter(ContainerBuilder $container) {
$definition = $container->getDefinition('some_core_service');
$definition->setClass('Drupal\my_module\MyCoreOverrideService');
}
}
namespace Drupal\my_module;
class MyCoreOverrideService implements SomeCoreServiceInterface {
public function reimplement() {
// ...
}
public function every() {
// ...
}
public function interface() {
// ...
}
public function method() {
// ...
}
}
namespace Drupal\my_module;
class MyCoreOverrideService extends SomeCoreService {
public function onlyTheOverriddenMethod() {
// ...
$someDataFromTheOriginalService =
parent::methodFromTheOrginalService();
}
}
CoreService
ContribModuleService extends CoreService
CustomModuleService extends CoreService
services:
my_module.core_service_decorator:
class: Drupal\my_module\CoreServiceDecorator
arguments: [ '@my_module.core_service_decorator.inner', '@config.factory']
decorates: core_service
namespace Drupal\my_module;
class CoreServiceDecorator implements SomeCoreServiceInterface {
public function __construct(
CoreServiceInterface $coreService,
ConfigFactoryInterface $configFactory
) {
$this->coreService = $coreService;
$this->configFactory = $configFactory;
}
public function method() {
$coreServiceData = $this->coreService->method();
return $data;
}
}
namespace Drupal\my_module;
class CoreServiceDecorator implements SomeCoreServiceInterface {
public function proxy() {
return $this->coreService->proxy();
}
public function every() {
return $this->coreService->every();
}
public function interface() {
return $this->coreService->interface();
}
public function method() {
return $this->coreService->method();
}
}
CoreService
ContribModuleServiceDecorator
CustomModuleServiceDecorator
services:
my_module.core_service_decorator:
class: Drupal\my_module\CoreServiceDecorator
arguments: [ '@my_module.core_service_decorator.inner', '@config.factory']
decorates: core_service
decoration_priority: 5
CoreService
CustomModuleServiceDecorator
(priority 5)ContribModuleServiceDecorator
(priority 0)
services:
ie9.css.collection_renderer:
class: Drupal\ie9\Asset\CssCollectionRenderer
arguments: [ '@state' , '@ie9.css.collection_renderer.inner' ]
decorates: asset.css.collection_renderer
namespace Drupal\ie9\Asset;
class CssCollectionRenderer implements AssetCollectionRendererInterface {
public function __construct(StateInterface $state, AssetCollectionRendererInterface $cssCollectionRenderer) {
$this->state = $state;
$this->originalCssCollectionRenderer = $cssCollectionRenderer;
}
public function render(array $css_assets) {
if (count($css_assets) <= 31) {
return $this->originalCssCollectionRenderer->render($css_assets);
}
// Render for IE9...
return $elements;
}
}