Skip to content

Instantly share code, notes, and snippets.

View davpsh's full-sized avatar
😒
I may be slow to respond.

David davpsh

😒
I may be slow to respond.
View GitHub Profile
@morsok
morsok / referer.php
Last active February 28, 2022 10:22
Drupal 8 : Do something based on the referer if it's your website or another.
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\Request;
function my_function() {
$previousUrl = \Drupal::request()->server->get('HTTP_REFERER');
$fake_request = Request::create($previousUrl);
$url_object = \Drupal::service('path.validator')->getUrlIfValid($fake_request->getRequestUri());
if ($url_object) {
$route_name = $url_object->getRouteName();
// Do something with $route_name;
@luksak
luksak / CartEventSubscriber.php
Last active October 19, 2017 18:14
Display message if order total is below a certain amount such as to get free shipping after adding products to the cart and when viewing the cart page.
<?php
namespace Drupal\MODULE\EventSubscriber;
use Drupal\commerce_order\Entity\Order;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\commerce_cart\Event\CartEntityAddEvent;
use Drupal\commerce_cart\Event\CartEvents;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;