diff --git a/modules/product/src/Entity/ProductVariation.php b/modules/product/src/Entity/ProductVariation.php index 2ff85fd40b..a60401995c 100644 --- a/modules/product/src/Entity/ProductVariation.php +++ b/modules/product/src/Entity/ProductVariation.php @@ -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; @@ -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; } diff --git a/modules/product/src/Event/ProductEvents.php b/modules/product/src/Event/ProductEvents.php index a2a2ccb9f1..47aa154cab 100644 --- a/modules/product/src/Event/ProductEvents.php +++ b/modules/product/src/Event/ProductEvents.php @@ -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. * diff --git a/modules/product/src/Event/ProductVariationTitleGenerateEvent.php b/modules/product/src/Event/ProductVariationTitleGenerateEvent.php new file mode 100644 index 0000000000..929edc22a6 --- /dev/null +++ b/modules/product/src/Event/ProductVariationTitleGenerateEvent.php @@ -0,0 +1,72 @@ +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; + } + +}