Drupal 7: Create a datetime field in a schema with db_create_table

To create a table in Drupal 7, you can use the “my_module_update_7000” hook in the “my_module.install” file. The datetime type is unfortunately not mapped, which is why you have to use the “mysql_type” key const MY_MODULE_TABLE_NAME = ‘my_module_table’; function get_my_module_schema(): array { return [ ‘description’ => ‘description’, ‘fields’ => [ ‘id’ => [‘type’ => ‘serial’, … Read more

Install drupal/console on drupal 9

MacBook avec code sur un bureau

The drupal/console package was never made fully compatible with Drupal 9, and its development has now stopped. If you are starting a recent project, the command-line reference is now Drush. The modern solution: Drush Drush is actively maintained and compatible with Drupal 9, 10 and beyond: composer require drush/drush You then access the commands through … Read more

PHP Spaceship Operator <=>

Code sur écran d'ordinateur

A little-known but very useful operator, for example to sort an array, is the spaceship operator: <=> It compares two values and returns an integer (-1, 0, 1) depending on the result. $a < $b returns -1 $a = $b returns 0 $a > $b returns 1 One way to see it is here: https://3v4l.org/WcRSo … Read more

Categories PHP

PHP CS Fixer – How to keep your code clean

Code sur écran d'ordinateur

A really useful tool to keep your code clean is PHP Coding Standards Fixer. It allows us to follow the PHP standards defined in PSR-1, PSR-2, etc. It is also possible to configure it to follow the style of your team with a documentation. The recommended way to install it is outside your code, but … Read more

PHPUnit – How to test a controller that throws an error

Code sur écran d'ordinateur

Let’s say we have a method in a controller like this: /** * @Route(path=”/add”, name=”add”, methods={“POST”}) */ public function add(Request $request,): Response { throw new BadRequestHttpException(‘Error’); } To test that the exception is properly thrown, just write this public function testList(): void { $this->expectException(BadRequestHttpException::class); $client = self::createClient(); $client->catchExceptions (false); $crawler = $client->request( Request::METHOD_POST, ‘/add’ ); … Read more

macOS – Install PHP 7.4 and force its use

MacBook Pro affichant du code

Start by installing homebrew /bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)” PHP 7.4 has been removed from homebrew-core. You need the tap that keeps the older versions brew tap shivammathur/php brew install shivammathur/php/php@7.4 brew link –force php@7.4 brew services start php@7.4 export PATH=”/usr/local/opt/php@7.4/bin:$PATH” export PATH=”/usr/local/opt/php@7.4/sbin:$PATH” On Apple Silicon, replace /usr/local/opt with /opt/homebrew/opt Once this is done, check … Read more

Categories OSX

Install Xdebug 3 with PhpStorm on macOS

MacBook Pro affichant un éditeur de code

For this example, we will use the Docker installation as well as the Symfony 5 setup described in another article: Article Docker, Symfony 5 Creating an alias for the IP used On MacOS, it is necessary to create an alias for the IP we use. It is common to use the IP: 10.254.254.254 The command … Read more

Symfony4 how to create a custom error page 404

Code sur écran d'ordinateur

You have to start by installing the necessary bundle: composer require symfony/twig-pack When done, create the template error404.html.twig templates/ └─ bundles/ └─ TwigBundle/ └─ Exception/ └─ error404.html.twig Example of the error404.html.twig template {% extends ‘base.html.twig’ %} {% block body %} <h1>Page not found</h1> <p> The requested page couldn’t be located. Checkout for any URL misspelling … Read more