Commit b4dec3f4 authored by Daniel Pepin's avatar Daniel Pepin
Browse files

Issue #2536510: Redirect subscriber for tags/collections taxonomy terms.

parent bd2337fe
Loading
Loading
Loading
Loading
+4 −0
Changes for shopify.services.yml: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -3,3 +3,7 @@ services:
    class: Drupal\shopify\Event\ShopifyWebhookSubscriber
    tags:
      - {name: event_subscriber}
  shopify.term_redirect_subscriber:
    class: Drupal\shopify\Event\ShopifyTermRedirectSubscriber
    tags:
      - {name: event_subscriber}
 No newline at end of file
+49 −0
Changes for src/Event/ShopifyTermRedirectSubscriber.php: 49 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php
/**
 * @file
 * Contains taxonomy term redirect subscriber functionality.
 */
namespace Drupal\shopify\Event;

use Drupal\shopify\Entity\ShopifyProduct;
use Drupal\taxonomy\Entity\Term;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * Class ShopifyWebhookSubscriber
 *
 * Provides the webhook subscriber functionality.
 */
class ShopifyTermRedirectSubscriber implements EventSubscriberInterface {

  /**
   * Redirects shopify tag/collection taxonomy terms to the right page.
   *
   * @todo: Not sure this is the best way of doing things.
   */
  public function checkForRedirection(GetResponseEvent $event) {
    if ($term = $event->getRequest()->get('taxonomy_term')) {
      if ($term instanceof Term && $term->bundle() == ShopifyProduct::SHOPIFY_TAGS_VID) {
        $event->setResponse(new RedirectResponse('/' . shopify_store_url() . '/tag/' . $term->id()));
      }
    }
    if ($term = $event->getRequest()->get('taxonomy_term')) {
      if ($term instanceof Term && $term->bundle() == ShopifyProduct::SHOPIFY_COLLECTIONS_VID) {
        $event->setResponse(new RedirectResponse('/' . shopify_store_url() . '/collection/' . $term->id()));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array('checkForRedirection');
    return $events;
  }


}