Drupal 8 Twig cheat sheet


To check if a field has a value, check to see if the 0 array item is populated. Simply checking if content.field_name is truthy will not work since that variable will be defined even if the field is empty.

Common Git Commands

#On Server
git init --bare

#On Client
git init
git remote add reponame user@server:/path/to/repo.git
git push name-of-repo master

#List Remote
git remote -v
origin  git@hostname:USERNAME/REPOSITORY.git (fetch)
origin  git@hostname:USERNAME/REPOSITORY.git (push)

#Change Remote url
git remote set-url origin https://hostname/USERNAME/REPOSITORY.git

How to Create a MySQL database backup snapshot and restore it

Sometimes you want to create a snapshot of the site database at a specific point in time, and then do some modifications and if anything has gone wrong restore that snapshot

To create a database snapshot run the following command

 mysqldump --add-drop-table -u mysqluser -p database_name > ~/db_name_date_time.sql

To restore that snapshot run the following command

mysql -u mysqluser -p database_name < ~/db_name_date_time.sql

You can also take a snapshot of the site directory using the command

How to add Custom Permissions to your Drupal 8 module

In order to add custom permissions we used to use hook_permission in Drupal 7, however in Drupal 8 this is done through the yourmodulename.permissions.yml file

Here is an example of what the file should look like

administer commerce_amazon_slider:
  title: 'Administer Commerce Amazon Slider'
  description: 'View, edit and update Commerce Amazon Slider configurations'