View source
<?php
namespace Drupal\photos\Entity;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EditorialContentEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\media\Entity\Media;
use Drupal\photos\PhotosAlbum;
use Drupal\photos\PhotosImageInterface;
use Drupal\user\EntityOwnerTrait;
class PhotosImage extends EditorialContentEntityBase implements PhotosImageInterface {
use EntityOwnerTrait;
use StringTranslationTrait;
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
foreach (array_keys($this
->getTranslationLanguages()) as $langcode) {
$translation = $this
->getTranslation($langcode);
if (!$translation
->getOwner()) {
$translation
->setOwnerId(0);
}
}
if (!$this
->getRevisionUser()) {
$this
->setRevisionUserId($this
->getOwnerId());
}
}
public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) {
parent::preSaveRevision($storage, $record);
$is_new_revision = $this
->isNewRevision();
if (!$is_new_revision && isset($this->original) && empty($record->revision_log_message)) {
$record->revision_log_message = $this->original->revision_log_message->value;
}
if ($is_new_revision) {
$record->revision_created = self::getRequestTime();
}
}
public static function preDelete(EntityStorageInterface $storage, array $entities) {
parent::preDelete($storage, $entities);
foreach ($entities as $entity) {
$album_id = $entity
->getAlbumId();
Cache::invalidateTags([
'node:' . $album_id,
'photos:album:' . $album_id,
]);
Cache::invalidateTags([
'photos:image:' . $entity
->id(),
]);
}
}
public function getFids() {
$fids = [];
$photosImageFields = \Drupal::service('entity_field.manager')
->getFieldDefinitions('photos_image', 'photos_image');
foreach ($photosImageFields as $key => $field) {
$fieldType = $field
->getType();
if ($fieldType == 'file' || $fieldType == 'image') {
foreach ($this->{$key} as $item) {
$fids[$item->entity
->id()] = $item->entity
->id();
}
}
elseif ($fieldType == 'entity_reference') {
$settings = $field
->getSettings();
if ($settings['target_type'] == 'media') {
foreach ($this->{$key} as $item) {
$media = Media::load($item->entity
->id());
$fid = $media
->getSource()
->getSourceFieldValue($media);
$fids[$fid] = $fid;
}
}
}
}
return $fids;
}
public function getTitle() {
return $this
->get('title')->value;
}
public function setTitle($title) {
$this
->set('title', $title);
return $this;
}
public function getDescription() {
return $this
->get('description')->value;
}
public function setDescription($description) {
$this
->set('description', $description);
return $this;
}
public function getFormat() {
return $this
->get('description')->format;
}
public function setFormat($format) {
$this
->get('description')->format = $format;
return $this;
}
public function getAlbumId() {
return $this
->get('album_id')->target_id;
}
public function setAlbumId($album_id) {
$this
->set('album_id', $album_id);
return $this;
}
public function getAlbumUrl() {
$album_link_override = \Drupal::config('photos.settings')
->get('album_link_override');
if ($album_link_override) {
$album_link_override = str_replace(':', '.', $album_link_override);
$url = Url::fromRoute('view.' . $album_link_override, [
'node' => $this
->getAlbumId(),
]);
}
else {
$url = Url::fromRoute('entity.node.canonical', [
'node' => $this
->getAlbumId(),
]);
}
return $url;
}
public function getWeight() {
return $this
->get('weight')->value;
}
public function setWeight($weight) {
$this
->set('weight', $weight);
return $this;
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function setCreatedTime($timestamp) {
$this
->set('created', $timestamp);
return $this;
}
protected function urlRouteParameters($rel) {
$uri_route_parameters = [];
if (!in_array($rel, [
'collection',
'add-page',
'add-form',
], TRUE)) {
$uri_route_parameters[$this
->getEntityTypeId()] = $this
->id();
$uri_route_parameters['node'] = $this
->getAlbumId();
}
if ($rel === 'add-form' && $this
->getEntityType()
->hasKey('bundle')) {
$parameter_name = $this
->getEntityType()
->getBundleEntityType() ?: $this
->getEntityType()
->getKey('bundle');
$uri_route_parameters[$parameter_name] = $this
->bundle();
}
if ($rel === 'revision' && $this instanceof RevisionableInterface) {
$uri_route_parameters[$this
->getEntityTypeId() . '_revision'] = $this
->getRevisionId();
}
return $uri_route_parameters;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields += static::ownerBaseFieldDefinitions($entity_type);
$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(t('Title'))
->setDescription(t('The image title.'))
->setRequired(TRUE)
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE);
$fields['description'] = BaseFieldDefinition::create('text_long')
->setLabel(t('Description'))
->setDescription(t('The image description field.'))
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'text_default',
'weight' => 1,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('form', [
'type' => 'text_textfield',
'weight' => 1,
])
->setDisplayConfigurable('form', TRUE);
$fields['uid']
->setLabel(t('Authored by'))
->setDescription(t('The username of the author.'))
->setRevisionable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'weight' => -3,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 4,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Authored on'))
->setDescription(t('The time that the image was created.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'timestamp',
'weight' => -2,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('form', [
'type' => 'datetime_timestamp',
'weight' => 5,
])
->setDisplayConfigurable('form', TRUE);
$fields['album_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Album ID'))
->setDescription(t('The album node ID.'))
->setRequired(TRUE)
->setRevisionable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'entity_reference_label',
'weight' => -1,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 3,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'placeholder' => '',
],
])
->setSetting('target_type', 'node')
->setDisplayConfigurable('form', TRUE);
$fields['status']
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => TRUE,
],
'weight' => 120,
])
->setDisplayConfigurable('form', TRUE);
$fields['weight'] = BaseFieldDefinition::create('integer')
->setLabel(t('Weight'))
->setDescription(t('The image weight for custom sort order.'))
->setDefaultValue(0)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'number_integer',
])
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('form', [
'type' => 'number',
'weight' => 20,
])
->setDisplayConfigurable('form', TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the image was last edited.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE);
return $fields;
}
public static function getRequestTime() {
return \Drupal::time()
->getRequestTime();
}
public function getPager($id, $type = 'album_id') {
$entity_id = $this
->id();
$db = \Drupal::database();
$query = $db
->select('photos_image_field_data', 'p');
$query
->innerJoin('node_field_data', 'n', 'n.nid = p.album_id');
$query
->fields('p', [
'id',
'album_id',
]);
$query
->fields('n', [
'title',
]);
$order = [
'column' => 'p.id',
'sort' => 'DESC',
];
if ($type == 'album_id') {
$album_data = $db
->query('SELECT data FROM {photos_album} WHERE album_id = :album_id', [
':album_id' => $id,
])
->fetchField();
$album_data = unserialize($album_data);
$default_order = \Drupal::config('photos.settings')
->get('photos_display_imageorder');
$image_order = isset($album_data['imageorder']) ? $album_data['imageorder'] : $default_order;
$order = explode('|', $image_order);
$order = PhotosAlbum::orderValueChange($order[0], $order[1]);
$query
->condition('p.album_id', $id);
}
elseif ($type == 'uid') {
$query
->condition('p.uid', $id);
}
$query
->orderBy($order['column'], $order['sort']);
if ($order['column'] != 'p.id') {
$query
->orderBy('p.id', 'DESC');
}
$results = $query
->execute();
$stop = $pager['prev'] = $pager['next'] = 0;
$num = 0;
$previousImageId = NULL;
$photosImageStorage = \Drupal::entityTypeManager()
->getStorage('photos_image');
$photosImageViewBuilder = \Drupal::entityTypeManager()
->getViewBuilder('photos_image');
foreach ($results as $result) {
$num++;
if ($stop == 1) {
$photosImage = $photosImageStorage
->load($result->id);
$image_view = $photosImageViewBuilder
->view($photosImage, 'pager');
$pager['nextView'] = $image_view;
$pager['nextUrl'] = Url::fromRoute('entity.photos_image.canonical', [
'node' => $result->album_id,
'photos_image' => $photosImage
->id(),
])
->toString();
break;
}
if ($result->id == $entity_id) {
$photosImage = $photosImageStorage
->load($result->id);
$image_view = $photosImageViewBuilder
->view($photosImage, 'pager');
$pager['currentView'] = $image_view;
$stop = 1;
}
else {
$previousImageId = $result->id;
}
$pager['albumTitle'] = $result->title;
}
if ($previousImageId) {
$photosImage = $photosImageStorage
->load($previousImageId);
$image_view = $photosImageViewBuilder
->view($photosImage, 'pager');
$pager['prevView'] = $image_view;
$pager['prevUrl'] = Url::fromRoute('entity.photos_image.canonical', [
'node' => $id,
'photos_image' => $photosImage
->id(),
])
->toString();
}
return $pager;
}
}