Commit 06624302 authored by woprrr's avatar woprrr Committed by woprrr

Issue #2800739 by woprrr, matiasmiranda, Berdir, mbovan: Improve findCrop() performance

parent 9eb652f3
......@@ -5,8 +5,29 @@ namespace Drupal\crop;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
/**
* Image crop storage class.
* Defines the storage handler class for image crop storage.
*
* This extends the Drupal\Core\Entity\Sql\SqlContentEntityStorage class,
* adding required special handling for comment entities.
*/
class CropStorage extends SqlContentEntityStorage implements CropStorageInterface {
/**
* {@inheritdoc}
*/
public function getCrop($uri, $type) {
$query = $this->database->select('crop_field_data', 'cfd');
$query->addField('cfd', 'cid');
$query->condition('cfd.uri', $uri, 'LIKE');
if ($type) {
$query->condition('cfd.type', $type);
}
$query->range(0, 1);
$cid = $query->execute()->fetchField();
return $cid ? $this->load($cid) : NULL;
}
}
......@@ -11,4 +11,17 @@ use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
*/
interface CropStorageInterface extends SqlEntityStorageInterface, DynamicallyFieldableEntityStorageSchemaInterface, EntityBundleListenerInterface {
/**
* Retrieve crop ID based on image URI and crop type.
*
* @param string $uri
* URI of the image.
* @param string $type
* Crop type.
*
* @return \Drupal\crop\CropInterface|null
* A Crop object or NULL if nothing matches the search parameters.
*/
public function getCrop($uri, $type);
}
......@@ -129,16 +129,7 @@ class Crop extends ContentEntityBase implements CropInterface {
* {@inheritdoc}
*/
public static function findCrop($uri, $type) {
$query = \Drupal::entityQuery('crop')
->condition('uri', $uri);
if ($type) {
$query->condition('type', $type);
}
$crop = $query->sort('cid')
->range(0, 1)
->execute();
return $crop ? \Drupal::entityTypeManager()->getStorage('crop')->load(current($crop)) : NULL;
return \Drupal::entityTypeManager()->getStorage('crop')->getCrop($uri, $type);
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment