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 […]
Category Archives: Code
[Drupal] Custom scrollbars with Overlay Scrollbars library
The road to custom scrollbars Reminiscing. I am currently working on a website for a video game reviews project I have had with an old friend of mine (FuuDoh). Obviously, I have switched to Drupal to get a result quicker than what I would get with the PHP only solution I have been working on […]
[Drupal] Change CKEditor behaviour for line breaks (br or p)
Here is a little code snippet to be added in a .module file.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/** * Implements hook_editor_js_settings_alter(). * * {@inheritdoc} */ function MODULE_editor_js_settings_alter(array &$settings) { foreach ($settings['editor']['formats'] as $name => $value) { // Use <br> instead of <p> on enter. // See: https://docs.ckeditor.com/ckeditor4/latest/api/CKEDITOR.html#property-ENTER_BR. // CKEditor natively converts double <br> into <p> on rendering. $settings['editor']['formats'][$name]['editorSettings']['enterMode'] = '2'; } } |
Additional Information Instructions made on Drupal 8.5.x. Will break text align buttons in the CKEditor toolbar: Issue on Drupal.org The default behaviour of CKEditor is the following: Enter: inserts a paragraph directly in the WYSIWYG, Shift + Enter: inserts a line […]
[Drupal] Make the Search API Page search block appear in Bootstrap markup
When using Search API Page, I needed to theme the search form in the bootstrap structure. The cleanest answer I have found is to add some variables to trigger the bootstrap overrides on the search form.
1 2 3 4 5 6 7 8 9 |
/** * Implements hook_form_FORM_ID_alter(). */ function MYMODULE_form_search_api_page_block_form_alter(&$form, FormStateInterface $form_state, $form_id) { // We need this to make our search_api_page_block_form rendered in the bootstrap fashion // This code was inspired by \Drupal\bootstrap\Plugin\Form\SearchBlockForm $form['actions']['submit']['#icon_only'] = true; $form['keys']['#input_group_button'] = true; } |
Additional Information Instructions made on Drupal 8.3.x (probably, haha).
[Drupal] VM + XDebug + Netbeans
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 […]
[Drupal] Use admin_block and admin_block_content templates
This article is about the default admin_page, admin_block and admin_block_content templates which serves as the base of the content display on some system configuration pages such as the one on this image: 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
<?php namespace Drupal\your_module\Controller; use Drupal; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Url; class YourModuleController extends ControllerBase { protected $database; function content() { // Render array to return $mainRenderArray = []; // admin_page style (half width) $mainRenderArray[] = [ '#theme' => 'admin_page', '#blocks' => [ // Array of admin_block [ 'title' => $this->t('A block, on the left'), 'position' => 'left', 'content' => [ '#theme' => 'admin_block_content', '#content' => [ [ 'title' => 'Do something', 'url' => Url::fromRoute('your_module.some.example.route'), 'description' => $this->t('This will link to a route.') ], [ 'title' => 'Navigate to a nice blog', 'url' => Url::fromUri('https://blog.dakwamine.fr'), 'description' => $this->t('This will link to a URI, external in this case.') ], [ 'title' => 'Go to contents page', 'url' => Url::fromUri('internal:/admin/content'), 'description' => $this->t('Also works for internal URI.') ] ] ] ], [ 'title' => $this->t('Another block, on the right'), 'position' => 'right', 'content' => [ '#theme' => 'admin_block_content', '#content' => [ [ 'title' => 'Another link to a route', 'url' => Url::fromRoute('your_module.another.route'), 'description' => $this->t('Yup.') ], [ 'title' => 'Website example link', 'url' => Url::fromUri('https://website.example'), 'description' => $this->t('Goes on website.example.') ], ] ] ] ], ]; // admin_block style (full width) $mainRenderArray[] = [ '#theme' => 'admin_block', '#block' => [ 'title' => $this->t('Full width block'), 'content' => [ '#theme' => 'admin_block_content', '#content' => [ [ 'title' => 'Another link', 'url' => Url::fromUri('internal:/admin/people'), 'description' => $this->t('A link to the people page.') ], [ 'title' => 'Another link again', 'url' => Url::fromUri('https://website.example'), 'description' => $this->t('Goes on website.example.') ], ] ], ], ]; return $mainRenderArray; } } |
Explanation If you use admin_page, you will be able to define the column in which the blocks dwell: left or […]
Replace image URLs in WordPress posts from http to https
I just switched my website to https for testing purpose. But changing the website’s URL to https:// is not sufficient to enable a fully secure browsing experience, as images in posts are still loaded from http://. Or more precisely, most modern browsers (FF or Opera for instance) are now clever enough to guess that the […]
[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: […]