View source
<?php
namespace Drupal\crop\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\crop\CropInterface;
use Drupal\crop\EntityProviderNotFoundException;
use Drupal\image\ImageStyleInterface;
class Crop extends ContentEntityBase implements CropInterface {
public function position() {
return [
'x' => (int) $this->x->value,
'y' => (int) $this->y->value,
];
}
public function setPosition($x, $y) {
$this->x = $x;
$this->y = $y;
return $this;
}
public function anchor() {
return [
'x' => (int) ($this->x->value - $this->width->value / 2),
'y' => (int) ($this->y->value - $this->height->value / 2),
];
}
public function size() {
return [
'width' => (int) $this->width->value,
'height' => (int) $this->height->value,
];
}
public function setSize($width, $height) {
$this->width = $width;
$this->height = $height;
return $this;
}
public function provider() {
$plugin_manager = \Drupal::service('plugin.manager.crop.entity_provider');
if (!$plugin_manager
->hasDefinition($this->entity_type->value)) {
throw new EntityProviderNotFoundException(t('Entity provider @id not found.', [
'@id' => $this->entity_type->value,
]));
}
return $plugin_manager
->createInstance($this->entity_type->value);
}
public static function cropExists($uri, $type = NULL) {
$query = \Drupal::entityQuery('crop')
->condition('uri', $uri);
if ($type) {
$query
->condition('type', $type);
}
return (bool) $query
->execute();
}
public static function findCrop($uri, $type) {
return \Drupal::entityTypeManager()
->getStorage('crop')
->getCrop($uri, $type);
}
public static function getCropFromImageStyle($uri, ImageStyleInterface $image_style) {
$effects = [];
$crop = FALSE;
foreach ($image_style
->getEffects() as $uuid => $effect) {
$effects[$effect
->getPluginId()] = [
'uuid' => $uuid,
'provider' => $effect
->getPluginDefinition()['provider'],
];
}
if (isset($effects['crop_crop']) && $image_style
->getEffects()
->has($effects['crop_crop']['uuid'])) {
$type = $image_style
->getEffect($effects['crop_crop']['uuid'])
->getConfiguration()['data']['crop_type'];
$crop = self::findCrop($uri, $type);
}
if (!$crop) {
foreach ($effects as $effect) {
$provider = $effect['provider'];
$crop = self::findCrop($uri, $provider);
}
}
return $crop;
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
if (!$this
->get('revision_uid')->entity) {
$this
->set('revision_uid', \Drupal::currentUser()
->id());
}
if (empty($this->uri->value) && !empty($this->entity_type->value) && !empty($this->entity_id->value)) {
$entity = \Drupal::entityTypeManager()
->getStorage($this->entity_type->value)
->load($this->entity_id->value);
if ($uri = $this
->provider()
->uri($entity)) {
$this
->set('uri', $uri);
}
}
}
public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) {
parent::preSaveRevision($storage, $record);
if (!$this
->isNewRevision() && isset($this->original) && (!isset($record->revision_log) || $record->revision_log === '')) {
$record->revision_log = $this->original->revision_log->value;
}
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
$flush_derivative_images = \Drupal::config('crop.settings')
->get('flush_derivative_images');
if ($flush_derivative_images) {
image_path_flush($this->uri->value);
}
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = [];
$fields['cid'] = BaseFieldDefinition::create('integer')
->setLabel(t('Crop ID'))
->setDescription(t('The crop ID.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The crop UUID.'))
->setReadOnly(TRUE);
$fields['vid'] = BaseFieldDefinition::create('integer')
->setLabel(t('Revision ID'))
->setDescription(t('The crop revision ID.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['type'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Type'))
->setDescription(t('The crop type.'))
->setSetting('target_type', 'crop_type')
->setReadOnly(TRUE);
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language code'))
->setDescription(t('The node language code.'))
->setRevisionable(TRUE);
$fields['entity_id'] = BaseFieldDefinition::create('integer')
->setLabel(t('Entity ID'))
->setDescription(t('ID of entity crop belongs to.'))
->setSetting('unsigned', TRUE)
->setRevisionable(TRUE)
->setReadOnly(TRUE);
$fields['entity_type'] = BaseFieldDefinition::create('string')
->setLabel(t('Entity type'))
->setDescription(t('The type of entity crop belongs to.'))
->setRevisionable(TRUE)
->setReadOnly(TRUE);
$fields['uri'] = BaseFieldDefinition::create('uri')
->setLabel(t('URI'))
->setDescription(t('The URI of the image crop belongs to.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setSetting('max_length', 255);
$fields['height'] = BaseFieldDefinition::create('integer')
->setLabel(t('Height'))
->setDescription(t('The crop height.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['width'] = BaseFieldDefinition::create('integer')
->setLabel(t('Width'))
->setDescription(t('The crop width.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['x'] = BaseFieldDefinition::create('integer')
->setLabel(t('X coordinate'))
->setDescription(t("The crop's X coordinate."))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['y'] = BaseFieldDefinition::create('integer')
->setLabel(t('Y coordinate'))
->setDescription(t("The crop's Y coordinate."))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['revision_timestamp'] = BaseFieldDefinition::create('created')
->setLabel(t('Revision timestamp'))
->setDescription(t('The time that the current revision was created.'))
->setRevisionable(TRUE);
$fields['revision_uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Revision author ID'))
->setDescription(t('The user ID of the author of the current revision.'))
->setSetting('target_type', 'user')
->setRevisionable(TRUE);
$fields['revision_log'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Revision Log'))
->setDescription(t('The log entry explaining the changes in this revision.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE);
return $fields;
}
}