Best Practice
Use 'elseif' instead of 'else if'
#CodingTips
As of PHP 5.4 you can also use the short array syntax, which replaces array() with []
#CodingTips
Functions in general shall be named using snake_case(say, my_function()), and using camelCase(say, myFunction()) when declared within a plugin class
#CodingTips
Variables in general shall be named using snake_case(say, $my_variable), and using camelCase(say, $myVariable) when declared within a plugin class
#CodingTips
Manage automatically assigning of new permissions whenever a module is enabled here- admin/config/people/accounts
#ConfigurationTips
Manage source of Main-menu and User-menu links here- admin/structure/menu/settings
#ConfigurationTips
Helper function(s) shall be named prefixing an underscore(say, _my_helper_function()), which can prevent hooks from being called
#CodingTips
Ideally, configuring of 'Private file system path' at admin/config/media/file-system should be located outside of your Drupal root folder(say, ../my_private_files)
#ConfigurationTips
You should be aware that uploading files as 'Private file' will slow down the process of loading the files as Drupal has to be bootstrapped for every file that needs to be downloaded
#ConfigurationTips #BeAware
Code should always be pushed up(dev -> staging -> production) and databases should only be pushed down(production -> staging -> dev)
#DevelopmentTips
Get Raw SQL Query of drupal dynamic queries before executing it using $query->__toString();
#DebugTips
In VI-Editor, Press ESC key to come in command mode and for undo type :U and for redo type :Ctrl+R
#LinuxTips
Insert queries must always use a query builder object(layer of abstraction), allowing individual database drivers special handling for column values (if applicable), example case for LOB and BLOB fields.
#DatabaseQueryTips
Drupal uses the .inc extension to prevent files from being executed directly.
#DevelopmentTips
XDebug
ReplyDeleteLando
https://docs.lando.dev/guides/lando-with-vscode.html#getting-started
Restart VSCode
launch.js
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000,
"pathMappings": {
"/app": "${workspaceFolder}/"
}
}
zend_extension="/usr/lib/php/7.2/modules/xdebug-2.7.1.so"
/usr/lib/php/20170718/xdebug.so
services:
appserver:
webroot: web
xdebug: true
config:
php: .vscode/php.ini
touch .vscode/php.ini
code .vscode/php.ini
[PHP]
; Xdebug
xdebug.max_nesting_level = 256
xdebug.show_exception_trace = 0
xdebug.collect_params = 0
; Extra custom Xdebug setting for debug to work in VSCode.
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_host = ${LANDO_HOST_IP}
; xdebug.remote_connect_back = 1
xdebug.remote_log = /tmp/xdebug.log
lando rebuild -y
touch .vscode/launch.json
code .vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000,
"log": true,
"pathMappings": {
"/app/": "${workspaceFolder}/",
}
}
]
}
#Debugging PhpUnit
Vagrant
launch.js
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000,
"pathMappings": {
"/var/www/lofi/docroot": "${workspaceFolder}/docroot"
}
}
vagrant ssh
// Xdebug changes
sudo vim /etc/php/7.2/fpm/conf.d/20-xdebug.ini
// Check if Xdebug installed
php -i | grep xdebug
// OR, install Xdebug
sudo apt install php-pear
sudo pecl install xdebug
php -i | grep xdebug
// You should add "zend_extension=/usr/lib/php/20170718/xdebug.so" to php.ini
// Actully, instead I added above /etc/php/7.2/fpm/conf.d/20-xdebug.ini instead ini.php because OPcache shall not cause issue loading Xdebug first.
sudo vim /etc/php/7.2/fpm/php.ini
sudo service php7.2-fpm restart
php -i | grep xdebug
/*
zend_extension=/usr/lib/php/20170718/xdebug.so
xdebug.coverage_enable=0
xdebug.default_enable=1
xdebug.remote_enable=1
xdebug.remote_connect_back=1
xdebug.remote_host=10.0.2.2
xdebug.remote_port=9000
xdebug.remote_log=/tmp/xdebug.log
xdebug.remote_autostart=true
xdebug.idekey="VSCODE"
xdebug.max_nesting_level=256
*/
sudo service php7.2-fpm restart
Note: If suddenly some day Xdebug stops working remove all breakpoints, variable watches etc and restart.
connection 9: read ECONNRESET
May be ignoring that breakpoint.
Remove other folders from workspace and restart VScode
TWIG
ReplyDeleteDEBUG
{{ dump(title) }}
{{ dump() }}
To dump only the available variable keys use:
{{ dump(_context|keys) }}
{{ element['#myvars'].var1 }}
https://www.drupal.org/docs/theming-drupal/twig-in-drupal/discovering-and-inspecting-variables-in-twig-templates
Enable twig debug
ReplyDelete- cd sites/default
- sudo touch services.yml
- sudo chmod 777 services.yml
- cat default.services.yml > services.yml
- sudo chmod 644 services.yml
PHPCS
ReplyDelete# List installed coding standards vendor/bin/phpcs -i
The installed coding standards are PEAR, Zend, PSR2, MySource, Squiz, PSR1 and PSR12
# If Drupal and DrupalPractice not installed, then do it
vendor/bin/phpcs --config-set installed_paths vendor/drupal/coder/coder_sniffer
The installed coding standards are PEAR, Zend, PSR2, MySource, Squiz, PSR1, PSR12, Drupal and DrupalPractice
# Test the code standards
vendor/bin/phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml path/to/module
vendor/bin/phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml web/modules/custom/yoyoyo
https://www.drupal.org/node/1587138