Passed
Pull Request — 8.x-2.x (#26)
by Frédéric G.
02:49
created

EventTemplate::preCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\mongodb_watchdog\Entity;
4
5
use Drupal\Core\Entity\EntityStorageInterface;
6
use Drupal\Core\Field\BaseFieldDefinition;
7
use Drupal\Core\Entity\ContentEntityBase;
8
use Drupal\Core\Entity\EntityChangedTrait;
9
use Drupal\Core\Entity\EntityTypeInterface;
10
use Drupal\user\UserInterface;
11
12
/**
13
 * Defines the Event template entity.
14
 *
15
 * @ingroup mongodb_watchdog
16
 *
17
 * @ContentEntityType(
18
 *   id = "mongodb_watchdog_event_template",
19
 *   label = @Translation("Event template"),
20
 *   handlers = {
21
 *     "storage" = "Drupal\mongodb_watchdog\Model\EventTemplateStorage",
22
 *     "storage_schema" = "Drupal\mongodb_watchdog\Model\EventTemplateStorageSchema",
23
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
24
 *     "list_builder" = "Drupal\mongodb_watchdog\EventTemplateListBuilder",
25
 *     "Zviews_data" = "Drupal\mongodb_watchdog\Entity\EventTemplateViewsData",
26
 *     "translation" = "Drupal\mongodb_watchdog\EventTemplateTranslationHandler",
27
 *
28
 *     "form" = {
29
 *       "default" = "Drupal\mongodb_watchdog\Form\EventTemplateForm",
30
 *       "add" = "Drupal\mongodb_watchdog\Form\EventTemplateForm",
31
 *       "edit" = "Drupal\mongodb_watchdog\Form\EventTemplateForm",
32
 *       "delete" = "Drupal\mongodb_watchdog\Form\EventTemplateDeleteForm",
33
 *     },
34
 *     "access" = "Drupal\mongodb_watchdog\EventTemplateAccessControlHandler",
35
 *     "route_provider" = {
36
 *       "html" = "Drupal\mongodb_watchdog\EventTemplateHtmlRouteProvider",
37
 *     },
38
 *   },
39
 *   base_table = "mongodb_watchdog_event_template",
40
 *   data_table = "mongodb_watchdog_event_template_field_data",
41
 *   translatable = TRUE,
42
  *   admin_permission = "administer event template entities",
43
 *   entity_keys = {
44
 *     "id" = "id",
45
 *     "label" = "name",
46
 *     "uuid" = "uuid",
47
 *     "uid" = "user_id",
48
 *     "langcode" = "langcode",
49
 *     "status" = "status",
50
 *   },
51
 *   links = {
52
 *     "canonical" = "/admin/reports/watchdog/mongodb_watchdog_event_template/{mongodb_watchdog_event_template}",
53
 *     "add-form" = "/admin/reports/watchdog/mongodb_watchdog_event_template/add",
54
 *     "edit-form" = "/admin/reports/watchdog/mongodb_watchdog_event_template/{mongodb_watchdog_event_template}/edit",
55
 *     "delete-form" = "/admin/reports/watchdog/mongodb_watchdog_event_template/{mongodb_watchdog_event_template}/delete",
56
 *     "collection" = "/admin/reports/watchdog/mongodb_watchdog_event_template",
57
 *   },
58
 *   field_ui_base_route = "mongodb_watchdog_event_template.settings"
59
 * )
60
 */
61
class EventTemplate extends ContentEntityBase implements EventTemplateInterface {
62
63
  use EntityChangedTrait;
64
65
  /**
66
   * {@inheritdoc}
67
   */
68
  public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
69
    parent::preCreate($storage_controller, $values);
70
    $values += array(
71
      'user_id' => \Drupal::currentUser()->id(),
72
    );
73
  }
74
75
  /**
76
   * {@inheritdoc}
77
   */
78
  public function getName() {
79
    return $this->get('name')->value;
80
  }
81
82
  /**
83
   * {@inheritdoc}
84
   */
85
  public function setName($name) {
86
    $this->set('name', $name);
87
    return $this;
88
  }
89
90
  /**
91
   * {@inheritdoc}
92
   */
93
  public function getCreatedTime() {
94
    return $this->get('created')->value;
95
  }
96
97
  /**
98
   * {@inheritdoc}
99
   */
100
  public function setCreatedTime($timestamp) {
101
    $this->set('created', $timestamp);
102
    return $this;
103
  }
104
105
  /**
106
   * {@inheritdoc}
107
   */
108
  public function getOwner() {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
109
    return $this->get('user_id')->entity;
110
  }
111
112
  /**
113
   * {@inheritdoc}
114
   */
115
  public function getOwnerId() {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
116
    return $this->get('user_id')->target_id;
117
  }
118
119
  /**
120
   * {@inheritdoc}
121
   */
122
  public function setOwnerId($uid) {
123
    $this->set('user_id', $uid);
124
    return $this;
125
  }
126
127
  /**
128
   * {@inheritdoc}
129
   */
130
  public function setOwner(UserInterface $account) {
131
    $this->set('user_id', $account->id());
132
    return $this;
133
  }
134
135
  /**
136
   * {@inheritdoc}
137
   */
138
  public function isPublished() {
139
    return (bool) $this->getEntityKey('status');
140
  }
141
142
  /**
143
   * {@inheritdoc}
144
   */
145
  public function setPublished($published) {
146
    $this->set('status', $published ? NODE_PUBLISHED : NODE_NOT_PUBLISHED);
147
    return $this;
148
  }
149
150
  /**
151
   * {@inheritdoc}
152
   */
153
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
154
    $fields = parent::baseFieldDefinitions($entity_type);
155
156
    $fields['user_id'] = BaseFieldDefinition::create('entity_reference')
157
      ->setLabel(t('Authored by'))
158
      ->setDescription(t('The user ID of author of the Event template entity.'))
159
      ->setRevisionable(TRUE)
160
      ->setSetting('target_type', 'user')
161
      ->setSetting('handler', 'default')
162
      ->setTranslatable(TRUE)
163
      ->setDisplayOptions('view', array(
164
        'label' => 'hidden',
165
        'type' => 'author',
166
        'weight' => 0,
167
      ))
168
      ->setDisplayOptions('form', array(
169
        'type' => 'entity_reference_autocomplete',
170
        'weight' => 5,
171
        'settings' => array(
172
          'match_operator' => 'CONTAINS',
173
          'size' => '60',
174
          'autocomplete_type' => 'tags',
175
          'placeholder' => '',
176
        ),
177
      ))
178
      ->setDisplayConfigurable('form', TRUE)
179
      ->setDisplayConfigurable('view', TRUE);
180
181
    $fields['name'] = BaseFieldDefinition::create('string')
182
      ->setLabel(t('Name'))
183
      ->setDescription(t('The name of the Event template entity.'))
184
      ->setSettings(array(
185
        'max_length' => 50,
186
        'text_processing' => 0,
187
      ))
188
      ->setDefaultValue('')
189
      ->setDisplayOptions('view', array(
190
        'label' => 'above',
191
        'type' => 'string',
192
        'weight' => -4,
193
      ))
194
      ->setDisplayOptions('form', array(
195
        'type' => 'string_textfield',
196
        'weight' => -4,
197
      ))
198
      ->setDisplayConfigurable('form', TRUE)
199
      ->setDisplayConfigurable('view', TRUE);
200
201
    $fields['status'] = BaseFieldDefinition::create('boolean')
202
      ->setLabel(t('Publishing status'))
203
      ->setDescription(t('A boolean indicating whether the Event template is published.'))
204
      ->setDefaultValue(TRUE);
205
206
    $fields['created'] = BaseFieldDefinition::create('created')
207
      ->setLabel(t('Created'))
208
      ->setDescription(t('The time that the entity was created.'));
209
210
    $fields['changed'] = BaseFieldDefinition::create('changed')
211
      ->setLabel(t('Changed'))
212
      ->setDescription(t('The time that the entity was last edited.'));
213
214
    return $fields;
215
  }
216
217
}
218