You are here

protected function ProductVariation::generateTitle in Commerce Core 8.2

Generates the variation title based on attribute values.

Return value

string The generated value.

2 calls to ProductVariation::generateTitle()
ProductVariation::getOrderItemTitle in modules/product/src/Entity/ProductVariation.php
Gets the purchasable entity's order item title.
ProductVariation::preSave in modules/product/src/Entity/ProductVariation.php
Acts on an entity before the presave hook is invoked.

File

modules/product/src/Entity/ProductVariation.php, line 421

Class

ProductVariation
Defines the product variation entity class.

Namespace

Drupal\commerce_product\Entity

Code

protected function generateTitle() {
  if (!$this
    ->getProductId()) {

    // Title generation is not possible before the parent product is known.
    return '';
  }
  $product_title = $this
    ->getProduct()
    ->getTitle();
  if ($attribute_values = $this
    ->getAttributeValues()) {
    $attribute_labels = EntityHelper::extractLabels($attribute_values);
    $title = $product_title . ' - ' . implode(', ', $attribute_labels);
  }
  else {

    // When there are no attribute fields, there's only one variation.
    $title = $product_title;
  }
  return $title;
}