Skip to content

Instantly share code, notes, and snippets.


Lincoln Chawora lincoln-chawora

  • TPX Impact
View GitHub Profile
lincoln-chawora / YOUR_MODULE_NAME.module
Created June 30, 2021 15:50
How to render a view in code and check if it's empty
use Drupal\views\ViewExecutable;
use Drupal\views\Views;
* Implements hook_preprocess_HOOK().
function YOUR_MODULE_NAME_preprocess_HOOK(&$variables) {
$some_entity_id = 10;
$view = Views::getView('name_of_view');
if (is_object($view) && $most_linked_profiles_view instanceof ViewExecutable) {
lincoln-chawora / YOUR_MODULE_NAME.install
Created June 30, 2021 15:04
Drupal 8 How to update taxonomy url's alias in the database
use Drupal\Core\Database\Database;
* Update tags url alias'.
function YOUR_MODULE_NAME_update_8001() {
// "path_alias" is the name of the table being updated (this comes from the pathauto module)
// "alias" is the name of the field/column being updated.
// So whats happening here is that, we're updating the alias field/column on the path_alias table.
// The update being done is we're replacing the value of "//tags" with "/news-and-events/topics/".
lincoln-chawora / YOUR_MODULE_NAME.install
Last active June 30, 2021 15:20
How to delete multiple nodes by ID in Drupal 8 using hook update
* Delete multiple nodes by ID.
function YOU_MODULE_NAME_update_8001() {
$nids = [8418,8419];
/** @var \Drupal\node\NodeStorageInterface $storage */
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
if ($nodes = $storage->loadMultiple($nids)) {
lincoln-chawora / a_some_php_file_name.php
Created February 26, 2020 11:22
How to pass php values into javascript (js) in drupal 7
View a_some_php_file_name.php
function YOUR_THEME_some_preprocess_function(&$variables) {
$actual_value = array(
'my_text' => 'What ever data you wanna pass from php to js',
// Note, for this function to work $actual_value must have a key (my_text) and a value (what ever data...)
// the key will be used in javascript to get the value
drupal_add_js(array('nameUsedInJs' => $actual_value), 'setting');
lincoln-chawora / clickable_card.js
Created February 19, 2020 11:06
How to make a card clickable using jquery
View clickable_card.js
(function card($, Drupal) {
Drupal.behaviors.theme_card = {
attach(context) {
$('[data-card]', context)
.each(function eachCard() {
const $link = $(this)
lincoln-chawora / a_how_to_sort_an_array_by_a_value_inside_the_array.php
Last active February 16, 2020 18:34
How to sort an array by a value inside the array in php
View a_how_to_sort_an_array_by_a_value_inside_the_array.php
uasort($valid_variants_ids, function ($a, $b) {
// if the first item and the second item have the same value, do nothing.
if ($a['study_option_weight'] == $b['study_option_weight']) {
return 0;
// if the first item is less than the second item, move the first option down by one, otherwise move it up by one.
return $a['study_option_weight'] < $b['study_option_weight'] ? -1 : 1;
lincoln-chawora / Get-parent-entity-from-field-twig-file.html.twig
Created January 28, 2020 00:09
How to access the parent entity from a field twig template in drupal 8
View Get-parent-entity-from-field-twig-file.html.twig
{# This will work for paragraphs or nodes. #}
lincoln-chawora / YOUR_MODULE_NAME.install
Created January 10, 2020 18:56
How to update an existing drupal 8 site setting field with a hook update
* @file
* Install, update and uninstall functions for the YOUR_MODULE_NAME module.
* Implements hook_update_N().
* Update field_example on the my_example_setting site
* setting to have all the term_example taxonomy terms selected.
lincoln-chawora /
Last active January 10, 2020 18:57
How change the allowed values of a select list or radio field dynamically in drupal 8
allowed_values: { }
# Add the line below to your file as it will reference the function you'll create in your .module file
allowed_values_function: example_allowed_values_function
lincoln-chawora /
Last active December 10, 2019 00:10
How to set up xdebug with mamp and PHP Storm (drupal 8 site) (MacOS)
  1. Install the xdebug chrome extension and turn it on by clicking on it and clicking on debug:

  2. Type the following into your terminal (This will return the location of the php ini file being used, once you have the path you will need to open the file - also note the php version being used, this will be important later):

php --ini
  1. Find xdebug in the php.ini file and add the following (if the zend_extension line has a semi colon ";" remove it):