Skip to content

Instantly share code, notes, and snippets.

View WengerK's full-sized avatar
🤓

Kevin Wenger WengerK

🤓
View GitHub Profile
@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 / 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;
@WengerK
WengerK / OrderTotalPriceRange.php
Last active June 3, 2022 09:14
Drupal Commerce Custom Order Total Range Condition
<?php
namespace Drupal\my_custom_commerce_range_condition\Plugin\Commerce\Condition;
use Drupal\commerce\Plugin\Commerce\Condition\ConditionBase;
use Drupal\commerce_price\Price;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
/**
@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 / DatabaseContext.php
Last active November 4, 2021 13:16
Behat on Drupal - clearing Data Between Scenarios
<?php
namespace Drupal\Behat\Context\Drupal;
use Drupal\Component\Utility\Random;
use Drupal\DrupalExtension\Context\RawDrupalContext;
/**
* Defines Database features from the specific context.
*/
@WengerK
WengerK / MyKernelTest--no-create.php
Created February 19, 2021 13:40
Article Ressources - Factory Lollipop - Speeding up Kernel tests on Drupal
<?php
/**
* Example of Factory Lollipop usage for a KernelTest.
*/
class MyKernelTest extends LollipopKernelTestBase {
/**
* {@inheritdoc}
*/
@WengerK
WengerK / .travis.yml
Last active August 27, 2020 09:33
Drupal 8 contributed modules using Docker
language: php
services:
- docker
env:
global:
# The module name to be mounted and tested in the Docker.
- MODULE_NAME="my_module"
@WengerK
WengerK / AssignmentsParameters-bad.php
Created August 23, 2019 16:16
Article Ressources - Refactoring - an ode to code
function discount(\DateTime $start_date, $bar) {
if ($bar > 50) {
$start_date->add('2 days');
}
// do some calculation based on the $start_date...
return $foo;
}
@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;