Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variation title generate event #707

Open
wants to merge 5 commits into
base: 8.x-2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}

}