Search code examples
drupalcomposer-phpdrupal-8drupal-moduleskint

How to Install Kint Globally Drupal - Without Devel Module


I was trying to setup an easy way to debug the Lando Drupal installations using Kint module. But I don't want to install the Devel and Devel Kint Extras modules in each Installations.

What I have initially done is installed the Kint globally in my local.

composer global require kint-php/kint

But it is not possible to access the functions inside a Lando installation by requiring the global autoload file to the settings.local.php

include_once('/home/username/.composer/vendor/autoload.php');

The same is working on local Drupal installation, but not inside Lando installations.


Solution

  • Yes, it is possible to install PHP Kint Library globally for Drupal Projects without Devel Module installed.

    Install Kint Globally

    Install PHP Kint Library globally. 

    composer global require kint-php/kint

    Then copy the file path where the composer globally installed. If you are unable to find where the composer global directory, use the below command.

    composer config --list --global

    And figure out the [home] directory from the list.

    Now go to your settings.local.php in your drupal project. And include the global autoload file as below.

    include_once('/var/www/.composer/vendor/autoload.php');
    if (class_exists('Kint')) {
      Kint::$depth_limit = 4;
    }
    

    Change the /var/www/.composer/ to your home directory.
    For example: /home/adharsh/.config/composer/vendor/autoload.php

    Usually the settings.local.php file is gitignored, so there will be no change for your code base and the Kint is now ready to use.

    Yeah, its ready to use.

    Go to the file you want to debug and use d() function to debug.
    Example: d($variable);

    More functions are available in the Kint documentation.

    Install in Lando

    NOTE: If you are a lando user, you have to ssh (lando ssh) into lando and globally install Kint. The composer global directory will be in /var/www/.composer . You may need to reinstall the Kint globally if you are rebuilding Lando.
    But you can add the run command in lando file to install the composer on lando build.

    services:
      appserver:
        type: 'php:7.4'
        run:
          - "cd $LANDO_MOUNT && wget https://getcomposer.org/download/2.3.9/composer.phar"
          - "chmod +x composer.phar"
          - "php composer.phar install -n"
          - "php composer.phar global require kint-php/kint"
          - "rm composer.phar"
    

    Replace the https://getcomposer.org/download/2.3.9/composer.phar  composer download link with you specific composer version from getcomposer and save the lando file.