Skip to content

Instantly share code, notes, and snippets.

View BBGuy's full-sized avatar

Guy Schneerson BBGuy

View GitHub Profile
@BBGuy
BBGuy / address_utils.php
Created August 30, 2023 14:52
Drupal 2.X contrib modules
<?php
// Get all countries.
\Drupal::service('address.country_repository')->getList();
@BBGuy
BBGuy / cheat_sheet.twig
Last active March 12, 2024 12:35
drupal twig cheat sheet
#sources
# https://www.drupal.org/docs/contributed-modules/twig-tweak-2x/cheat-sheet
# https://straightupcraft.com/articles/how-to-check-if-a-variable-or-value-exists-using-twig
# Render a view
{{ drupal_view('who_s_new', 'block_1') }}
# Get the result of a view
{{ drupal_view_result('who_s_new', 'block_1') }}
@BBGuy
BBGuy / d9_commerce_field_group.php
Last active August 4, 2022 11:57
Drupal 9 - Field Group integration
<?php
// Field group does not by default show on commerce relaed entities and those can be added using a hook
/**
* Implements hook_field_group_content_element_keys_alter
*
* Add field groups to product and product variation.
*/
function fcl_commerce_field_group_content_element_keys_alter(&$keys) {
@BBGuy
BBGuy / file_func.php
Last active February 2, 2024 10:41
Drupal 9 - Working with files
<?php
use Drupal\file\Entity\File;
// Load a file using its ID
$file = File::load($fid);
// Find a file using its URI
@BBGuy
BBGuy / load_select_options.php
Last active March 15, 2023 09:47
Drupal 9 - Working with Taxonomy Entities
<?php
// Util functions to load a select lists options from a taxonomy or a node field with a text list.
function build_form() {
$form = [];
// Load a two level deep select tree from a taxonomy
$options = load_taxonomy_as_select_options('example_taxonomy');
$form['select_tree'] = [
<?php
public function buildForm(array $form, FormStateInterface $form_state) {
// If the form was submited the form build has access to the user entered data.
$user_input = $form_state->getUserInput();
$has_been_submitted = !empty($user_input);
// multiple submit buttons
$form['cases']['submit'] = [
@BBGuy
BBGuy / d8_my_module.install
Last active December 10, 2021 13:08
Drupal 8, 9 Schemas
<?php
use Drupal\Core\Database\Database;
/**
* Implements hook_schema().
*
* Notes:
* 'not null' = requiered
*/
<?php
//******** user ***************//
$current_user = \Drupal::currentUser();
$current_user_id \Drupal::currentUser()->id();
$user = User::load($user_id);
//******** messenger - set messeges ***************//
### Keybase proof
I hereby claim:
* I am BBGuy on github.
* I am guy_schneerson (https://keybase.io/guy_schneerson) on keybase.
* I have a public key whose fingerprint is E0F9 7435 1C0D 3783 458E 5F99 CE91 6F6F 7690 BF49
To claim this, I am signing this object:
<?php
// Working with commerce stock 8.x-1.x
// ****** Usfull commerce code. ******
// Get the curent store and user
$currentStore = \Drupal::service('commerce_store.current_store')->getStore();
$currentUser = \Drupal::currentUser();
// Load the user object