You are here

function commerce_add_to_cart_link_entity_extra_field_info in Commerce Add To Cart Link 8

Same name and namespace in other branches
  1. 2.x commerce_add_to_cart_link.module \commerce_add_to_cart_link_entity_extra_field_info()

Implements hook_entity_extra_field_info().

File

./commerce_add_to_cart_link.module, line 17
Hook implementations of commerce_add_to_cart_link module.

Code

function commerce_add_to_cart_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_cart_link'] = [
      'label' => t('Add to cart link'),
      'description' => t('Displays an add to cart 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_cart_link'] = [
      'label' => t('Add to cart link'),
      'description' => t('Displays an add to cart link for the default variation of the product.'),
      'weight' => 99,
      // We hide the field by default.
      'visible' => FALSE,
    ];
  }
  return $fields;
}