Drupal 8 Cheat Sheet
<?php
// Messenger - (D7 drupal_set_message)
\Drupal::messenger()->addMessage(t('Show this message to the user.'));
// Message Types
\Drupal::messenger()->addMessage($message, 'status');
\Drupal::messenger()->addMessage($message, 'warning');
\Drupal::messenger()->addMessage($message, 'error');
// Another way of displaying error type messages.
\Drupal::messenger()->addError(t('The user account %id does not exist.', ['%id' => $uid]));
// Add messenger to your class for use with $this->messenger
/**
* Gets the messenger.
*
* @return \Drupal\Core\Messenger\MessengerInterface
* The messenger.
*/
public function messenger() {
if (!isset($this->messenger)) {
$this->messenger = \Drupal::messenger();
}
return $this->messenger;
}
// **** working with nodes ******
// Check if an entity is a node.
if ($entity instanceof \Drupal\node\NodeInterface) {
$node = $entity;
}
// or
if ($entity->getEntityTypeId() == 'node') {
$node = $entity;
}
// check the bundle type of a node
$node_type = $entity->getType();
// Load by ID
$node = Node::load($nid);
//Load mathod 2 - https://www.metaltoad.com/blog/drupal-8-entity-api-cheat-sheet
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
$node = $node_storage->load($nid);
// Get a node from a form
$node = $form_state->getFormObject()->getEntity();
$is_new = $node->isNew();
// Get node id.
$nid = $node->id();
// Get node title
$title = $product->getTitle(); // use this
$title = $product->title->value; // although this is the same (but should use the function)
// Get a field value array
$node->get('field_name')->getValue();
$node->field_name->getValue();
// Get the 2nd value of a multi value field
$node->field_name->getValue()[1]['value'];
// Get the 3nd reference ID of a multi value entity reference field
$node->field_name->getValue()[2]['target_id'];
// Count the number of values
count($node->field_name->getValue())
// Get a field value
$node->field_name->Value;
// Single node entity reference field
$nid = $node->get('field_contact')->first()->getValue()['target_id'];
$node = Node::load($nid);
// Cycle a Entity reference fields
foreach ($node->field_po_lines as $reference) {
$entity_id = $reference->target_id;
$entity_storage = \Drupal::entityTypeManager()->getStorage('minirigs_po_line');
$entity = $entity_storage->load($entity_id);
}
// Cycle a Entity reference fields (th long way)
foreach ($node->field_po_lines as $po_line) {
/** @var \Drupal\Core\Entity\Plugin\DataType\EntityReference $entityReference */
$entityReference = $po_line->get('entity');
/** @var \Drupal\Core\Entity\Plugin\DataType\EntityAdapter $entityAdapter */
$entityAdapter = $entityReference->getTarget();
/** @var \Drupal\Core\Entity\EntityInterface $referencedEntity */
$referencedEntity = $entityAdapter->getValue();
}
// Get all the referenced entities in one go
$all__referenced_entities = $node->get('field_po_lines')->referencedEntities();
// Create node - need to include:
use Drupal\node\Entity\Node;
// Create a node mathod 1a - simple
$node = Node::create(['type' => 'article', 'title' => 'Sample article API', 'path' => ['alias' => '/sample-article-api']]);
$node->save();
// Create a node mathod 1b - more details
$data = [
'uid' => $user->id(),
'name' => 'admin',
'type' => 'article',
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'title' => $this->randomMachineName(),
'body' => 'body text',
'multivalue_field' => [ ['value' => 'first'], ['value' => 'second'] ]
'status' => 1,
'created' => '1435019805',
];
$node = Node::create($data);
$node->save();
// Create a node mathod 2 - field by field
$node = Node::create(['type' => 'article']);
$node->set('title', 'Title');
$node->set('field_text', 'My text');
$node->save();
// Populate a multivalue field for a string holding comma delimited values
$value = '1,2,3,4';
$value_array = explode(',', $value);
foreach ($value as $key => $value) {
$value_array[$key] = ['value' => trim($value)];
}
$node->set('multivalue_field', $value_array;
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');
}
// Drupal messenger (from commerce_stock)
\Drupal::messenger()->addMessage(t('Updated the stock.'));
\Drupal::messenger()->addError(t('The maximum quantity for %name that can be ordered is %qty.', [
'%name' => $name,
'%qty' => $stock_level,
]));
}
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();
// Work with products
// Load the product variation.
$variation_storage = \Drupal::service('entity_type.manager')->getStorage('commerce_product_variation');
$product_variation = $variation_storage->load($variation_id);
$variation_id = $product_variation->id();
// Get the product form the variation
$product = $product_variation->getProduct();
$product_id = $product_variation->getProductId();
// Cycle the line items of an order and load each product.
foreach ($order->getItems() as $id => $order_item) {
$purchased_entity = $order_item->getPurchasedEntity();
$pe_price = $purchased_entity->getPrice();
if (!$purchased_entity) {
// Not every order item has a purchased entity.
continue;
}
$name = $purchased_entity->getTitle();
$qty = $order_item->getQuantity();
// Unit price
$unit_price_obj = $order_item->getUnitPrice();
$price_amount = $unit_price_obj->getNumber();
$price_currency = $unit_price_obj->getCurrencyCode();
// Line total price
$line_price_obj = $order_item->getTotalPrice();
}
// Stock
// Check if a product is available.
$availabilityManager = \Drupal::service('commerce.availability_manager');
$availabe = $availabilityManager->check($product_variation, $prod_qty);
// Create a stock transaction
$stockManager = \Drupal::service('commerce_stock.service_manager');
// High level typed transaction using the stock manager.
$stockManager->receiveStock($purchasable_entity, $location_id, $zone, $quantity, $unit_cost, $message);
// Low level un-typed transaction using the stock updater.
$stockManager->getService($purchasable_entity)->getStockUpdater()->createTransaction($purchasable_entity, $location_id, $zone, $quantity, $unit_cost, $transaction_type, $metadata);
// Get Stock level
// Get the Stock service for the PurchasableEntity and use that to get the stock checker.
$stock_service = $stockManager->getService($entity);
$stock_checker = $stock_service->getStockChecker();
// You can then check if it is always in stock:
stock_checker->getIsAlwaysInStock($entity)
// Get the configuration object. Needed to get the locations to check for.
// $stock_config = $stock_service->getConfiguration();
// and finally you can get the stock level:
$stock_level = $stock_checker->getTotalStockLevel(
$entity,
$stock_config->getAvailabilityLocations($context, $entity)
);
// Needs the following use at the top of the file:
// use Drupal\commerce_product\Entity\ProductVariationInterface;
// Check if a purchasable entity is a product variation.
if ($purchasable_entity instanceof ProductVariationInterface) {
// Get the product variation from a line item.
$product_variation = $line_item->getPurchasedEntity();
}
// Get the Cart provider service
$cart_provider = \Drupal::service('commerce_cart.cart_provider');
// Get all the carts.
$all_carts = \Drupal::service('commerce_cart.cart_provider')->getCarts();
// Get the current store.
$store = \Drupal::service('commerce_store.current_store')->getStore();
// Get the cart for the curent store.
$cart = \Drupal::service('commerce_cart.cart_provider')->getCart('default', $store);
// get the store from an order
$order->getStore();
$order->getStoreid();
// Add to cart form
// Get the add to cart entity form.
$entity_form = $form_state->getFormObject();
// Get the line item and $purchased_entity.
$order_item = $entity_form->getEntity();
/** @var \Drupal\commerce\PurchasableEntityInterface $purchased_entity */
$purchased_entity = $order_item->getPurchasedEntity();
// Get the $context from the Add to cart form
/** @var \Drupal\commerce_product\Entity\ProductInterface $product */
$product = $form_state->get('product');
$selected_variation_id = $form_state->get('selected_variation');
if (!empty($selected_variation_id)) {
$selected_variation = \Drupal\commerce_product\Entity\ProductVariation::load($selected_variation_id);
}
else {
$selected_variation = $product->getDefaultVariation();
}
$context = commerce_stock_enforcement_get_context($selected_variation);
// Working with the cart page
// Get the order from the form state.
$order = $form_state->getFormObject()->getEntity();
// Checkout
// Check if the checkout form. need to make sure its not the commerce_checkout_flow_form (used to create new checkout flows)
if (strpos($form_id, "commerce_checkout_flow") !== false && $form_state->getFormObject()->getBaseFormId() == 'commerce_checkout_flow') {
// Get the order from the form
/** @var Drupal\Core\Form\FormInterface $form_object */
$form_object = $form_state->getFormObject();
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = $form_object->getOrder();
// get a field value using getValue(), check it and cycle using the store.
$store = $context->getStore();
$locations = $store->field_available_stock_locations->getValue();
// If we have store locations.
if (!empty($locations)) {
// Load them.
$store_locations = [];
foreach ($locations as $location) {
$store_locations[$location['target_id']] = $location['target_id'];
}
$store_locations = $this->loadMultiple($store_locations);
}
// Check what type is a PurchasableEntity.
$entity_type = $entity->getEntityTypeId(); //example: commerce_product_variation
$entity_bundle = $entity->bundle(); //example: default
// Reading a field with a single value - all the below will return the same value
$external_product_id = $entity->field_external_product_id->getValue()[0]['value'];
$external_product_id = $entity->field_external_product_id->getString();
$external_product_id = $entity->field_external_product_id->value;
$external_product_id = $entity->field_external_product_id[0]->value;
// User data
// Get details from the order
$billing_profile = $order->getBillingProfile();
$email = $this->order->getEmail();
$address = $billing_profile->address->first();
$line_1 = $address->address_line1,
$line_2 = $address->address_line2,
$city = $address->locality,
$company = $address->organization,
$country = $address->country_code,
$first_name = $address->given_name,
$middle_name = $address->additional_name,
$last_name = $address->family_name,
$state = $address->administrative_area,
$post_code = $address->postal_code,
// Get address as an array
$billing_profile->get('address')->first()->toArray())
// Shipping
// Get the shipments entity from the order reference.
$shipments = $order->shipments->referencedEntities();
$shipments = $order->get('shipments')->referencedEntities();
// Get the first shipment entity.
/** @var \Drupal\commerce_shipping\Entity\ShipmentInterface $shipment */
$shipment = reset($shipments);
// Get shipping Profile
$shipping_profile = $shipment->getShippingProfile();
// Get the address (from this point its the same as billing address)
$address = $shipping_profile->address->first();
$line_1 $shipping_profile->get('address')->address_line1;
// Get the address book profile ID.
$address_book_profile_id = $shipping_profile->getData('address_book_profile_id');
$address_book_profile = Profile::load($address_book_profile_id);
$is_default = $address_book_profile->isDefault();
// Shipping Method info.
$shipping_method = $shipment->getShippingMethod();
$shipping_method_label = $shipment->getShippingMethod()->label();
$shipping_method_id = $shipment->getShippingMethodId();
// Shipping service info.
$shipping_service = $shipment->getShippingService();
// Amount.
$shipping_amount_obj = $shipment->getAmount();
$shipping_original_amount_obj = $shipment->getOriginalAmount();
$shipping_amount = $shipping_amount_obj->getNumber();
$shipping_amount_currency =$shipping_amount_obj->getCurrencyCode()
// Other
$package_type = $shipment->getPackageType();
$package_type = $shipment->getPackageType();
$package_type_id = $shipment->getPackageType()->getId();
// Payment
// Payment method.
$payment_method = $order->get('payment_method')->entity;
// Payment method values.
$method_id = $payment_method->get('method_id')->value;
$payment_type = $payment_method->get('type')->value;
$card_type = $payment_method->get('card_type')->value;
// The remote ID returned by the request.
$remote_id = $payment_method->get('remote_id')->value;
// Payment Gateway
$payment_gateway = $payment_method->get('payment_gateway')->referencedEntities()[0];
$payment_gateway_mode = $payment_method->get('payment_gateway_mode')->value;
$payment_gateway_id = $payment_gateway->get('id');
$payment_gateway_configuration = $payment_gateway->get('configuration');
// Payment Gateway API Key
$api_key = $payment_gateway_configuration['api_key'];
// Price adjustments
// full documentation: https://docs.drupalcommerce.org/commerce2/developer-guide/pricing/adjustments
/** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
$adjustment_amount = $order_item->getUnitPrice()->multiply(0.1);
$order_item->addAdjustment(new Adjustment([
'type' => 'custom',
'label' => 'Plus 10%',
'amount' => $adjustment_amount,
'percentage' => '0.1',
'included' => FALSE,
'locked' => TRUE,
]));
$order_item->addAdjustment(new Adjustment([
'type' => 'fee',
'label' => '$10 fee',
'amount' => new Price('10.00', 'USD'),
]);
// Read pricing information.
$order_item->getUnitPrice();
$order_item->getTotalPrice();
$order_item->getAdjustedTotalPrice();
$order_item->getAdjustedTotalPrice(['custom']);
$order_item->getAdjustedTotalPrice(['fee']);
// Order adjustments
$order->addAdjustment(new Adjustment([
'type' => 'fee',
'label' => 'Handling fee',
'amount' => new Price('10.00', 'USD'),
'locked' => TRUE,
]));
// Read pricing information.
$order->getTotalPrice();
$order->getSubtotalPrice();
$order->getAdjustments();
$order->getAdjustments(['fee'])
// Split a $10 discount across all the lines of an order.
$splitter = \Drupal::getContainer()->get('commerce_order.price_splitter');
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$amounts = $splitter->split($order, new Price('10.00', 'USD'));
foreach ($order->getItems() as $order_item) {
if (isset($amounts[$order_item->id()])) {
$order_item->addAdjustment(new Adjustment([
'type' => 'custom',
'label' => $this->t('Special'),
'amount' => $amounts[$order_item->id()]->multiply('-1'),
]));
}
}
// Calculate the total unit discount for a line:
$purchased_entity = $order_item->getPurchasedEntity();
$purchased_entity_price = $purchased_entity->getPrice()->getNumber();
$line_adjustmented_unit_price = $order_item->getAdjustedUnitPrice()->getNumber();
$unit_discounbt = $purchased_entity_price - $line_adjustmented_unit_price;
Products get remaining time
function _kf_products_get_remaining_time($expiry_date) {
$remaining = '';
$diff = $expiry_date - REQUEST_TIME;
$days = floor($diff / 86400);
$hours = floor(($diff - ($days * 86400)) / 3600);
$minutes = floor(($diff - ($days * 86400) - ($hours * 3600)) / 60);
$seconds = floor($diff - ($days * 86400) - ($hours * 3600) - ($minutes * 60));
if ($diff > 86400) {
$remaining .= $days . ' d ';
}
if ($hours > 0) {
$remaining .= $hours . ' h ';
}
if ($minutes > 0) {
$remaining .= $minutes . ' m ';
}
if ($diff < 86400) {
$remaining .= $seconds . ' s ';
}
return $remaining;
}
Bình luận (0)
Add Comment