diff --git a/modules/product/src/Entity/ProductAttribute.php b/modules/product/src/Entity/ProductAttribute.php index 77db4ad0e2..bb007b7e47 100644 --- a/modules/product/src/Entity/ProductAttribute.php +++ b/modules/product/src/Entity/ProductAttribute.php @@ -37,11 +37,13 @@ * entity_keys = { * "id" = "id", * "label" = "label", + * "elementLabel" = "elementLabel", * "uuid" = "uuid" * }, * config_export = { * "id", * "label", + * "elementLabel", * "elementType" * }, * links = { @@ -68,6 +70,13 @@ class ProductAttribute extends ConfigEntityBundleBase implements ProductAttribut */ protected $label; + /** + * The customer facing attribute label. + * + * @var string + */ + protected $elementLabel; + /** * The attribute element type. * @@ -109,4 +118,11 @@ public static function postDelete(EntityStorageInterface $storage, array $entiti $value_storage->delete($values); } + /** + * {@inheritdoc} + */ + public function getElementLabel() { + return $this->elementLabel; + } + } diff --git a/modules/product/src/Entity/ProductAttributeInterface.php b/modules/product/src/Entity/ProductAttributeInterface.php index 28b8f043e6..7488f71034 100644 --- a/modules/product/src/Entity/ProductAttributeInterface.php +++ b/modules/product/src/Entity/ProductAttributeInterface.php @@ -25,4 +25,12 @@ public function getValues(); */ public function getElementType(); + /** + * Gets the attribute customer facing label. + * + * @return string + * The attribute element label. + */ + public function getElementLabel(); + } diff --git a/modules/product/src/Form/ProductAttributeForm.php b/modules/product/src/Form/ProductAttributeForm.php index c9da3da8fe..8c8665da3f 100644 --- a/modules/product/src/Form/ProductAttributeForm.php +++ b/modules/product/src/Form/ProductAttributeForm.php @@ -52,6 +52,14 @@ public function form(array $form, FormStateInterface $form_state) { '#maxlength' => 255, '#default_value' => $attribute->label(), '#required' => TRUE, + '#description' => $this->t('The admin facing attribute name which appears on administrative pages (e.g.- My T-shirt Colors).'), + ]; + $form['elementLabel'] = [ + '#type' => 'textfield', + '#title' => $this->t('Label'), + '#maxlength' => 255, + '#default_value' => $attribute->getElementLabel(), + '#description' => $this->t('The customer facing label which appears on any Add to Cart form (e.g.- Color). Leave empty to use admin facing name for this purpose. Note that this label can be overridden for each attribute reference field saved on a variation type.'), ]; $form['id'] = [ '#type' => 'machine_name', diff --git a/modules/product/src/Form/ProductVariationTypeForm.php b/modules/product/src/Form/ProductVariationTypeForm.php index 0d0344ae0c..ba66c70ae9 100644 --- a/modules/product/src/Form/ProductVariationTypeForm.php +++ b/modules/product/src/Form/ProductVariationTypeForm.php @@ -92,14 +92,35 @@ public function form(array $form, FormStateInterface $form_state) { ]; } - $used_attributes = []; + $used_attributes = $attribute_field = []; + $path = $definitions = ''; if (!$variation_type->isNew()) { $attribute_map = $this->attributeFieldManager->getFieldMap($variation_type->id()); $used_attributes = array_column($attribute_map, 'attribute_id'); + if (!empty($used_attributes)) { + $attribute_field = array_combine($used_attributes, array_column($attribute_map, 'field_name')); + $bundle = $variation_type->getEntityType()->getBundleOf(); + $path = $this->getDestinationArray()['destination'] . "/fields/{$bundle}.{$variation_type->id()}."; + $definitions = $this->attributeFieldManager->getFieldDefinitions($variation_type->id()); + } } /** @var \Drupal\commerce_product\Entity\ProductAttributeInterface[] $attributes */ $attributes = $this->entityTypeManager->getStorage('commerce_product_attribute')->loadMultiple(); - $attribute_options = EntityHelper::extractLabels($attributes); + $attribute_options = array_map(function ($attribute) use ($path, $attribute_field, $definitions) { + /** @var \Drupal\commerce_product\Entity\ProductAttributeInterface $attribute */ + $id = $attribute->id(); + $url = $attribute->url(); + $label = $attribute->label(); + + if (isset($attribute_field[$id])) { + $label = $definitions[$attribute_field[$id]]->label(); + $label = $this->t('%label', ['%label' => $label, ':href' => $path . $attribute_field[$id]]); + + return $label . ' => ' . $this->t('@id', [':href' => $url, '@id' => $id]); + } + + return $this->t('%label => @id', ['%label' => $label, ':href' => $url, '@id' => $id]); + }, $attributes); $form['original_attributes'] = [ '#type' => 'value', diff --git a/modules/product/src/ProductAttributeFieldManager.php b/modules/product/src/ProductAttributeFieldManager.php index 0aeac1f9c3..76219b0f0f 100644 --- a/modules/product/src/ProductAttributeFieldManager.php +++ b/modules/product/src/ProductAttributeFieldManager.php @@ -183,10 +183,11 @@ public function createField(ProductAttributeInterface $attribute, $variation_typ $field_storage->save(); } if (empty($field)) { + $label = $attribute->getElementLabel(); $field = FieldConfig::create([ 'field_storage' => $field_storage, 'bundle' => $variation_type_id, - 'label' => $attribute->label(), + 'label' => empty($label) ? $attribute->label() : $label, 'required' => TRUE, 'settings' => [ 'handler' => 'default', diff --git a/modules/product/src/ProductAttributeListBuilder.php b/modules/product/src/ProductAttributeListBuilder.php index 327afa3532..9702373ecc 100644 --- a/modules/product/src/ProductAttributeListBuilder.php +++ b/modules/product/src/ProductAttributeListBuilder.php @@ -4,6 +4,7 @@ use Drupal\Core\Config\Entity\ConfigEntityListBuilder; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Render\Markup; /** * Defines the list builder for product attributes. @@ -14,7 +15,8 @@ class ProductAttributeListBuilder extends ConfigEntityListBuilder { * {@inheritdoc} */ public function buildHeader() { - $header['label'] = $this->t('Attribute name'); + $header['label_id'] = $this->t('Attribute name => ID'); + $header['values'] = $this->t('Values'); return $header + parent::buildHeader(); } @@ -22,7 +24,27 @@ public function buildHeader() { * {@inheritdoc} */ public function buildRow(EntityInterface $entity) { - $row['label'] = $entity->label(); + $values = []; + $i = 0; + foreach ($entity->getValues() as $attribute_value) { + if ($i < 100) { + $value = $attribute_value->getName(); + $values[] = strlen($value) > 10 ? substr_replace($value, '...', 7) : $value; + } + $i++; + } + if (($count = count($values)) && $i > $count) { + $more = Markup::create(' (' . ($i - $count) . ' ' . $this->t('more values') . ' ...)'); + $values = $this->t('@values @more', ['@values' => implode(', ', $values), '@more' => $more]); + } + else { + $values = implode(', ', $values); + } + $element_label = $entity->getElementLabel(); + $label = empty($element_label) ? '' : "({$element_label})
"; + $row['label_id'] = Markup::create('' . $entity->label() . '
' . $label . '=> ' . $entity->id()); + $row['values'] = $values; + return $row + parent::buildRow($entity); }