private_page-8.x-1.0-alpha1/src/Entity/PrivatePage.php
src/Entity/PrivatePage.php
<?php
namespace Drupal\private_page\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\private_page\PrivatePageInterface;
use Drupal\user\UserInterface;
/**
* @ContentEntityType(
* id = "private_page",
* label = @Translation("Private page"),
* handlers = {
* "list_builder" = "Drupal\private_page\PrivatePageListBuilder",
* "views_data" = "Drupal\views\EntityViewsData",
* "form" = {
* "add" = "Drupal\private_page\Entity\Form\PrivatePageForm",
* "edit" = "Drupal\private_page\Entity\Form\PrivatePageForm",
* "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
* },
* "access" = "Drupal\private_page\Entity\Access\PrivatePageAccessControlHandler",
* },
* base_table = "private_page",
* admin_permission = "administer private page",
* entity_keys = {
* "id" = "id",
* "uuid" = "uuid"
* },
* links = {
* "edit-form" = "/private-page/{private_page}/edit",
* "delete-form" = "/private-page/{private_page}/delete",
* "collection" = "/private-pages"
* }
* )
*/
class PrivatePage extends ContentEntityBase implements PrivatePageInterface {
/**
* {@inheritdoc}
*/
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += array(
'user_id' => \Drupal::currentUser()->id(),
);
}
/**
* {@inheritdoc}
*/
public function getCreatedTime() {
return $this->get('created')->value;
}
/**
* {@inheritdoc}
*/
public function getChangedTime() {
return $this->get('changed')->value;
}
/**
* {@inheritdoc}
*/
public function setChangedTime($timestamp) {
$this->set('changed', $timestamp);
return $this;
}
/**
* {@inheritdoc}
*/
public function getChangedTimeAcrossTranslations() {
$changed = $this->getUntranslated()->getChangedTime();
foreach ($this->getTranslationLanguages(FALSE) as $language) {
$translation_changed = $this->getTranslation($language->getId())->getChangedTime();
$changed = max($translation_changed, $changed);
}
return $changed;
}
/**
* {@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 function setOwner(UserInterface $account) {
$this->set('user_id', $account->id());
return $this;
}
/**
* {@inheritdoc}
*/
public function getPrivatePagePath() {
return current($this->get('private_path')->getValue())['value'];
}
/**
* {@inheritdoc}
*/
public function getPermissions() {
return current($this->get('permissions')->getValue());
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the Contact entity.'))
->setReadOnly(TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the Contact entity.'))
->setReadOnly(TRUE);
$fields['private_path'] = BaseFieldDefinition::create('string')
->setLabel(t('Private path'))
->setDescription(t('The private page path.'));
$fields['permissions'] = BaseFieldDefinition::create('map')
->setLabel(t('Permissons'))
->setDescription(t('The private page permissions.'));
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language code'))
->setDescription(t('The language code of Contact entity.'));
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the entity was created.'));
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the entity was last edited.'));
return $fields;
}
}
