From 25e7fb026015ce020efcf067201440c92c415a89 Mon Sep 17 00:00:00 2001 From: Ben McClure Date: Tue, 4 Apr 2017 00:38:28 -0500 Subject: [PATCH 1/2] by bmcclure: Product variation title generate event --- .../product/src/Entity/ProductVariation.php | 7 ++ modules/product/src/Event/ProductEvents.php | 9 +++ .../ProductVariationTitleGenerateEvent.php | 72 +++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 modules/product/src/Event/ProductVariationTitleGenerateEvent.php diff --git a/modules/product/src/Entity/ProductVariation.php b/modules/product/src/Entity/ProductVariation.php index 2ff85fd40b..e8b71a20e4 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,11 @@ 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); + 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; + } + +} From 3d3c4fd80084e8614c4428736bbcde2c718275f0 Mon Sep 17 00:00:00 2001 From: Ben McClure Date: Tue, 4 Apr 2017 01:05:54 -0500 Subject: [PATCH 2/2] by bmcclure: Set the modified title --- modules/product/src/Entity/ProductVariation.php | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/product/src/Entity/ProductVariation.php b/modules/product/src/Entity/ProductVariation.php index e8b71a20e4..a60401995c 100644 --- a/modules/product/src/Entity/ProductVariation.php +++ b/modules/product/src/Entity/ProductVariation.php @@ -371,6 +371,7 @@ protected function generateTitle() { $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; }