Notes

Drupal 8 - Remove configuration from database
drush ev "\Drupal::configFactory()->getEditable('configuration.name')->delete();"

 

Friday, July 12, 2019 - 16:15
RuntimeException: Unable to determine class for field type found in the configuration
drush sqlq "DELETE FROM cache_config"
drush sqlq "DELETE FROM config WHERE name = 'field.storage.<module_name>.<field_machine_name>' OR data LIKE '%field.storage.<module_name>.<field_machine_name>%'"
drush sqlq "DELETE FROM config_snapshot  WHERE name = 'field.storage.<module_name>.<field_machine_name>' OR data LIKE '%field.storage.<module_name>.<field_machine_name>%'"

On my case It was happening with "taxonomy_term" module and field named "field_location".

Friday, June 28, 2019 - 15:46
Drupal 8 - Load file entity by URI
/* @var \Drupal\file\FileInterface[] $files */
$files = \Drupal::entityTypeManager()
  ->getStorage('file')
  ->loadByProperties(['uri' => $uri]);

 

Monday, June 24, 2019 - 21:35
Drupal 8 fix-permissions.sh

Script:

#!/bin/bash

# Help menu
print_help() {
cat <<-HELP
This script is used to fix permissions of a Drupal installation
you need to provide the following arguments:

  1) Path to your Drupal installation.
  2) Username of the user that you want to give files/directories ownership.
  3) HTTPD group name (defaults to www-data for Apache).

Usage: (sudo) bash ${0##*/} --drupal_path=PATH --drupal_user=USER --httpd_group=GROUP
Example: (sudo) bash ${0##*/} --drupal_path=/usr/local/apache2/htdocs --drupal_user=john --httpd_group=www-data
HELP
exit 0
}

if [ $(id -u) != 0 ]; then
  printf "**************************************\n"
  printf "* Error: You must run this with sudo or root*\n"
  printf "**************************************\n"
  print_help
  exit 1
fi

drupal_path=${1%/}
drupal_user=${2}
httpd_group="${3:-www-data}"

# Parse Command Line Arguments
while [ "$#" -gt 0 ]; do
  case "$1" in
    --drupal_path=*)
        drupal_path="${1#*=}"
        ;;
    --drupal_user=*)
        drupal_user="${1#*=}"
        ;;
    --httpd_group=*)
        httpd_group="${1#*=}"
        ;;
    --help) print_help;;
    *)
      printf "***********************************************************\n"
      printf "* Error: Invalid argument, run --help for valid arguments. *\n"
      printf "***********************************************************\n"
      exit 1
  esac
  shift
done

if [ -z "${drupal_path}" ] || [ ! -d "${drupal_path}/sites" ] || [ ! -f "${drupal_path}/core/modules/system/system.module" ] && [ ! -f "${drupal_path}/modules/system/system.module" ]; then
  printf "*********************************************\n"
  printf "* Error: Please provide a valid Drupal path. *\n"
  printf "*********************************************\n"
  print_help
  exit 1
fi

if [ -z "${drupal_user}" ] || [[ $(id -un "${drupal_user}" 2> /dev/null) != "${drupal_user}" ]]; then
  printf "*************************************\n"
  printf "* Error: Please provide a valid user. *\n"
  printf "*************************************\n"
  print_help
  exit 1
fi

cd $drupal_path
printf "Changing ownership of all contents of "${drupal_path}":\n user => "${drupal_user}" \t group => "${httpd_group}"\n"
chown -R ${drupal_user}:${httpd_group} .

printf "Changing permissions of all directories inside "${drupal_path}" to "rwxr-x---"...\n"
find . -type d -exec chmod u=rwx,g=rx,o= '{}' \;

printf "Changing permissions of all files inside "${drupal_path}" to "rw-r-----"...\n"
find . -type f -exec chmod u=rw,g=r,o= '{}' \;

printf "Changing permissions of "files" directories in "${drupal_path}/sites" to "rwxrwx---"...\n"
cd sites
find . -type d -name files -exec chmod ug=rwx,o= '{}' \;

printf "Changing permissions of all files inside all "files" directories in "${drupal_path}/sites" to "rw-rw----"...\n"
printf "Changing permissions of all directories inside all "files" directories in "${drupal_path}/sites" to "rwxrwx---"...\n"
for x in ./*/files; do
  find ${x} -type d -exec chmod ug=rwx,o= '{}' \;
  find ${x} -type f -exec chmod ug=rw,o= '{}' \;
done
echo "Done setting proper permissions on files and directories"

Usage: 

sudo fix-permissions.sh \
--drupal_path=/path/to/the/drupal/install \
--drupal_user=your_desired_user

 

Monday, April 8, 2019 - 11:28
Drupal 8 - Composer Update Core
# composer.json file:
# {
#   "name": "drupal/drupal",
#    ..... 
#   "require": {
#           "drupal/core": "^8.6.0",
#    .....
#  }
# Update Drupal core
composer update drupal/core webflo/drupal-core-require-dev --with-dependencies

 

Monday, December 17, 2018 - 09:41
Git reset tricks
# `soft` flag and `HEAD^` will drop last commit and keep changes.
git reset --soft HEAD^

# `hard` flag and `HEAD^` will drop last commit but it won't keep changes.
git reset --hard HEAD^

 

Monday, November 5, 2018 - 15:17
Drupal 8 - Sort multi arrays
// Import \Drupal\Component\Utility\SortArray class
use Drupal\Component\Utility\SortArray;

// Then applied it manually
uasort($result, function($a, $b) {
  return SortArray::sortByKeyString($a, $b, 'ARRAY_KEY_TO_SORT');
});

// Also It's possible to use without a closure function
uasort($form['elements'], [
  '\Drupal\Component\Utility\SortArray',
  'sortByWeightProperty'
]);

 

Wednesday, October 24, 2018 - 16:12
Drupal 8 - $link and $url objects
/** @var Drupal\Core\Utility\LinkGenerator $linkGenerator */
$linkGenerator = \Drupal::service('link_generator');

/** @var Drupal\Core\Url $installerUrl */
$installerUrl = Drupal\Core\Url::fromUri('base://core/install.php');

/** @var Drupal\Core\Url $installerLink */
$installerLink = $linkGenerator->generate('Install', $installerUrl);

// Print variable or invoke `__toString` magic method directly:  { "<a href="/core/install.php">Install</a>" }
var_dump($installerLink->__toString());

/** @var Drupal\Core\Url $externalUrl */
$externalUrl = Drupal\Core\Url::fromUri('https://www.keboca.com/notes', ['query' => ['foo' => 'bar']]);

/** @var Drupal\Core\GeneratedLink $externalLink */
$externalLink = $linkGenerator->generate('KEBOCA - Notes', $externalUrl);

// Print variable or invoke `__toString` magic method directly:  { "<a href="https://www.keboca.com/notes?foo=bar">KEBOCA - Notes</a>" }
var_dump($externalLink->__toString());

/** @var Drupal\Core\Url $internalUrl */
$internalUrl = Drupal\Core\Url::fromRoute('system.admin');

/** @var Drupal\Core\GeneratedLink $internalLink */
$internalLink = $linkGenerator->generate('Admin page', $internalUrl);

// Print variable or invoke `__toString` magic method directly:  { "<a href="/admin">Admin page</a>" }
var_dump($internalLink->__toString());

// It returns the raw URL value:  { "/admin/structure/types" }
$url = \Drupal::url('entity.node_type.collection');

/** @var  Drupal\Core\StringTranslation\TranslatableMarkup $text */
$text = t('Visit the <a href=":url">content types</a> page', [
  ':url' => $url
]);

// To render translatable as final HTML: { "Visit the <a href="/admin/structure/types">content types</a> page" }
var_dump($text->render());

 

 

Tuesday, October 23, 2018 - 20:36
Drupal 7 - Retrieve field value properly
/** @var array $field_items */
$field_items = field_get_items('node', $node, 'FIELD_MACHINE_NAME');

/** @var array $field_value */
$field_value = (!empty($field_items)) ? reset($field_items) : [];

 

Monday, October 22, 2018 - 10:41
Drupal 7 - Retrieve current node object
// On the page node/%node, the router loads the %node object
if($node = menu_get_object()) {
  echo '<pre>' . print_r($node, 1); 
}

 

Monday, October 22, 2018 - 10:25