Flipbook.php in 3D Flipbook 8
Namespace
Drupal\flipbook\EntityFile
src/Entity/Flipbook.phpView source
<?php
namespace Drupal\flipbook\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\flipbook\FlipbookInterface;
use Drupal\user\UserInterface;
/**
* Defines the Flipbook entity.
*
* @ingroup flipbook
*
* This is the main definition of the entity type. From it, an entityType is
* derived. The most important properties in this example are listed below.
*
* id: The unique identifier of this entityType. It follows the pattern
* 'moduleName_xyz' to avoid naming conflicts.
*
* label: Human readable name of the entity type.
*
* handlers: Handler classes are used for different tasks. You can use
* standard handlers provided by D8 or build your own, most probably derived
* from the standard class. In detail:
*
* - view_builder: we use the standard controller to view an instance. It is
* called when a route lists an '_entity_view' default for the entityType
* (see routing.yml for details. The view can be manipulated by using the
* standard drupal tools in the settings.
*
* - list_builder: We derive our own list builder class from the
* entityListBuilder to control the presentation.
* If there is a view available for this entity from the views module, it
* overrides the list builder. @todo: any view? naming convention?
*
* - form: We derive our own forms to add functionality like additional fields,
* redirects etc. These forms are called when the routing list an
* '_entity_form' default for the entityType. Depending on the suffix
* (.add/.edit/.delete) in the route, the correct form is called.
*
* - access: Our own accessController where we determine access rights based on
* permissions.
*
* More properties:
*
* - base_table: Define the name of the table used to store the data. Make sure
* it is unique. The schema is automatically determined from the
* BaseFieldDefinitions below. The table is automatically created during
* installation.
*
* - fieldable: Can additional fields be added to the entity via the GUI?
* Analog to content types.
*
* - entity_keys: How to access the fields. Analog to 'nid' or 'uid'.
*
* - links: Provide links to do standard tasks. The 'edit-form' and
* 'delete-form' links are added to the list built by the
* entityListController. They will show up as action buttons in an additional
* column.
*
* There are many more properties to be used in an entity type definition. For
* a complete overview, please refer to the '\Drupal\Core\Entity\EntityType'
* class definition.
*
* The following construct is the actual definition of the entity type which
* is read and cached. Don't forget to clear cache after changes.
*
* @ContentEntityType(
* id = "flipbook",
* label = @Translation("Flipbook entity"),
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\flipbook\Entity\Controller\FlipbookListBuilder",
* "views_data" = "Drupal\views\EntityViewsData",
* "form" = {
* "add" = "Drupal\flipbook\Form\FlipbookForm",
* "edit" = "Drupal\flipbook\Form\FlipbookForm",
* "delete" = "Drupal\flipbook\Form\FlipbookDeleteForm",
* },
* "access" = "Drupal\flipbook\FlipbookAccessControlHandler",
* },
* base_table = "flipbook",
* admin_permission = "administer flipbook entity",
* fieldable = TRUE,
* entity_keys = {
* "id" = "id",
* "label" = "name",
* "uuid" = "uuid",
* "flipbook_cover" = "flipbook_cover",
* "flipbook" = "flipbook"
*
* },
* links = {
* "canonical" = "/flipbook/{flipbook}",
* "edit-form" = "/flipbook/{flipbook}/edit",
* "delete-form" = "/contact/{flipbook}/delete",
* "collection" = "/flipbook/list"
* },
* field_ui_base_route = "flipbook.settings",
* )
*
* The 'links' above are defined by their path. For core to find the
* corresponding route, the route name must follow the correct pattern:
*
* entity.<entity-name>.<link-name> (replace dashes with underscores)
* Example: 'entity.flipbook.canonical'
*
* See routing file above for the corresponding implementation
*
* The 'Flipbook' class defines methods and fields for the contact entity.
*
* Being derived from the ContentEntityBase class, we can override the methods
* we want. In our case we want to provide access to the standard fields about
* creation and changed time stamps.
*
* Our interface (see FlipbookInterface) also exposes the EntityOwnerInterface.
* This allows us to provide methods for setting and providing ownership
* information.
*
* The most important part is the definitions of the field properties for this
* entity type. These are of the same type as fields added through the GUI, but
* they can by changed in code. In the definition we can define if the user with
* the rights privileges can influence the presentation (view, edit) of each
* field.
*/
class Flipbook extends ContentEntityBase implements FlipbookInterface {
/**
* {@inheritdoc}
*
* When a new entity instance is added, set the user_id entity reference to
* the current user as the creator of the instance.
*/
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'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}
*
* 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) {
// Standard field, used as unique if primary index.
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the Contact entity.'))
->setReadOnly(TRUE);
// Standard field, unique outside of the scope of the current project.
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the Contact entity.'))
->setReadOnly(TRUE);
// Name field for the contact.
// We set display options for the view as well as the form.
// Users with correct privileges can change the view and edit configuration.
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('Name of the Flipbook entity.'))
->setRequired(TRUE)
->setSettings([
'default_value' => '',
'max_length' => 255,
'text_processing' => 0,
])
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -6,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -6,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['flipbook_cover'] = BaseFieldDefinition::create('image')
->setLabel(t('Flipbook Cover Image'))
->setDescription(t('Upload Flipbook Cover Image'))
->setRequired(TRUE)
->setSettings([
'file_directory' => 'flipbook',
'alt_field_required' => FALSE,
'file_extensions' => 'png jpg jpeg',
])
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'default',
'weight' => 0,
])
->setDisplayOptions('form', [
'label' => 'hidden',
'type' => 'image_image',
'weight' => 0,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
// Owner field of the contact.
// Entity reference field, holds the reference to the user object.
// The view shows the user name field of the user.
// The form presents a auto complete field for the user name.
$validators = [
'file_validate_extensions' => [
'pdf doc docx xls xlsx ppt pptx png jpg jpeg',
],
'file_validate_size' => [
file_upload_max_size(),
],
];
$fields['flipbook'] = BaseFieldDefinition::create('file')
->setLabel(t('Flipbook PDF'))
->setDescription(t('Upload Flipbook pdf file.'))
->setRequired(TRUE)
->setSetting('upload_validators', $validators)
->setSetting('file_extensions', 'pdf png jpg jpeg svg ... etc ')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'file',
'weight' => -3,
])
->setDisplayOptions('form', [
'type' => 'file_generic',
// doesn't work.
'description' => [
// doesn't work.
'theme' => 'file_upload_help',
// doesn't work.
'description' => t('A Gettext Portable Object file.'),
],
'settings' => [
'upload_validators' => $validators,
],
'weight' => -3,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language code'))
->setDescription(t('The language code of Flipbook 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;
}
}