Skip to content

Instantly share code, notes, and snippets.

View BBGuy's full-sized avatar

Guy Schneerson BBGuy

View GitHub Profile
<?php
// Get the request object.
$request = \Drupal::request();
// Query - get all parameter.
$q = $request->query->all();
// Check if a query parameter exists.
if ($request->query->has('commerce_product_v_id')) {
// Get the query parameter.
$pvid = $request->query->get('commerce_product_v_id');
@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
*/
@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 / dc_emw.php
Last active November 16, 2022 14:08
Working with Drupal commerce entities using entity_metadata_wrapper. Just a bunch of exampled copied from the web or added by me. will keep adding over time. now more then just commerce entities
<?php
// Basics
// Get a value.
$value = $wrapper->field_x->value();
// If the field is a reference field you will get the field object
// To get the wrapper back use:
$wrapper_b = $wrapper->field_ref;
// To set a value.
@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'] = [
@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();
<?php
// some examples on
// https://docs.drupalcommerce.org/commerce2/developer-guide/products/product-architecture/code-recipes
// https://docs.drupalcommerce.org/commerce2/developer-guide/products/product-management/code-recipes
// https://docs.drupalcommerce.org/commerce2/developer-guide/products/displaying-products/code-recipes
// Work with orders.
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = \Drupal::entityTypeManager()->getStorage('commerce_order')->load($order_id);
<?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 / 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 / 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') }}