• Drupal 8: Change Commerce Product Variation URL to SKU

    To pull this off, we need to write a custom module. Let's name our module commerce_variation_sku. In our commerce_variation_sku.module file, we need implement the hook hook_entity_type_build to change the entity class and entity storage class to our custom classes. /** * Implements hook_entity_type_build(). */ function commerce_variation_sku_entity_type_build(array &$entity_types) { if (isset($entity_types['commerce_product_variation'])) { $entity_types['commerce_product_variation']->setClass('Drupal\commerce_variation_sku\Entity\ProductVariation'); $entity_types['commerce_product_variation']->setStorageClass('Drupal\commerce_variation_sku\ProductVariationStorage'); } } Next, we create our custom entity class that will extend the original entity class. <?php namespace Drupal\commerce_variation_sku\Entity; use Drupal\Core\Url; use Drupal\commerce_product\Entity\ProductVariation as ProductVariationBase; class ProductVariation extends ProductVariationBase { /** * {@inheritdoc} */ public function toUrl($rel = 'canonical', array $options = []) { // Product variation URLs depend on the parent product. if (!$this->getProductId()) { // RouteNotFoundException tells EntityBase::uriRelationships() // to skip this product variation's link relationships. throw new RouteNotFoundException(); } // StringFormatter assumes 'revision' is always a valid link template. if (in_array($rel, ['canonical', 'revision'])) { $route_name = 'entity.commerce_product.canonical'; $route_parameters = [ 'commerce_product' => $this->getProductId(), ]; $options += [ 'query' => [ 'sku' => $this->getSku(), ], 'entity_type' => 'commerce_product', 'entity' => $this->getProduct(), // Display links by default based on the current language. 'language' => $this->language(), ]; return new Url($route_name, $route_parameters, $options); } else { return parent::toUrl($rel, $options); } } }   Then we create our custom entity storage class that again extends the original entity storage class. <?php namespace Drupal\commerce_variation_sku; use Drupal\commerce_product\Entity\ProductInterface; use Drupal\commerce_product\ProductVariationStorage as ProductVariationStorageBase; /** * Defines the product variation storage. */ class ProductVariationStorage extends ProductVariationStorageBase { /** * {@inheritdoc} */ public function loadFromContext(ProductInterface $product) { $current_request = $this->requestStack->getCurrentRequest(); if ($sku = $current_request->query->get('sku')) { $variation = $this->loadBySku($sku); if (in_array($variation->id(), $product->getVariationIds())) { /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $variation */ if ($variation->isPublished() && $variation->access('view')) { return $variation; } } } return $product->getDefaultVariation(); } } Last step is to clear your cache and you're good to go šŸ˜€ 
  • drupal 8

    Drupal 8: Redirect to page from Event Subscriber with destination query

    Create your Event Subscriber Create your module's services yml file and declare the event subscriber services: mymodule.autologin: class: Drupal\mymodule\EventSubscriber\AutoLogin tags: - {name: event_subscriber} Save this as inside mymodule folder Create the actual class used from above code <?php namespace Drupal\mymodule\EventSubscriber; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class AutoLogin implements EventSubscriberInterface { public function redirectToLogin() { // PUT YOUR LOGIC HERE } /** * {@inheritdoc} */ public static function getSubscribedEvents() { $events[KernelEvents::REQUEST][] = ['redirectToLogin']; return $events; } } Save this as AutoLogin.php inside mymodule/src/EventSubscriber folder   Redirect using URL::fromRoute with destination query $current_path = \Drupal::service('path.current')->getPath(); $url = \Drupal\Core\Url::fromRoute('simplesamlphp_auth.saml_login', [], ['query' => ['destination' => $current_path, 'absolute' => TRUE]]); $response = new RedirectResponse($url->toString(), 302); $event->setResponse($response); Put the above code in the AutoLogin::redirectLogin(). This code will get the current path and redirect to saml login page to auto login, then if the login is successful, it will redirect to the original path that we are trying to access.  
  • Using Drupal 8 Structure Sync Module

    Structure Sync module is a great tool that helps you import/export your site's contents like Taxonomy, Menu and Blocks. Installation Download the latest version from or by using composer: composer require drupal/structure_sync Then go to Extend and install Structure Sync module or by drush: vendor/bin/drush en structure_sync --uri=default   Exporting Go to Structure > Structure sync. You will see Taxonomies, Custom blocks and Menu links tabs. Choose one that you want to export. Select the item/items you want to export and click the Export button. You should see a success message that tells that you have successfully exported the items. Go to Configuration > Configuration synchronization > Export to get the configuration sync file. This file will include the structure sync export files.   Importing Go to Configuration > Configuration synchronization > Import and upload the configuration sync file. (You can skip this step if your configuration sync file is committed on your repo and you have your sync folder fully configured) Go to Configuration > Configuration synchronization > Synchronize and click the Import all button to import everything from the config sync file. After the sync is finished, go to Structure > Structure sync. You will see Taxonomies, Custom blocks and Menu links tabs. Choose one that you want to import. Click Import button accordingly.  
  • drupal 8

    I'm now running in Drupal 8!

    After years of waiting and contemplating, I finally jumped in.  I still think Drupal 8 is lacking a lot of things but I think we are almost there. I even have created some simple modules for Drupal 8 to play around with it. Hopefully more and more modules will become more stable especially the important ones. I spent a lot of my time using Drupal 7 and I still think that Drupal 7 is still a good CMS but I think it's time to move on to the new one. I really like that now we can run Drupal with Composer and of course, it's running on top of Symfony framework. There's a huge learning curve but I guess it will pay off. I hope to have a good relationship with Drupal 8 just like what I had with Drupal 7. ?