Objectives We want to add on a node page layout a block which uses a field from the currently rendered content (node). We want this block to be translated the same as the currently rendered entity. As a anonymous user, we want it to respect the published settings of the currently rendered entity. As an […]
Tag Archives: drupal
[Drupal] Base hook schema example to put in .install
Introduction Delighted hook_schema is one of those methods where you are happy to have a usable documentation on the api website. It is pretty much self-explanatory and you don’t have to look for hours on third parties websites to get some usable information. 🙂 hook_schema on api.drupal.org and don’t forget to select your Drupal version! […]
[Drupal] Table render array example structure
Result 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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
public function controllerCallbackExample() { return [ '#type' => 'table', '#caption' => 'This is a caption. Use a translated string here.', '#header' => [ 'Header 1', 'Header 2', 'Header 3', 'Header 4' ], '#rows' => [ [ 'Row 1 Cell 1', 'Row 1 Cell 2', 'Row 1 Cell 3', ['data' => 'Row 1 Cell 4'] ], [ 'class' => ['second-row-class', 'may contain spaces but dont use them'], 'data' => [ 'Row 2 Cell 1', [ 'data' => 'Row 2 Cell 2' ], [ 'colspan' => 2, 'data' => 'Row 3 Cell 3 & 4', 'style' => 'background-color: pink; text-align: center' ] ], 'style' => 'font-weight: bold' ], [ 'data' => [ 'Row 3 Cell 1', 'Row 3 Cell 2', 'Row 3 Cell 3', 'Row 3 Cell 4' ], 'no_striping' => FALSE // Not working in 8.2.x, may be fixed later ] ], '#footer' => [ [ 'data' => [ 'Footer 1', [ 'colspan' => 2, 'data' => 'Footer 2 with colspan = 2', 'style' => 'text-align: inherit' ], [ 'data' => 'Footer 4', 'style' => 'text-align: inherit' ] ], 'style' => 'text-align: center' ], [ [ 'colspan' => 4, 'class' => 'second-footer-row', 'data' => 'Another Footer Line', 'style' => ['background-color: purple;', 'text-align: center;', 'color: white;'] ] ] ] ]; } |
Additional Information Tested to work on Drupal 8.2.x. For some reason, no_striping doesn’t work as expected in 8.2.x (it is ignored). The script which handles the render array conversion to twig ready variables is located here: core\includes\theme.inc. The twig template which renders the table on the default theme is here: core\themes\classy\templates\dataset\table.html.twig.
[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 […]
Mini point sur ma carrière
J’ai quittĂ© volontiers l’industrie du jeu vidĂ©o, ayant perdu toute motivation d’y travailler. L’entreprise dans laquelle j’avais passĂ© plus de quatre ans Ă essayer de produire des jeux et des applications est sur le point de fermer dĂ©finitivement. Un chapitre de ma vie est en train de se clore, j’imagine. Avais-je dit que j’Ă©tais rentrĂ© […]