Skip to content

Instantly share code, notes, and snippets.

View davidjguru's full-sized avatar
🍅
Working within a pomodoro cycle

David Rodriguez davidjguru

🍅
Working within a pomodoro cycle
View GitHub Profile
@DanLaufer
DanLaufer / Drupal 8 - Get a field from a paragraph reference in node preprocessing
Last active April 13, 2023 12:38
Drupal 8 - Get a field from a paragraph in template_preprocess_node()
if($node) {
// we have a node, so use headline in hero, or fall back to node title
if($node->field_hero_section && $node->field_hero_section->getValue() && $node->field_hero_section->getValue()[0]) {
$paragraph = $node->field_hero_section->getValue()[0];
$paragraph_entity = \Drupal\paragraphs\Entity\Paragraph::load( $paragraph['target_id'] );
return $paragraph_entity->field_headline->value;
} else {
return $node->getTitle();
}
}
@bdlangton
bdlangton / Blocks.md
Last active October 12, 2023 08:40
Drupal 8 programmatic solutions

Render custom blocks

$bid = 'myblock';
$block = \Drupal\block_content\Entity\BlockContent::load($bid);
$render = \Drupal::entityTypeManager()->getViewBuilder('block_content')->view($block);

Render plugin blocks

$block_manager = \Drupal::service('plugin.manager.block');
@keopx
keopx / settings.local.php
Last active March 25, 2024 12:22
Drupal 8 Redis settings.local.php
<?php
/**
* Set redis configuration.
*/
/** @see: https://docs.platform.sh/frameworks/drupal8/redis.html */
if (extension_loaded('redis')) {
// Set Redis as the default backend for any cache bin not otherwise specified.
// $settings['cache']['default'] = 'cache.backend.redis';
@jez500
jez500 / d8-views-exp-form-alter.php
Last active October 12, 2023 12:20
Drupal 8 - Hook form alter views exposed form
<?php
/**
* Implements hook_form_alter().
*/
function MYMODULE_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$view_names = array('my_view_name');
$view = $form_state->getStorage('view');
if ($form_id == 'views_exposed_form' && in_array($view['view']->id(), $view_names)) {
// Do some shilzzle.
@himerus
himerus / nodemaker_term_create.php
Created January 21, 2017 11:38
Create a taxonomy term programmatically in Drupal 8.
<?php
/**
* @file
* Contains various helper functions.
*/
use Drupal\taxonomy\Entity\Term;
/**
@WengerK
WengerK / ContactForm-basic.php
Last active June 12, 2023 19:39
Drupal 8 — Inline validation in forms
<?php
/**
* @file
* Contains \Drupal\my_contact\Form\ContactForm.
*/
namespace Drupal\my_contact\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
@JeffTomlinson
JeffTomlinson / MyService.php
Last active July 22, 2023 16:28
Drupal 8 Configuration Dependency Injection
<?php
namespace Drupal\my_module\Services;
use Drupal\Core\Config\ConfigFactory;
/**
* Class MyService.
*
* @package Drupal\my_module\Services
@crittermike
crittermike / IntegerDropdownWidget.php
Created October 17, 2016 20:14
Drupal 8 form widget example: creates a select dropdown for integer fields. Place in src/Plugin/Field/FieldWidget
<?php
/**
* @file
* Defines a dropdown widget for integer fields.
*/
namespace Drupal\nba_content_core\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldFilteredMarkup;
use Drupal\Core\Field\FieldItemListInterface;
@illepic
illepic / file_rel_path_d8.php
Last active August 23, 2023 09:40
Get relative path for fid in Drupal 8
<?php
use Drupal\file\Entity\File;
// public://images/blah.jpg
$drupal_file_uri = File::load($fid)->getFileUri();
// /sites/default/files/images/blah.jpg
$image_path = file_url_transform_relative(file_create_url($drupal_file_uri));
@Jaesin
Jaesin / delete_content_entities.d8.php
Created June 22, 2015 07:14
Delete all content entities in Drupal 8.
<?php
// Delete all nodes.
entity_delete_multiple('node', \Drupal::entityQuery('node')->execute());
// Delete all files.
entity_delete_multiple('file', \Drupal::entityQuery('file')->execute());
// Delete all taxonomy terms.
entity_delete_multiple('taxonomy_term', \Drupal::entityQuery('taxonomy_term')->execute());