Skip to content

Instantly share code, notes, and snippets.

View clrockwell's full-sized avatar
🏠
Working from home

Chris Rockwell clrockwell

🏠
Working from home
  • Vanderbilt University Medical Center
View GitHub Profile
{% set has_bg_colour = paragraph.field_background_colour.value %}
{#
We are going to check here if the paragraph bundle has a background colour
set. If so, we apply that background colour as a class on that paragraph bundle.
Then, if it has a background colour, we decide that we want to give it some
padding, so we also add a .has-bg class to it. If individual paragraph bundles
need to override that, we can use removceClass on their paragraph.html.twig files.
#}
<?php
/**
* snippets for dealing with custom entities
* Use dependency injection in actual code
*/
function d8_custom_entity_new_entity_form() {
$entity = \Drupal::entityTypeManager()
->getStorage('the_entity_type')
->create()
->setOwnerId(\Drupal::currentUser()->id());
<?php
namespace Drupal\require_login\EventSubscriber;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Path\PathValidator;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
@clrockwell
clrockwell / CreateLicense.php
Last active September 2, 2017 19:58
A service for creating licenses programmatically
<?php
namespace Drupal\free_trial;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\user\RoleInterface;
/**
* Class CreateLicense.
*/
class CreateLicense implements CreateLicenseInterface {
@clrockwell
clrockwell / CommerceLicenseCodeSamples.php
Created September 2, 2017 20:42
Some Commerce License code samples
<?php
// Load all licenses for current user that grant a specific license
$existing = $this->entityTypeManager->getStorage('commerce_license')
->loadByProperties([
'license_role' => 'machine_name_of_role',
'uid' => \Drupal::current_user()->id()
]);
@clrockwell
clrockwell / your_form_name.module
Created September 9, 2017 16:15
Drupal 8: altering a @FormElement - update password_confirm element to require 6 characters
<?php
/**
* Implements hook_element_info_alter().
*/
function scholarrx_user_registration_element_info_alter(array &$info) {
$info['password_confirm']['#process'][] = 'scholarrx_process_password_confirm';
}
function scholarrx_process_password_confirm(&$element, FormStateInterface $form_state, &$complete_form) {
@clrockwell
clrockwell / LicenseProrated.php
Last active June 27, 2018 20:50
Provide a prorated discount for license roles; useful in upsells when someone already has purchased role, they will receive a discount equal to the amount of time left.
<?php
namespace Drupal\commerce_promotion_prorated\Plugin\Commerce\PromotionOffer;
use Drupal\commerce_license\Entity\License;
use Drupal\commerce_order\Adjustment;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_order\Entity\OrderItem;
use Drupal\commerce_order\Entity\OrderItemInterface;
use Drupal\commerce_price\Price;
@clrockwell
clrockwell / RefundPayment.php
Created September 21, 2017 13:43
A sample endpoint for refunding a payment in Commerce 2.x
<?php
namespace Drupal\module\Plugin\rest\resource;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_payment\Entity\Payment;
use Drupal\commerce_payment\Exception\PaymentGatewayException;
use Drupal\commerce_payment\PaymentGatewayManager;
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\PaymentGatewayBase;
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\SupportsRefundsInterface;
@clrockwell
clrockwell / CreatePluginInstance.php
Created October 1, 2017 14:57
Example of creating a plugin instance with the default configuration
<?php
// using PayPal EC as example
/** @var PaymentGatewayManager $plugin */
$plugin = \Drupal::service('plugin.manager.commerce_payment_gateway');
// Get the id of config from `drupal debug:config | grep paypal`
/** @var Config $config */
$config = \Drupal::service('config.factory')
<?php
namespace Drupal\commerce_form_customizations\Plugin\Commerce\ReportType;
use Drupal\commerce_order\Adjustment;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_order\Entity\OrderItemInterface;
use Drupal\commerce_price\Price;
use Drupal\commerce_promotion\Entity\CouponInterface;