Skip to content

Instantly share code, notes, and snippets.

View BBGuy's full-sized avatar

Guy Schneerson BBGuy

View GitHub Profile
@BBGuy
BBGuy / dc_add_default_product.module
Created August 8, 2016 14:40
Create a default producr if one was not set.
/**
* Implements hook_form_alter().
*/
function dwwcommerce_form_alter(&$form, &$form_state, $form_id) {
// The style edit form.
if ($form_id == 'product_style_node_form') {
// Add a custom submit.
$form['actions']['submit']['#submit'][] = 'mymodule_node_prod_display_form_submit';
}
}
<?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');
<?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
### 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
//******** user ***************//
$current_user = \Drupal::currentUser();
$current_user_id \Drupal::currentUser()->id();
$user = User::load($user_id);
//******** messenger - set messeges ***************//
@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
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 / 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 / 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 / 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) {