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 / 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.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 / 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']