Hey! Long time no see! Heehee. I am just dropping a note here about the basic steps of how to install XDebug for a specific setup. Here is my configuration: host: OS: Windows 7 64 bits IP: 192.168.0.3 IDE: Netbeans 8.2 guest: OS: Debian Jessie 64 bits in VirtualBox IP: 192.168.0.4 (bridged network in VirtualBox […]
Tag Archives: php
[Drupal] Create an event subscriber
Instructions I will be creating a dedicated module using Drupal Console for this event subscriber. But feel free to use your own if you already have one! (don’t forget to backup) Access the drupal root folder in command line. Generate a new module:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
dakwamine@debian-drupal:/var/www/html/drupal$ drupal generate:module Enter the new module name: > Test Module Enter the module machine name [test_module]: > Enter the module Path [/modules/custom]: > Enter module description [My Awesome Module]: > Enter package name [Custom]: > Enter Drupal Core version [8.x]: > Do you want to generate a .module file (yes/no) [yes]: > no Define module as feature (yes/no) [no]: > Do you want to add a composer.json file to your module (yes/no) [yes]: > no Would you like to add module dependencies (yes/no) [no]: > Do you confirm generation? (yes/no) [yes]: > Generated or updated files Site path: /var/www/html/drupal 1 - modules/custom/test_module/test_module.info.yml |
Generate the event subscriber. We will subscribe to the kernel.request event: […]
[Drupal] Service dependency injection in a service type class
The code
1 2 3 4 5 6 7 8 9 10 11 |
services: my_module.event_subscriber: # Your service class class: Drupal\my_module\Entity\EventSubscriber\MyModuleSubscriber # Add your services to load here in the array arguments: ['@current_user'] tags: # For this example, I am making an event subscriber class - { name: event_subscriber } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?php namespace Drupal\my_module\Entity\EventSubscriber; use Drupal\Core\Session\AccountProxy; use Symfony\Component\EventDispatcher\Event; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\KernelEvents; class MyModuleSubscriber implements EventSubscriberInterface { /** * Current user service. * @var AccountProxy */ protected $currentUser; public function __construct(AccountProxy $current_user) { // Reference the current_user service $this->currentUser = $current_user; } static function getSubscribedEvents() { // Stuff related to events subscribing in Drupal $events[KernelEvents::REQUEST][] = ['kernel_request']; return $events; } /** * @param GetResponseEvent $event */ public function kernel_request(Event $event) { // Do stuff here, using $this->currentUser } } |
Additional Information Declare the services your class will use in the .services.yml file of your module. Unlike plugin type class service dependency injection, there is no create method to define the needed services. The __construct signature needs to respect the declared arguments from the .services.yml file. Originally made and tested on […]
[Drupal] Service dependency injection in a plugin type class
The code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?php namespace Drupal\my_module\Controller; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Entity\EntityTypeManager; // Example service class to load and use use Drupal\Core\Entity\Query\QueryFactory; // Another example service class to load and use use Symfony\Component\DependencyInjection\ContainerInterface; class MyModuleController extends ControllerBase { /** * @var QueryFactory */ protected $queryFactory; /** * @var EntityTypeManager */ protected $entityTypeManager; public static function create(ContainerInterface $container) { // Get your services here. return new static( $container->get('entity.query'), $container->get('entity_type.manager') ); } public function __construct(QueryFactory $query_factory, EntityTypeManager $entity_type_manager) { // Reference your services here for future use $this->queryFactory = $query_factory; $this->entityTypeManager = $entity_type_manager; } public function content() { // Do Controller stuff... $ids = $this->queryFactory->get('node')->condition('type', 'article')->pager(15)->execute(); $entities = $this->entityTypeManager->getStorage('node')->loadMultiple($ids); // etc... } } |
Additional Information Unlike service type class service dependency injection, there may be no need to implement an interface to be able to load the needed services, such as extending from ControllerBase. So if you are extending from a class which already has the dependency injection implementation, you can just override the create […]
PHP/HTML : Lecteurs vidéo Youtube et Blip XHTML
Vous avez le code source ici : Lien vers le code Il s’agit d’un script PHP qui permet de sélectionner le bon lecteur en fonction de la plateforme. Par exemple, sur Flash, le player Blip va être un <video> tandis que sur PC et autres, il s’agira d’un <object> contenant un lecteur Flash. Le code […]
Un modèle d’organisation perso de mes scripts pour Fuu-Doh!
Bon, en fait, je ne sais pas si je suis le premier à coder ainsi. Mais comme j’aime vraiment le modèle que j’ai conçu pour organiser les scripts de Fuu-Doh! v2, je pense que je partagerai bientôt le concept aux développeurs PHP qui pourraient en être intéressés. C’est un modèle de conception qui est très […]