You are here

commerce_add_to_wishlist_link.module in Commerce Add To Cart Link 8

Same filename and directory in other branches
  1. 2.x modules/wishlist/commerce_add_to_wishlist_link.module

Hook implementations of commerce_add_to_wishlist_link module.

File

modules/wishlist/commerce_add_to_wishlist_link.module
View source
<?php

/**
 * @file
 * Hook implementations of commerce_add_to_wishlist_link module.
 */
use Drupal\commerce_add_to_wishlist_link\AddToWishlistLink;
use Drupal\commerce_product\Entity\ProductType;
use Drupal\commerce_product\Entity\ProductVariationType;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;

/**
 * Implements hook_entity_extra_field_info().
 */
function commerce_add_to_wishlist_link_entity_extra_field_info() {
  $fields = [];

  // First, add a pseudo field to product variations.

  /** @var \Drupal\commerce_product\Entity\ProductVariationTypeInterface $product_variation_type */
  foreach (ProductVariationType::loadMultiple() as $product_variation_type) {
    $fields['commerce_product_variation'][$product_variation_type
      ->id()]['display']['add_to_wishlist_link'] = [
      'label' => t('Add to wishlist link'),
      'description' => t('Displays an add to wishlist link.'),
      'weight' => 99,
      // We hide the field by default, as there are in many cases more view
      // modes to hide than to show (e.g. "cart", "summary").
      'visible' => FALSE,
    ];
  }

  // For simplicity, add a field to products too (targeting default variation).

  /** @var \Drupal\commerce_product\Entity\ProductTypeInterface $product_type */
  foreach (ProductType::loadMultiple() as $product_type) {
    $fields['commerce_product'][$product_type
      ->id()]['display']['add_to_wishlist_link'] = [
      'label' => t('Add to wishlist link'),
      'description' => t('Displays an add to wishlist link for the default variation of the product.'),
      'weight' => 99,
      // We hide the field by default.
      'visible' => FALSE,
    ];
  }
  return $fields;
}

/**
 * Implements hook_theme().
 */
function commerce_add_to_wishlist_link_theme() {
  return [
    'commerce_add_to_wishlist_link' => [
      'variables' => [
        'url' => NULL,
        'product_variation' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_theme_suggestions_commerce_add_to_wishlist_link().
 */
function commerce_add_to_wishlist_link_theme_suggestions_commerce_add_to_wishlist_link(array $variables) {
  $original = $variables['theme_hook_original'];
  $variation = $variables['product_variation'];
  $suggestions = [];
  $suggestions[] = $original . '__' . $variation
    ->bundle();
  $suggestions[] = $original . '__' . $variation
    ->id();
  return $suggestions;
}

/**
 * Implements hook_ENTITY_TYPE_view() for commerce_product.
 */
function commerce_add_to_wishlist_link_commerce_product_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {

  /** @var \Drupal\commerce_product\Entity\ProductInterface $entity */
  if ($display
    ->getComponent('add_to_wishlist_link')) {

    /** @var \Drupal\commerce_add_to_cart_link\CartLinkTokenInterface $cart_link_token_service */
    $cart_link_token_service = \Drupal::service('commerce_add_to_cart_link.token');
    $variation = $entity
      ->getDefaultVariation();
    if (empty($variation)) {

      // Avoid errors, if the product has no (active) variations.
      return;
    }
    $build['add_to_wishlist_link'] = (new AddToWishlistLink($variation))
      ->build();
  }
}

/**
 * Implements hook_ENTITY_TYPE_view() for commerce_product_variation.
 */
function commerce_add_to_wishlist_link_commerce_product_variation_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {

  /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $entity */
  if ($display
    ->getComponent('add_to_wishlist_link')) {
    $build['add_to_wishlist_link'] = (new AddToWishlistLink($entity))
      ->build();
  }
}