ProductAttributeValue.php 4.35 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php

namespace Drupal\commerce_product\Entity;

use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;

/**
 * Defines the product attribute value entity class.
 *
 * @ContentEntityType(
 *   id = "commerce_product_attribute_value",
 *   label = @Translation("Product attribute value"),
16
17
 *   label_singular = @Translation("product attribute value"),
 *   label_plural = @Translation("product attribute values"),
18
19
20
21
 *   label_count = @PluralTranslation(
 *     singular = "@count product attribute value",
 *     plural = "@count product attribute values",
 *   ),
22
 *   bundle_label = @Translation("Product attribute"),
23
24
 *   handlers = {
 *     "event" = "Drupal\commerce_product\Event\ProductAttributeValueEvent",
25
 *     "storage" = "Drupal\commerce_product\ProductAttributeValueStorage",
bojanz's avatar
bojanz committed
26
 *     "access" = "Drupal\commerce_product\ProductAttributeValueAccessControlHandler",
27
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
28
 *     "views_data" = "Drupal\commerce\CommerceEntityViewsData",
29
30
 *     "translation" = "Drupal\content_translation\ContentTranslationHandler"
 *   },
31
 *   admin_permission = "administer commerce_product_attribute",
32
 *   translatable = TRUE,
33
 *   content_translation_ui_skip = TRUE,
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 *   base_table = "commerce_product_attribute_value",
 *   data_table = "commerce_product_attribute_value_field_data",
 *   entity_keys = {
 *     "id" = "attribute_value_id",
 *     "bundle" = "attribute",
 *     "label" = "name",
 *     "langcode" = "langcode",
 *     "uuid" = "uuid",
 *   },
 *   bundle_entity_type = "commerce_product_attribute",
 *   field_ui_base_route = "entity.commerce_product_attribute.edit_form",
 * )
 */
class ProductAttributeValue extends ContentEntityBase implements ProductAttributeValueInterface {

bojanz's avatar
bojanz committed
49
  use EntityChangedTrait;
50

51
52
53
54
55
56
57
58
  /**
   * {@inheritdoc}
   */
  public function getAttribute() {
    $storage = $this->entityTypeManager()->getStorage('commerce_product_attribute');
    return $storage->load($this->bundle());
  }

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  /**
   * {@inheritdoc}
   */
  public function getAttributeId() {
    return $this->bundle();
  }

  /**
   * {@inheritdoc}
   */
  public function getName() {
    return $this->get('name')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setName($name) {
    $this->set('name', $name);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getWeight() {
    return $this->get('weight')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setWeight($weight) {
    $this->set('weight', $weight);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getCreatedTime() {
    return $this->get('created')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setCreatedTime($timestamp) {
    $this->set('created', $timestamp);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
bojanz's avatar
bojanz committed
115
    $fields = parent::baseFieldDefinitions($entity_type);
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
    // Override the label for the generated bundle field.
    $fields['attribute']->setLabel(t('Attribute'));

    $fields['name'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Name'))
      ->setDescription(t('The attribute value name.'))
      ->setRequired(TRUE)
      ->setTranslatable(TRUE)
      ->setSettings([
        'default_value' => '',
        'max_length' => 255,
      ])
      ->setDisplayOptions('view', [
        'label' => 'hidden',
        'type' => 'string',
        'weight' => -5,
      ])
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
        'weight' => -5,
      ])
137
      ->setDisplayConfigurable('view', TRUE)
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
      ->setDisplayConfigurable('form', TRUE);

    $fields['weight'] = BaseFieldDefinition::create('integer')
      ->setLabel(t('Weight'))
      ->setDescription(t('The weight of this attribute value in relation to others.'))
      ->setDefaultValue(0);

    $fields['created'] = BaseFieldDefinition::create('created')
      ->setLabel(t('Created'))
      ->setDescription(t('The time when the attribute value was created.'))
      ->setTranslatable(TRUE);

    $fields['changed'] = BaseFieldDefinition::create('changed')
      ->setLabel(t('Changed'))
      ->setDescription(t('The time when the attribute value was last edited.'))
      ->setTranslatable(TRUE);

    return $fields;
  }

}