Skip to content
8 changes: 8 additions & 0 deletions modules/product/src/Entity/ProductVariation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Drupal\commerce\EntityHelper;
use Drupal\commerce_price\Price;
use Drupal\commerce_product\Event\ProductEvents;
use Drupal\commerce_product\Event\ProductVariationTitleGenerateEvent;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
Expand Down Expand Up @@ -365,6 +367,12 @@ protected function generateTitle() {
$title = $product_title;
}

/** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher */
$event_dispatcher = \Drupal::service('event_dispatcher');
$event = new ProductVariationTitleGenerateEvent($title, $this);
$event_dispatcher->dispatch(ProductEvents::PRODUCT_VARIATION_TITLE_GENERATE, $event);
$title = $event->getTitle();

return $title;
}

Expand Down
9 changes: 9 additions & 0 deletions modules/product/src/Event/ProductEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ final class ProductEvents {
*/
const PRODUCT_VARIATION_TRANSLATION_DELETE = 'commerce_product.commerce_product_variation.translation_delete';

/**
* Name of the event fired after generating a product variation title.
*
* @Event
*
* @see \Drupal\commerce_product\Event\ProductVariationTitleGenerateEvent
*/
const PRODUCT_VARIATION_TITLE_GENERATE = 'commerce_product.commerce_product_variation.title_generate';

/**
* Name of the event fired when filtering variations.
*
Expand Down
72 changes: 72 additions & 0 deletions modules/product/src/Event/ProductVariationTitleGenerateEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Drupal\commerce_product\Event;

use Drupal\commerce_product\Entity\ProductVariationInterface;
use Symfony\Component\EventDispatcher\Event;

/**
* Defines the product variation title generate event.
*
* @see \Drupal\commerce_product\Event\ProductEvents
*/
class ProductVariationTitleGenerateEvent extends Event {

/**
* The generated title.
*
* @var string
*/
protected $title;

/**
* The product variation.
*
* @var \Drupal\commerce_product\Entity\ProductVariationInterface
*/
protected $productVariation;

/**
* Constructs a new ProductVariationEvent.
*
* @param string $title
* The generated title.
* @param \Drupal\commerce_product\Entity\ProductVariationInterface $product_variation
* The product variation.
*/
public function __construct($title, ProductVariationInterface $product_variation) {
$this->title = $title;
$this->productVariation = $product_variation;
}

/**
* Gets the generated title.
*
* @return string
* The generated title.
*/
public function getTitle() {
return $this->title;
}

/**
* Updates the generated title.
*
* @param string $title
* The generated title
*/
public function setTitle($title) {
$this->title = $title;
}

/**
* Gets the product variation.
*
* @return \Drupal\commerce_product\Entity\ProductVariationInterface
* The product variation.
*/
public function getProductVariation() {
return $this->productVariation;
}

}