ProductVariationInlineForm.php in Commerce Core 8.2
File
modules/product/src/Form/ProductVariationInlineForm.php
View source
<?php
namespace Drupal\commerce_product\Form;
use Drupal\commerce\EntityHelper;
use Drupal\Core\Entity\EntityInterface;
use Drupal\inline_entity_form\Form\EntityInlineForm;
class ProductVariationInlineForm extends EntityInlineForm {
protected $variationTypes;
public function getEntityTypeLabels() {
$labels = [
'singular' => $this
->t('variation'),
'plural' => $this
->t('variations'),
];
return $labels;
}
public function getTableFields($bundles) {
$fields = parent::getTableFields($bundles);
$fields['label']['label'] = $this
->t('Title');
$fields['price'] = [
'type' => 'field',
'label' => $this
->t('Price'),
'weight' => 10,
];
$fields['status'] = [
'type' => 'field',
'label' => $this
->t('Status'),
'weight' => 100,
'display_options' => [
'settings' => [
'format' => 'custom',
'format_custom_true' => $this
->t('Published'),
'format_custom_false' => $this
->t('Unpublished'),
],
],
];
return $fields;
}
public function getEntityLabel(EntityInterface $entity) {
$variation_type = $this
->loadVariationType($entity
->bundle());
if (!$variation_type
->shouldGenerateTitle()) {
return $entity
->label();
}
if ($attribute_values = $entity
->getAttributeValues()) {
$attribute_labels = EntityHelper::extractLabels($attribute_values);
$label = implode(', ', $attribute_labels);
}
else {
$label = $this
->t('N/A');
}
return $label;
}
protected function loadVariationType($variation_type_id) {
if (!isset($this->variationTypes[$variation_type_id])) {
$storage = $this->entityTypeManager
->getStorage('commerce_product_variation_type');
$this->variationTypes[$variation_type_id] = $storage
->load($variation_type_id);
}
return $this->variationTypes[$variation_type_id];
}
}