Drupal 8 Notes

 

Deprecated Calls :

  • watchdog

Instead use

\Drupal::logger('devel')->notice($message); // to log notice

\Drupal::logger('devel')->error($message); // to log error

  • checkPlain

Will be removed from Drupal 9 onwards so suggested not to use in Drupal 8 as well.

Rely on Twig's auto-escaping feature, or use the #plain_text key when constructing
a render array that contains plain text in order to use the renderer's auto-escaping feature.

  • valid_email_address($mail)

Instead use

\Drupal::service('email.validator')->isValid($var_email)

  • l()

Instead use

\Drupal::l(t('New Contact'), $var_url)

  • url()

Instead use

For Internal url :
use Drupal\Core\Url;
$url = Url::fromRoute('book.admin');

For External url :
use Drupal\Core\Url;
$url = Url::fromUri('http://www.example.com/');

 

To Disable Drupal 8 caching during development

1. Copy and rename the sites/example.settings.local.php to be sites/default/settings.local.php

$ cp sites/example.settings.local.php sites/default/settings.local.php

2. Open settings.php file in sites/default and uncomment these lines:

if (file_exists(__DIR__ . '/settings.local.php')) {

include __DIR__ . '/settings.local.php';

}

This will include the local settings file as part of Drupal's settings file.

3. Uncomment this line to enable the null cache service:

$settings['container_yamls'][] = DRUPAL_ROOT . '/sites/development.services.yml';

4. Open settings.local.php and change the following to be TRUE if you want to work with enabled css- and js-aggregation:

$config['system.performance']['css']['preprocess'] = FALSE;

$config['system.performance']['js']['preprocess'] = FALSE;

5. Uncomment these lines in settings.local.php to Disable the render cache and Disable Dynamic Page Cache

$settings['cache']['bins']['render'] = 'cache.backend.null';

$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';

If you do not want to install test modules and themes, set following to FALSE

$settings['extension_discovery_scan_tests'] = TRUE;

6. Open services.yml in the sites folder and add the following block (to disable twig cache)

 parameters:

      twig.config:

           debug : true

          auto_reload: true

         cache: false

7. Afterwards you have to rebuild the Drupal cache. Otherwise your website will encounter an unexpected error on page reload. This can be done by visiting the following URL from your Drupal 8 website:

http://yoursite/core/rebuild.php

8. Finished! Now you're able to develop in Drupal 8 without manual cache rebuilds on a regular basis.

Reference :

  1. https://cryptic.zone/blog/drupal-8-cheatsheet-developers

 

Add new comment