Added "owner" functions to OCEvent.php as it was erroring without them

This commit is contained in:
naomi 2021-01-28 16:32:28 +00:00
parent 517938ab7b
commit c479e3b995
1 changed files with 71 additions and 0 deletions

View File

@ -2,6 +2,7 @@
namespace Drupal\opencase_entities\Entity; namespace Drupal\opencase_entities\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase; use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait; use Drupal\Core\Entity\EntityChangedTrait;
@ -44,6 +45,7 @@ use Drupal\Core\Datetime\DrupalDateTime;
* "bundle" = "type", * "bundle" = "type",
* "label" = "name", * "label" = "name",
* "uuid" = "uuid", * "uuid" = "uuid",
* "uid" = "user_id",
* "langcode" = "langcode", * "langcode" = "langcode",
* "published" = "status", * "published" = "status",
* }, * },
@ -94,6 +96,45 @@ class OCEvent extends ContentEntityBase implements OCEventInterface {
return $this; return $this;
} }
/**
* {@inheritdoc}
*/
public function getOwner() {
return $this->get('user_id')->entity;
}
/**
* {@inheritdoc}
*/
public function getOwnerId() {
return $this->get('user_id')->target_id;
}
/**
* {@inheritdoc}
*/
public function setOwnerId($uid) {
$this->set('user_id', $uid);
return $this;
}
/**
* {@inheritdoc}
*/
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'user_id' => \Drupal::currentUser()->id(),
];
}
/**
* {@inheritdoc}
*/
public function setOwner(UserInterface $account) {
$this->set('user_id', $account->id());
return $this;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -103,6 +144,19 @@ class OCEvent extends ContentEntityBase implements OCEventInterface {
// Add the published field. // Add the published field.
$fields += static::publishedBaseFieldDefinitions($entity_type); $fields += static::publishedBaseFieldDefinitions($entity_type);
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Entered by'))
->setDescription(t('The user ID of author of the Event entity.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'author',
'weight' => -4,
]);
$fields['name'] = BaseFieldDefinition::create('string') $fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name')) ->setLabel(t('Name'))
->setDescription(t('The name of the Event entity.')) ->setDescription(t('The name of the Event entity.'))
@ -172,6 +226,23 @@ class OCEvent extends ContentEntityBase implements OCEventInterface {
]) ])
->setRequired(FALSE); ->setRequired(FALSE);
$fields['attendees'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Attendees'))
->setDescription(t('People attending this event.'))
->setSetting('target_type', 'oc_actor')
->setSetting('handler', 'default')
->setTranslatable(TRUE)
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setDisplayOptions('view', [
'type' => 'string',
'weight' => 50,
])
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete_tags',
'weight' => 50,
])
->setRequired(FALSE);
return $fields; return $fields;
} }