Log.php 4.95 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php

namespace Drupal\commerce_log\Entity;

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

/**
 * Defines the log entity class.
 *
 * @ContentEntityType(
 *   id = "commerce_log",
 *   label = @Translation("Log"),
 *   label_singular = @Translation("log"),
 *   label_plural = @Translation("logs"),
 *   label_count = @PluralTranslation(
 *     singular = "@count log",
 *     plural = "@count logs",
 *   ),
 *   handlers = {
bojanz's avatar
bojanz committed
22
 *     "access" = "Drupal\commerce_log\LogAccessControlHandler",
23
24
 *     "list_builder" = "Drupal\commerce_log\LogListBuilder",
 *     "storage" = "Drupal\commerce_log\LogStorage",
25
 *     "storage_schema" = "Drupal\commerce_log\LogStorageSchema",
26
 *     "view_builder" = "Drupal\commerce_log\LogViewBuilder",
27
 *     "views_data" = "Drupal\commerce\CommerceEntityViewsData",
28
29
 *   },
 *   base_table = "commerce_log",
bojanz's avatar
bojanz committed
30
 *   internal = TRUE,
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
115
116
 *   entity_keys = {
 *     "id" = "log_id",
 *     "uuid" = "uuid",
 *   },
 * )
 */
class Log extends ContentEntityBase implements LogInterface {

  /**
   * {@inheritdoc}
   */
  public function getUserId() {
    return $this->get('uid')->target_id;
  }

  /**
   * {@inheritdoc}
   */
  public function getUser() {
    return $this->get('uid')->entity;
  }

  /**
   * {@inheritdoc}
   */
  public function label() {
    return $this->getCategory()->getLabel() . ': ' . $this->getTemplate()->getLabel();
  }

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

  /**
   * {@inheritdoc}
   */
  public function getCategory() {
    $log_category_manager = \Drupal::service('plugin.manager.commerce_log_category');
    return $log_category_manager->createInstance($this->getCategoryId());
  }

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

  /**
   * {@inheritdoc}
   */
  public function getTemplate() {
    $log_template_manager = \Drupal::service('plugin.manager.commerce_log_template');
    return $log_template_manager->createInstance($this->getTemplateId());
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function getSourceEntity() {
    return \Drupal::entityTypeManager()
      ->getStorage($this->getSourceEntityTypeId())
      ->load($this->getSourceEntityId());
  }

  /**
   * {@inheritdoc}
   */
  public function getParams() {
117
118
119
120
121
122
    if (!$this->get('params')->isEmpty()) {
      return $this->get('params')->first()->getValue();
    }
    else {
      return [];
    }
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  }

  /**
   * {@inheritdoc}
   */
  public function setParams(array $params) {
    $this->set('params', $params);
    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) {
    $fields = parent::baseFieldDefinitions($entity_type);

    $fields['uid'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('User'))
      ->setDescription(t('The user for the log.'))
      ->setSetting('target_type', 'user')
      ->setSetting('handler', 'default')
      ->setDefaultValueCallback('Drupal\commerce_log\Entity\Log::getCurrentUserId');

    $fields['template_id'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Log template ID'))
      ->setDescription(t('The log template plugin ID'));

    $fields['category_id'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Log category ID'))
      ->setDescription(t('The log category plugin ID'));

    $fields['source_entity_id'] = BaseFieldDefinition::create('integer')
      ->setLabel(t('Source entity ID'))
      ->setDescription(t('The source entity ID'));

    $fields['source_entity_type'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Source entity type'))
175
176
      ->setDescription(t('The source entity type'))
      ->setSetting('max_length', EntityTypeInterface::ID_MAX_LENGTH);
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

    $fields['params'] = BaseFieldDefinition::create('map')
      ->setLabel(t('Params'))
      ->setDescription(t('A serialized array of parameters for the log template.'));

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

    return $fields;
  }

  /**
   * Default value callback for 'uid' base field definition.
   *
   * @see ::baseFieldDefinitions()
   *
   * @return array
   *   An array of default values.
   */
  public static function getCurrentUserId() {
    return [\Drupal::currentUser()->id()];
  }

}