Skip to content

Instantly share code, notes, and snippets.

View WengerK's full-sized avatar
🤓

Kevin Wenger WengerK

🤓
View GitHub Profile
@WengerK
WengerK / README.md
Last active March 27, 2024 10:38
Debug Drupal - KernelTest, BrowserTest & JavascriptTest

Debug Drupal - KernelTest, BrowserTest & JavascriptTest

Debugging tests can often be a pain, but using those snippets it's possible to get a lot of informations when using BrowserTestBase or JavascriptTestBase.

Get HTML of a page

Sometimes you'll want to inspect the HTML of the page or a specific element. There is no official method for this yet but the following works:

$this->assertEquals('', $this->getSession()->getPage()->getHTML());
@WengerK
WengerK / README.md
Last active November 22, 2023 19:42
Drupal 8 - How to translate Config API

Article Ressources - Drupal 8, How to translate Config API

This is the Gist repository for my article Drupal 8, How to translate Config API.

Be aware that this article has been wrote for the Blog of Antistatique — Web Agency in Lausanne, Switzerland. A place where I work as Full Stack Web Developer.

Feel free to read it the full article on Medium or check it out on Antistatique.

Content of this Gist :

  • SettingsForm.php : Basic Form Config
@WengerK
WengerK / ContactForm-basic.php
Last active June 12, 2023 19:39
Drupal 8 — Inline validation in forms
<?php
/**
* @file
* Contains \Drupal\my_contact\Form\ContactForm.
*/
namespace Drupal\my_contact\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
@WengerK
WengerK / ArticleAutoCompleteController.php
Last active May 4, 2023 19:00
Article Ressources - How to create a custom Autocomplete using the Drupal 8 Form API
<?php
namespace Drupal\my_module\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Component\Utility\Xss;
@WengerK
WengerK / AuthorDefaultWidget.php
Created April 12, 2023 15:08
Article Ressources - How to create an Entity Field Widget Autocomplete for Internal Data on Drupal
<?php
namespace Drupal\my_module\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'author_default' widget.
@WengerK
WengerK / databaseService.php
Created September 4, 2017 09:46
Drupal 8 - Print raw SQL queries for debugging
<?php
/**
* Debugging using the database connection.
*/
/** @var \Drupal\Core\Database\Connection */
$connection = \Drupal::service('database');
$query = $connection->select('node', 'node');
$query->fields('node', ['nid'])
@WengerK
WengerK / NullAsArray.php
Last active March 10, 2023 17:01
How to Migrate content into Drupal Paragraphs
<?php
namespace Drupal\my_module_migrate\Plugin\migrate\process;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
/**
* Transforms a null value as an empty array usable on sub_process.
@WengerK
WengerK / drupal.module
Created February 27, 2014 15:26
Drupal custom module - How to disable 'promote to front page' and 'sticky' options?
<?php
/**
* Use hook_form_alter to unset both options on node edit forms.
* But on the main admin content page, the options still appear under 'Update options' dropdown.
*/
/**
* Implements hook_form_alter().
* This is remove the promote to frontpage, and make sticky options from node edit pages
@WengerK
WengerK / README.md
Created February 6, 2023 09:29
Debug Symfony - Functional Tests & Panther Tests

Debug Symfony - Functional Tests & Panther Tests

Debugging tests can often be a pain, but using those snippets it's possible to get a lot of informations when using WebTestCase or PantherTestCase.

Get HTML of a page

Sometimes you'll want to inspect the HTML of the page or a specific element. There is no official method for this yet but the following works:

$crawler = $client->request('GET', '/my-url/');
@WengerK
WengerK / FeaturedArticlesResource.php
Created November 7, 2022 14:08
How to create a custom endpoint using Drupal JSON:API
<?php
namespace Drupal\my_custom_resource\Resource;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\jsonapi\ResourceResponse;
use Drupal\jsonapi_resources\Resource\EntityQueryResourceBase;
use Drupal\node\NodeInterface;
use Symfony\Component\HttpFoundation\Request;