Notes
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
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^
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'
]);
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());
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) : [];
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);
}
Drupal 7 - Load taxonomy tree
// Retrieve vocabulary based on its machine name
$vocabulary = taxonomy_vocabulary_machine_name_load('tags');
// Load whole taxonomies related to given vocabulary
$tree = taxonomy_get_tree($vocabulary->vid);
MySQL - CREATE DATABASE AND USER
-- Create fresh database
CREATE DATABASE drupal7_db;
-- Create a new MySQL user with specific password
CREATE USER drupal7_dbuser@localhost IDENTIFIED BY 'a2NUpXBffCYUHP6a';
-- Give full access to new MySQL user to new database just created
GRANT ALL PRIVILEGES ON drupal7_db.* TO 'drupal7_dbuser'@'localhost';
-- Rebuild privileges to make persistent those changes
FLUSH PRIVILEGES;
PostgreSQL - Sequence Manipulation Functions
-- Retrive current sequence value
SELECT last_value FROM <sequence_name>;
-- Update sequence value
SELECT setval('sequence_name',1000);
Drupal 7 - MySQL SELECT variable Table
-- Retrieve site name stored into variable table directly from SQL
SELECT name, CONVERT (value USING utf8)
FROM `variable`
WHERE name = 'site_name'