Skip to content

Instantly share code, notes, and snippets.

View lincoln-chawora's full-sized avatar

Lincoln Chawora lincoln-chawora

  • TPX Impact
View GitHub Profile
@lincoln-chawora
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
{# This will work for paragraphs or nodes. #}
element['#object']
@lincoln-chawora
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
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)) {
$node_storage->delete($nodes);
@lincoln-chawora
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
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
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
lincoln-chawora / clickable_card.js
Created February 19, 2020 11:06
How to make a card clickable using jquery
(function card($, Drupal) {
Drupal.behaviors.theme_card = {
attach(context) {
$('[data-card]', context)
.once('theme-card')
.each(function eachCard() {
// https://inclusive-components.design/cards/
const $link = $(this)
.find('a')
@lincoln-chawora
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
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
lincoln-chawora / field.storage.node.field_name.yml
Last active January 10, 2020 18:57
How change the allowed values of a select list or radio field dynamically in drupal 8
settings:
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
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
lincoln-chawora / load_node.php
Last active January 10, 2020 18:48
How load a node in drupal 8
$route_name = \Drupal::routeMatch()->getRouteName();
if ($route_name == 'entity.node.canonical') {
node = \Drupal::routeMatch()->getParameter('node');
$node_type = $node->getType();
$title = $node->getTitle();
$visible = $node->isPublished();
$node->setPromoted(TRUE);
$uid = $node->getOwnerId();
// $author will be user object of the person who created the node.