diff --git a/content_entity_example/content_entity_example.install b/content_entity_example/content_entity_example.install index 4b08565..caf01d4 100755 --- a/content_entity_example/content_entity_example.install +++ b/content_entity_example/content_entity_example.install @@ -11,7 +11,7 @@ function content_entity_example_schema() { $schema['content_entity_example'] = array( 'description' => 'Stores ContentEntityExample items.', 'fields' => array( - 'ceeid' => array( + 'id' => array( 'type' => 'serial', 'not null' => TRUE, 'description' => 'Primary Key: Unique ContentEntityExample item ID.', @@ -22,20 +22,6 @@ function content_entity_example_schema() { 'length' => 128, 'not null' => FALSE, ), - 'type' => array( - 'description' => 'The bundle of the ContentEntityExample entity.', - 'type' => 'varchar', - 'length' => 32, - 'not null' => TRUE, - 'default' => '', - ), - 'langcode' => array( - 'description' => 'The {language}.langcode of the original variant of this ContentEntityExample entity.', - 'type' => 'varchar', - 'length' => 12, - 'not null' => TRUE, - 'default' => '', - ), 'name' => array( 'description' => 'The name of the ContentEntityExample entity.', 'type' => 'varchar', @@ -43,22 +29,27 @@ function content_entity_example_schema() { 'not null' => TRUE, 'default' => '', ), - 'user_id' => array( + 'uid' => array( 'type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE, 'default' => NULL, 'description' => 'The {users}.uid of the associated user.', ), - 'content_entity_example_field' => array( - 'description' => 'Additional field for the ContentEntityExample entity.', - 'type' => 'varchar', - 'length' => 32, - 'not null' => FALSE, - 'default' => '', + 'created' => array( + 'description' => 'The Unix timestamp when the entity was created.', + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + ), + 'changed' => array( + 'description' => 'The Unix timestamp when the entity was most recently saved.', + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, ), ), - 'primary key' => array('ceeid'), + 'primary key' => array('id'), 'unique keys' => array( 'uuid' => array('uuid'), ), diff --git a/content_entity_example/content_entity_example.module b/content_entity_example/content_entity_example.module index d166e44..7f1452b 100755 --- a/content_entity_example/content_entity_example.module +++ b/content_entity_example/content_entity_example.module @@ -10,17 +10,20 @@ */ function content_entity_example_menu_link_defaults() { $links = array(); + $links['content_entity_example.admin.structure.settings'] = array( 'link_title' => 'Content Entity Example Settings', 'description' => 'Configure content_entity_example entity', 'route_name' => 'content_entity_example.settings', 'parent' => 'system.admin.structure', ); + $links['content_entity_example.list'] = array( 'link_title' => 'Content Entity Example Listing', 'description' => 'List ContentEntityExample content', 'route_name' => 'content_entity_example.list', ); + return $links; } @@ -29,19 +32,19 @@ function content_entity_example_menu_link_defaults() { */ function content_entity_example_permission() { return array( - 'delete_content_entity_example' => array( + 'delete content_entity_example entity' => array( 'title' => t('Delete entity content.'), ), - 'add_content_entity_example' => array( + 'add content_entity_example entity' => array( 'title' => t('Add entity content'), ), - 'view_content_entity_example' => array( + 'view content_entity_example entity' => array( 'title' => t('View entity content'), ), - 'edit_content_entity_example' => array( + 'edit content_entity_example entity' => array( 'title' => t('Edit entity content'), ), - 'admin_content_entity_example' => array( + 'admininister content_entity_example entity' => array( 'title' => t('Administer settings'), ), ); diff --git a/content_entity_example/content_entity_example.routing.yml b/content_entity_example/content_entity_example.routing.yml index e1c3773..b7970f5 100755 --- a/content_entity_example/content_entity_example.routing.yml +++ b/content_entity_example/content_entity_example.routing.yml @@ -4,39 +4,39 @@ content_entity_example.view: _entity_view: 'content_entity_example' _title: 'Content Entity Example Content' requirements: - _permission: 'view_content_entity_example' + _permission: 'view content_entity_example entity' content_entity_example.list: path: '/content-entity-example/list' defaults: _entity_list: 'content_entity_example' _title: 'ContentEntityExample Content List' requirements: - _permission: 'view_content_entity_example' + _permission: 'view content_entity_example entity' content_entity_example.add: path: '/content-entity-example/add' defaults: _entity_form: content_entity_example.add _title: 'Add ContentEntityExample Content' requirements: - _permission: 'add_content_entity_example' + _permission: 'add content_entity_example entity' content_entity_example.edit: path: '/content-entity-example/{content_entity_example}/edit' defaults: _entity_form: content_entity_example.edit _title: 'Edit ContentEntityExample content' requirements: - _permission: 'edit_content_entity_example' + _permission: 'edit content_entity_example entity' content_entity_example.delete: path: '/content-entity-example/{content_entity_example}/delete' defaults: _entity_form: content_entity_example.delete _title: 'Delete ContentEntityExample Content ' requirements: - _permission: 'delete_content_entity_example' + _permission: 'delete content_entity_example entity' content_entity_example.settings: path: 'admin/structure/content_entity_example_settings' defaults: _form: '\Drupal\content_entity_example\Form\ContentEntityExampleSettingsForm' _title: 'ContentEntityExample Settings' requirements: - _permission: 'admin_content_entity_example' + _permission: 'administer content_entity_example entity' diff --git a/content_entity_example/lib/Drupal/content_entity_example/ContentEntityExampleInterface.php b/content_entity_example/lib/Drupal/content_entity_example/ContentEntityExampleInterface.php index 8a85939..e4a9541 100755 --- a/content_entity_example/lib/Drupal/content_entity_example/ContentEntityExampleInterface.php +++ b/content_entity_example/lib/Drupal/content_entity_example/ContentEntityExampleInterface.php @@ -5,44 +5,12 @@ */ namespace Drupal\content_entity_example; -use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityTypeInterface; +use Drupal\user\EntityOwnerInterface; /** * Provides an interface defining a ContentEntityExample entity. */ -interface ContentEntityExampleInterface extends EntityInterface { - /** - * Returns the identifier. - * - * @return int - * The entity identifier. - */ - public function id(); - /** - * Returns the entity UUID (Universally Unique Identifier). - * - * The UUID is guaranteed to be unique and can be used to identify an entity - * across multiple systems. - * - * @return string - * The UUID of the entity. - */ - public function uuid(); - /** - * Return the Value of ContentEntityExample Field. - * - * @return string - * The content of the field. - */ - public function getContentEntityExampleField(); - /** - * Defines the base fields of the entity type. - * - * @param string $entity_type - * Name of the entity type - * - * @return \Drupal\Core\Field\FieldDefinitionInterface[] - * An array of entity field definitions, keyed by field name. - */ - public static function baseFieldDefinitions(EntityTypeInterface $entity_type); +interface ContentEntityExampleInterface extends ContentEntityInterface, EntityOwnerInterface { + } diff --git a/content_entity_example/lib/Drupal/content_entity_example/Entity/ContentEntityExample.php b/content_entity_example/lib/Drupal/content_entity_example/Entity/ContentEntityExample.php index c59d82a..e400654 100755 --- a/content_entity_example/lib/Drupal/content_entity_example/Entity/ContentEntityExample.php +++ b/content_entity_example/lib/Drupal/content_entity_example/Entity/ContentEntityExample.php @@ -10,6 +10,7 @@ use Drupal\Core\Field\FieldDefinition; use Drupal\Core\Entity\ContentEntityBase; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\content_entity_example\ContentEntityExampleInterface; +use Drupal\user\UserInterface; /** * Defines the ContentEntityExample entity. * @@ -25,14 +26,12 @@ use Drupal\content_entity_example\ContentEntityExampleInterface; * "edit" = "Drupal\content_entity_example\Entity\Form\ContentEntityExampleFormController", * "delete" = "Drupal\content_entity_example\Entity\Form\ContentEntityExampleDeleteForm", * }, - * "translation" = "Drupal\content_translation\ContentTranslationController" * }, * base_table = "content_entity_example", - * admin_permission = "admin_content_entity_example", + * admin_permission = "admin content_entity_example entity", * fieldable = TRUE, - * translatable = TRUE, * entity_keys = { - * "id" = "ceeid", + * "id" = "id", * "label" = "name", * "uuid" = "uuid" * }, @@ -44,51 +43,78 @@ use Drupal\content_entity_example\ContentEntityExampleInterface; * ) */ class ContentEntityExample extends ContentEntityBase implements ContentEntityExampleInterface { + /** * {@inheritdoc} */ - public function id() { - return $this->get('ceeid')->value; + public static function preCreate(EntityStorageControllerInterface $storage_controller, array &$values) { + parent::preCreate($storage_controller, $values); + $values += array( + 'uid' => \Drupal::currentUser()->id(), + ); } /** * {@inheritdoc} */ - public function getContentEntityExampleField() { - return $this->content_entity_example_field->value; + public function getCreatedTime() { + return $this->get('created')->value; } /** * {@inheritdoc} */ - public static function preCreate(EntityStorageControllerInterface $storage_controller, array &$values) { - parent::preCreate($storage_controller, $values); - $values += array( - 'user_id' => \Drupal::currentUser()->id(), - ); + public function getChangedTime() { + return $this->get('changed')->value; } + /** + * {@inheritdoc} + */ + public function getOwner() { + return $this->get('uid')->entity; + } + + /** + * {@inheritdoc} + */ + public function getOwnerId() { + return $this->get('uid')->target_id; + } + + /** + * {@inheritdoc} + */ + public function setOwnerId($uid) { + $this->set('uid', $uid); + return $this; + } + + /** + * {@inheritdoc} + */ + public function setOwner(UserInterface $account) { + $this->set('uid', $account->id()); + return $this; + } /** * {@inheritdoc} */ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { - $fields['ceeid'] = FieldDefinition::create('integer') + $fields['id'] = FieldDefinition::create('integer') ->setLabel(t('ID')) ->setDescription(t('The ID of the ContentEntityExample entity.')) ->setReadOnly(TRUE); + $fields['uuid'] = FieldDefinition::create('uuid') ->setLabel(t('UUID')) ->setDescription(t('The UUID of the ContentEntityExample entity.')) ->setReadOnly(TRUE); - $fields['langcode'] = FieldDefinition::create('language') - ->setLabel(t('Language code')) - ->setDescription(t('The language code of ContentEntityExample entity.')); + $fields['name'] = FieldDefinition::create('string') ->setLabel(t('Name')) ->setDescription(t('The name of the ContentEntityExample entity.')) - ->setTranslatable(TRUE) - ->setPropertyConstraints('value', array('Length' => array('max' => 32))) ->setSettings(array( 'default_value' => '', 'max_length' => 255, @@ -106,38 +132,18 @@ class ContentEntityExample extends ContentEntityBase implements ContentEntityExa ->setDisplayConfigurable('form', TRUE) ->setDisplayConfigurable('view', TRUE); - $fields['type'] = FieldDefinition::create('string') - ->setLabel(t('Type')) - ->setDescription(t('The bundle of the ContentEntityExample entity.')) - ->setRequired(TRUE); - - $fields['user_id'] = FieldDefinition::create('entity_reference') + $fields['uid'] = FieldDefinition::create('entity_reference') ->setLabel(t('User ID')) ->setDescription(t('The ID of the associated user.')) - ->setSettings(array('target_type' => 'user')) - ->setTranslatable(TRUE); + ->setSettings(array('target_type' => 'user')); - $fields['content_entity_example_field'] = FieldDefinition::create('string') - ->setLabel(t('First ContentEntityExample Field')) - ->setDescription(t('One field of the ContentEntityExample entity.')) - ->setTranslatable(TRUE) - ->setPropertyConstraints('value', array('Length' => array('max' => 32))) - ->setSettings(array( - 'default_value' => '', - 'max_length' => 255, - 'text_processing' => 0, - )) - ->setDisplayOptions('view', array( - 'label' => 'Above', - 'type' => 'string', - 'weight' => -5, - )) - ->setDisplayOptions('form', array( - 'type' => 'string', - 'weight' => -5, - )) - ->setDisplayConfigurable('form', TRUE) - ->setDisplayConfigurable('view', TRUE); + $fields['created'] = FieldDefinition::create('created') + ->setLabel(t('Created')) + ->setDescription(t('The time that the entity was created.')); + + $fields['changed'] = FieldDefinition::create('changed') + ->setLabel(t('Changed')) + ->setDescription(t('The time that the entity was last edited.')); return $fields; } diff --git a/content_entity_example/lib/Drupal/content_entity_example/Entity/Controller/ContentEntityExampleListController.php b/content_entity_example/lib/Drupal/content_entity_example/Entity/Controller/ContentEntityExampleListController.php index 7f30285..08fe8bf 100755 --- a/content_entity_example/lib/Drupal/content_entity_example/Entity/Controller/ContentEntityExampleListController.php +++ b/content_entity_example/lib/Drupal/content_entity_example/Entity/Controller/ContentEntityExampleListController.php @@ -21,7 +21,6 @@ class ContentEntityExampleListController extends EntityListController { public function buildHeader() { $header['id'] = t('ContentEntityExampleID'); $header['label'] = t('Label'); - $header['content_entity_example_field'] = t('ContentEntityExampleField'); return $header + parent::buildHeader(); } @@ -31,8 +30,10 @@ class ContentEntityExampleListController extends EntityListController { public function buildRow(EntityInterface $entity) { /* @var $entity \Drupal\content_entity_example\Entity\ContentEntityExample */ $row['id'] = $entity->id(); - $row['label'] = l($this->getLabel($entity), 'content-entity-example/' . $entity->id()); - $row['content_entity_example_field'] = $entity->getContentEntityExampleField(); + $row['label'] = \Drupal::l($this->getLabel($entity), + 'content_entity_example.view', array( + 'content_entity_example' => $entity->id(), + )); return $row + parent::buildRow($entity); } } diff --git a/content_entity_example/lib/Drupal/content_entity_example/Entity/Form/ContentEntityExampleFormController.php b/content_entity_example/lib/Drupal/content_entity_example/Entity/Form/ContentEntityExampleFormController.php index 69eef29..d7b6d86 100755 --- a/content_entity_example/lib/Drupal/content_entity_example/Entity/Form/ContentEntityExampleFormController.php +++ b/content_entity_example/lib/Drupal/content_entity_example/Entity/Form/ContentEntityExampleFormController.php @@ -22,10 +22,10 @@ class ContentEntityExampleFormController extends ContentEntityFormController { $form = parent::form($form, $form_state); $entity = $this->entity; - $form['user_id'] = array( + $form['uid'] = array( '#type' => 'textfield', '#title' => 'UID', - '#default_value' => $entity->user_id->target_id, + '#default_value' => $entity->uid->target_id, '#size' => 60, '#maxlength' => 128, '#required' => TRUE,