Created
January 6, 2017 14:30
-
-
Save rachellawson/226a583f1c6c469ef72e344149e54eb3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\destinations\Entity; | |
use Drupal\Core\Entity\EntityStorageInterface; | |
use Drupal\Core\Field\BaseFieldDefinition; | |
use Drupal\Core\Entity\RevisionableContentEntityBase; | |
use Drupal\Core\Entity\EntityChangedTrait; | |
use Drupal\Core\Entity\EntityTypeInterface; | |
use Drupal\user\UserInterface; | |
/** | |
* Defines the Destination entity. | |
* | |
* @ingroup destinations | |
* | |
* @ContentEntityType( | |
* id = "destination", | |
* label = @Translation("Destination"), | |
* bundle_label = @Translation("Destination type"), | |
* handlers = { | |
* "storage" = "Drupal\destinations\DestinationEntityStorage", | |
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder", | |
* "list_builder" = "Drupal\destinations\DestinationEntityListBuilder", | |
* "views_data" = "Drupal\destinations\Entity\DestinationEntityViewsData", | |
* "translation" = "Drupal\destinations\DestinationEntityTranslationHandler", | |
* | |
* "form" = { | |
* "default" = "Drupal\destinations\Form\DestinationEntityForm", | |
* "add" = "Drupal\destinations\Form\DestinationEntityForm", | |
* "edit" = "Drupal\destinations\Form\DestinationEntityForm", | |
* "delete" = "Drupal\destinations\Form\DestinationEntityDeleteForm", | |
* }, | |
* "access" = "Drupal\destinations\DestinationEntityAccessControlHandler", | |
* "route_provider" = { | |
* "html" = "Drupal\destinations\DestinationEntityHtmlRouteProvider", | |
* }, | |
* }, | |
* base_table = "destination", | |
* data_table = "destination_field_data", | |
* revision_table = "destination_revision", | |
* revision_data_table = "destination_field_revision", | |
* translatable = TRUE, | |
* admin_permission = "administer destination entities", | |
* entity_keys = { | |
* "id" = "id", | |
* "revision" = "vid", | |
* "bundle" = "type", | |
* "label" = "name", | |
* "uuid" = "uuid", | |
* "uid" = "user_id", | |
* "langcode" = "langcode", | |
* "status" = "status", | |
* }, | |
* links = { | |
* "canonical" = "/destination/{destination}", | |
* "add-page" = "/destination/add", | |
* "add-form" = "/destination/add/{destination_type}", | |
* "edit-form" = "/destination/{destination}/edit", | |
* "delete-form" = "/destination/{destination}/delete", | |
* "version-history" = "/destination/{destination}/revisions", | |
* "revision" = "/destination/{destination}/revisions/{destination_revision}/view", | |
* "revision_revert" = "/destination/{destination}/revisions/{destination_revision}/revert", | |
* "translation_revert" = "/destination/{destination}/revisions/{destination_revision}/revert/{langcode}", | |
* "revision_delete" = "/destination/{destination}/revisions/{destination_revision}/delete", | |
* "collection" = "/admin/content/destinations", | |
* }, | |
* bundle_entity_type = "destination_type", | |
* field_ui_base_route = "entity.destination_type.edit_form" | |
* ) | |
*/ | |
class DestinationEntity extends RevisionableContentEntityBase implements DestinationEntityInterface { | |
use EntityChangedTrait; | |
/** | |
* {@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 preSave(EntityStorageInterface $storage) { | |
parent::preSave($storage); | |
foreach (array_keys($this->getTranslationLanguages()) as $langcode) { | |
$translation = $this->getTranslation($langcode); | |
// If no owner has been set explicitly, make the anonymous user the owner. | |
if (!$translation->getOwner()) { | |
$translation->setOwnerId(0); | |
} | |
} | |
// If no revision author has been set explicitly, make the destination owner the | |
// revision author. | |
if (!$this->getRevisionUser()) { | |
$this->setRevisionUserId($this->getOwnerId()); | |
} | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getType() { | |
return $this->bundle(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getName() { | |
return $this->get('name')->value; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function setName($name) { | |
$this->set('name', $name); | |
return $this; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getCreatedTime() { | |
return $this->get('created')->value; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function setCreatedTime($timestamp) { | |
$this->set('created', $timestamp); | |
return $this; | |
} | |
/** | |
* {@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 isPublished() { | |
return (bool) $this->getEntityKey('status'); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function setPublished($published) { | |
$this->set('status', $published ? TRUE : FALSE); | |
return $this; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getRevisionCreationTime() { | |
return $this->get('revision_timestamp')->value; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function setRevisionCreationTime($timestamp) { | |
$this->set('revision_timestamp', $timestamp); | |
return $this; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getRevisionUser() { | |
return $this->get('revision_uid')->entity; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function setRevisionUserId($uid) { | |
$this->set('revision_uid', $uid); | |
return $this; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { | |
$fields = parent::baseFieldDefinitions($entity_type); | |
$fields['user_id'] = BaseFieldDefinition::create('entity_reference') | |
->setLabel(t('Authored by')) | |
->setDescription(t('The user ID of author of the Destination entity.')) | |
->setRevisionable(TRUE) | |
->setSetting('target_type', 'user') | |
->setSetting('handler', 'default') | |
->setTranslatable(TRUE) | |
->setDisplayOptions('view', array( | |
'label' => 'hidden', | |
'type' => 'hidden', | |
'weight' => 0, | |
)) | |
->setDisplayOptions('form', array( | |
'type' => 'hidden', | |
'settings' => array( | |
'match_operator' => 'CONTAINS', | |
'size' => '60', | |
'autocomplete_type' => 'tags', | |
'placeholder' => '', | |
), | |
)); | |
$fields['name'] = BaseFieldDefinition::create('string') | |
->setLabel(t('Destination Name')) | |
->setDescription(t('The name of the Destination.')) | |
->setRevisionable(TRUE) | |
->setSettings(array( | |
'max_length' => 50, | |
'text_processing' => 0, | |
)) | |
->setDefaultValue('') | |
->setDisplayOptions('view', array( | |
'label' => 'hidden', | |
'type' => 'string', | |
'weight' => -4, | |
)) | |
->setDisplayOptions('form', array( | |
'type' => 'string_textfield', | |
'weight' => -4, | |
)); | |
$fields['status'] = BaseFieldDefinition::create('boolean') | |
->setLabel(t('Publishing status')) | |
->setDescription(t('A boolean indicating whether the Destination is published.')) | |
->setRevisionable(TRUE) | |
->setDefaultValue(TRUE); | |
$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.')); | |
$fields['revision_timestamp'] = BaseFieldDefinition::create('created') | |
->setLabel(t('Revision timestamp')) | |
->setDescription(t('The time that the current revision was created.')) | |
->setQueryable(FALSE) | |
->setRevisionable(TRUE); | |
$fields['revision_uid'] = BaseFieldDefinition::create('entity_reference') | |
->setLabel(t('Revision user ID')) | |
->setDescription(t('The user ID of the author of the current revision.')) | |
->setSetting('target_type', 'user') | |
->setQueryable(FALSE) | |
->setRevisionable(TRUE); | |
$fields['revision_translation_affected'] = BaseFieldDefinition::create('boolean') | |
->setLabel(t('Revision translation affected')) | |
->setDescription(t('Indicates if the last edit of a translation belongs to current revision.')) | |
->setReadOnly(TRUE) | |
->setRevisionable(TRUE) | |
->setTranslatable(TRUE); | |
$fields['gps'] = BaseFieldDefinition::create('geolocation') | |
->setLabel(t('GPS Coordinates')) | |
->setDescription(t('The GPS Coordinates of this Destination. You may enter address in the search field and the location will be retrieved via Google Maps.')) | |
->setRevisionable(TRUE) | |
->setDisplayOptions('view', array( | |
'label' => 'above', | |
'type' => 'geolocation_map', | |
'weight' => 1, | |
)) | |
->setDisplayOptions('form', array( | |
'type' => 'geolocation_googlegeocoder', | |
'weight' => 1, | |
'title' => t('title thing'), | |
'settings' => array( | |
'set_marker' => '1', | |
'info_text' => t('Some info text'), | |
'use_overridden_map_settings' => 0, | |
'google_map_settings' => array( | |
'type' => 'TERRAIN', | |
'zoom' => 6, | |
'mapTypeControl' => TRUE, | |
'streetViewControl' => FALSE, | |
'zoomControl' => TRUE, | |
'scrollwheel' => FALSE, | |
'disableDoubleClickZoom' => FALSE, | |
'draggable' => TRUE, | |
'height' => '300px', | |
'width' => '100%', | |
'info_auto_display' => TRUE, | |
'disableAutoPan' => TRUE, | |
'preferScrollingToZooming' => FALSE, | |
'gestureHandling' => 'auto', | |
), | |
'populate_address_field' => TRUE, | |
'target_address_field' => 'address', | |
'default_longitude' => -1.82545, | |
'default_latitude' => 53.9246, | |
'auto_client_location' => TRUE, | |
'auto_client_location_marker' => FALSE, | |
'allow_override_map_settings' => FALSE, | |
), | |
)); | |
$fields['address'] = BaseFieldDefinition::create('address') | |
->setLabel(t('Address')) | |
->setDescription(t('The address of this Destination, if known. Choosing a location on the map above will attempt to fill this out using the Google Maps API. You might know better!')) | |
->setRevisionable(TRUE) | |
->setDisplayOptions('view', array( | |
'label' => 'hidden', | |
'type' => 'address_default', | |
'weight' => 2, | |
)) | |
->setDisplayOptions('form', array( | |
'type' => 'address_default', | |
'weight' => 2, | |
)) | |
->setSettings(array( | |
'fields' => array( | |
'givenName' => FALSE, | |
'additionalName' => FALSE, | |
'familyName' => FALSE, | |
'organization' => FALSE, | |
'addressLine1' => 'addressLine1', | |
'addressLine2' => 'addressLine2', | |
'locality' => 'locality', | |
'dependentLocality' => 'dependentLocality', | |
'administrativeArea' => 'administrativeArea', | |
'postalCode' => 'postalCode', | |
'sortingCode' => 'sortingCode', | |
), | |
)); | |
return $fields; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment