Skip to content

Instantly share code, notes, and snippets.

Avatar

Guy Schneerson BBGuy

View GitHub Profile
View Drupal8Form.php
<?php
public function buildForm(array $form, FormStateInterface $form_state) {
// Text field
$form['text_field'] = [
'#type' => 'textfield',
'#title' => $this->t('Name'),
'#description' => $this->t('Enter your name.'),
'#maxlength' => 50,
'#size' => 50,
@BBGuy
BBGuy / d8_my_module.install
Last active Apr 19, 2021
Drupal 8 Schemas
View d8_my_module.install
<?php
use Drupal\Core\Entity\EntityTypeInterface;
/**
* Implements hook_schema().
*
* Notes:
* 'not null' = requiered
*/
View drupal_8_cheat_sheet.php
<?php
//******** user ***************//
$current_user = \Drupal::currentUser();
$current_user_id \Drupal::currentUser()->id();
$user = User::load($user_id);
//******** messenger - set messeges ***************//
View gist:5a75bd776cdf632ae7a473023ebae8ee
### 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:
View d8_commerce_stock.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
$user = \Drupal\user\Entity\User::load($current_user->id());
View drupal_8_services.php
<?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 / dc_add_default_product.module
Created Aug 8, 2016
Create a default producr if one was not set.
View dc_add_default_product.module
/**
* 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';
}
}
View Drupal_8_commerce_code_cs.php
<?php
// Work with orders.
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = \Drupal::entityTypeManager()->getStorage('commerce_order')->load($order_id);
$order_id = $order->id();
// Work with line items.
$qty = $order_item->getQuantity();
@BBGuy
BBGuy / commerce_tools.php
Last active Jan 11, 2018
Drupal 7 commerce utility functionality
View commerce_tools.php
<?php
/**
* Get the node ID for a commerce product using it's product ID.
*
* Asumes only one. If not will return the first node referencing the product.
* If the commerce product has no product display will return FALSE.
*/
function _get_product_referencing_nid($product_id) {
$query = new EntityFieldQuery();
@BBGuy
BBGuy / gist:592f191e2ab120f75df0
Last active Aug 29, 2015
Working with Images in Drupal
View gist:592f191e2ab120f75df0
// Using the Entity API
$image = $MY_ENITIY->field_image->value();
$variables = array(
'style_name' => 'my_style__img',
'path' => $centre['image']['uri'],
'width' => $centre['image']['width'],
'height' => $centre['image']['height'],
);
$img = theme('image_style', $variables );