ConfigPages.php in Config Pages 8
File
src/Entity/ConfigPages.php
View source
<?php
namespace Drupal\config_pages\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\config_pages\ConfigPagesInterface;
use Drupal\Core\Url;
class ConfigPages extends ContentEntityBase implements ConfigPagesInterface {
use EntityChangedTrait;
protected $theme;
public function createDuplicate() {
$duplicate = parent::createDuplicate();
$duplicate->revision_id->value = NULL;
$duplicate->id->value = NULL;
return $duplicate;
}
public function setTheme($theme) {
$this->theme = $theme;
return $this;
}
public function getTheme() {
return $this->theme;
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('Config page ID'))
->setDescription(t('The config page ID.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The config page UUID.'))
->setReadOnly(TRUE);
$fields['label'] = BaseFieldDefinition::create('string')
->setLabel(t('ConfigPage description'))
->setDescription(t('A brief description of your config page.'))
->setRevisionable(FALSE)
->setTranslatable(FALSE)
->setDisplayOptions('view', [
'type' => 'hidden',
])
->setDisplayConfigurable('form', TRUE);
$fields['type'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('ConfigPage type'))
->setDescription(t('The config page type.'))
->setSetting('target_type', 'config_pages_type');
$fields['context'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Context'))
->setDescription(t('The Config Page context.'))
->setRevisionable(FALSE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the config page was last edited.'))
->setTranslatable(TRUE)
->setRevisionable(TRUE);
return $fields;
}
public function getChangedTime() {
return $this
->get('changed')->value;
}
public function setLabel($label) {
$this
->set('label', $label);
return $this;
}
public static function create(array $values = []) {
return \Drupal::entityTypeManager()
->getStorage('config_pages')
->create($values);
}
public static function config($type, $context = NULL) {
if (!empty($type)) {
$conditions['type'] = $type;
if ($context == NULL) {
$type = ConfigPagesType::load($type);
if (!is_object($type)) {
return NULL;
}
$conditions['context'] = $type
->getContextData();
}
else {
$conditions['context'] = $context;
}
$list = \Drupal::entityTypeManager()
->getStorage('config_pages')
->loadByProperties($conditions);
}
return $list ? current($list) : NULL;
}
public function urlInfo($rel = 'canonical', array $options = []) {
$config_pages_type = ConfigPagesType::load($this
->bundle());
$menu = $config_pages_type
->get('menu');
$path = isset($menu['path']) ? $menu['path'] : '';
return $path ? Url::fromRoute('config_pages.' . $this
->bundle(), [
'config_pages' => $this
->id(),
]) : Url::fromRoute('entity.config_pages.canonical', [
'config_pages' => $this
->id(),
]);
}
}