Skip to content

Instantly share code, notes, and snippets.

@samuelsolis
Created February 7, 2018 08:15
Show Gist options
  • Save samuelsolis/30785b814cfa3bdae7fcf2abaac287b0 to your computer and use it in GitHub Desktop.
Save samuelsolis/30785b814cfa3bdae7fcf2abaac287b0 to your computer and use it in GitHub Desktop.
Drupal 8 custom entity without bundle
<?php
/**
* @file
* Contains \Drupal\example\Entity\Example.
*/
namespace Drupal\example\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\example\ExampleInterface;
use Drupal\user\UserInterface;
/**
* Defines the Example entity.
*
* @ingroup example
*
* @ContentEntityType(
* id = "example",
* label = @Translation("Example entity"),
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "views_data" = "Drupal\views\EntityViewsData",
* "list_builder" = "Drupal\example\Entity\Controller\ExampleListBuilder",
* },
* base_table = "examples",
* admin_permission = "administer example entity",
* fieldable = FALSE,
* entity_keys = {
* "id" = "id",
* "label" = "title",
* },
* links = {
* "collection" = "/example/list"
* },
* )
*/
class Example extends ContentEntityBase implements ExampleInterface {
/**
* {@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 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}
*
* Define the field properties here.
*
* Field name, type and size determine the table structure.
*
* In addition, we can define how the field and its content can be manipulated
* in the GUI. The behaviour of the widgets used can be determined here.
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
// Some example fields.
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setSettings([
'size' => 'big',
])
->setDescription(t('The ID of the Example entity.'))
->setReadOnly(TRUE);
$fields['uid'] = BaseFieldDefinition::create('integer')
->setLabel(t('Customer user id'))
->setDescription(t('Customer user id in origin'))
->setSettings(array(
'default_value' => null,
))
->setDisplayOptions('view', array(
'label' => 'above',
'type' => 'int',
))
->setDisplayConfigurable('view', TRUE)
->setReadOnly(TRUE);
$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(t('Contact textfield'))
->setDescription(t('Example textfield property'))
->setSettings(array(
'default_value' => '',
'max_length' => 255,
))
->setDisplayOptions('view', array(
'label' => 'above',
'type' => 'string',
))
->setDisplayConfigurable('view', TRUE);
//Meta fields
$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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment