Issue #2321901 by JeroenT, epari.siva, unstatu, Temoor, lokapujya, LinL, pcambra, piyuesh23: Replace all instances of entity_load('image_style') and entity_load_multiple('image_style') with static method calls

8.0.x
Alex Pott 2015-04-14 16:21:18 +02:00
parent dba95189d1
commit 1f9399f690
10 changed files with 54 additions and 25 deletions

View File

@ -11,6 +11,7 @@ use Drupal\field\Entity\FieldConfig;
use Drupal\file\Entity\File;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\field\FieldConfigInterface;
use Drupal\image\Entity\ImageStyle;
/**
* Image style constant for user presets in the database.
@ -213,7 +214,7 @@ function image_file_predelete(File $file) {
* The Drupal file path to the original image.
*/
function image_path_flush($path) {
$styles = entity_load_multiple('image_style');
$styles = ImageStyle::loadMultiple();
foreach ($styles as $style) {
$style->flush($path);
}
@ -228,7 +229,7 @@ function image_path_flush($path) {
* Array of image styles both key and value are set to style name.
*/
function image_style_options($include_empty = TRUE) {
$styles = entity_load_multiple('image_style');
$styles = ImageStyle::loadMultiple();
$options = array();
if ($include_empty && !empty($styles)) {
$options[''] = t('- None -');
@ -270,7 +271,7 @@ function image_style_options($include_empty = TRUE) {
* - attributes: Associative array of attributes to be placed in the img tag.
*/
function template_preprocess_image_style(&$variables) {
$style = entity_load('image_style', $variables['style_name']);
$style = ImageStyle::load($variables['style_name']);
// Determine the dimensions of the styled image.
$dimensions = array(

View File

@ -7,6 +7,7 @@
namespace Drupal\image\Plugin\Field\FieldFormatter;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
@ -44,6 +45,13 @@ class ImageFormatter extends ImageFormatterBase implements ContainerFactoryPlugi
*/
protected $linkGenerator;
/**
* The image style entity storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $imageStyleStorage;
/**
* Constructs an ImageFormatter object.
*
@ -66,10 +74,11 @@ class ImageFormatter extends ImageFormatterBase implements ContainerFactoryPlugi
* @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator
* The link generator service.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, LinkGeneratorInterface $link_generator) {
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, LinkGeneratorInterface $link_generator, EntityStorageInterface $image_style_storage) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->currentUser = $current_user;
$this->linkGenerator = $link_generator;
$this->imageStyleStorage = $image_style_storage;
}
/**
@ -85,7 +94,8 @@ class ImageFormatter extends ImageFormatterBase implements ContainerFactoryPlugi
$configuration['view_mode'],
$configuration['third_party_settings'],
$container->get('current_user'),
$container->get('link_generator')
$container->get('link_generator'),
$container->get('entity.manager')->getStorage('image_style')
);
}
@ -192,7 +202,7 @@ class ImageFormatter extends ImageFormatterBase implements ContainerFactoryPlugi
// Collect cache tags to be added for each item in the field.
$cache_tags = array();
if (!empty($image_style_setting)) {
$image_style = entity_load('image_style', $image_style_setting);
$image_style = $this->imageStyleStorage->load($image_style_setting);
$cache_tags = $image_style->getCacheTags();
}

View File

@ -8,6 +8,7 @@
namespace Drupal\image\Tests;
use Drupal\simpletest\WebTestBase;
use Drupal\image\Entity\ImageStyle;
/**
* Tests the file move function for images and image styles.
@ -31,7 +32,7 @@ class FileMoveTest extends WebTestBase {
$file = entity_create('file', (array) current($this->drupalGetTestFiles('image')));
// Create derivative image.
$styles = entity_load_multiple('image_style');
$styles = ImageStyle::loadMultiple();
$style = reset($styles);
$original_uri = $file->getFileUri();
$derivative_uri = $style->buildUri($original_uri);

View File

@ -8,6 +8,7 @@
namespace Drupal\image\Tests;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\image\Entity\ImageStyle;
use Drupal\image\ImageStyleInterface;
use Drupal\node\Entity\Node;
@ -129,7 +130,7 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
}
// Load the saved image style.
$style = entity_load('image_style', $style_name);
$style = ImageStyle::load($style_name);
// Ensure that third party settings were added to the config entity.
// These are added by a hook_image_style_presave() implemented in
@ -211,7 +212,7 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
$this->assertEqual($this->getImageCount($style), 0, format_string('Image style %style was flushed after renaming the style and updating the order of effects.', array('%style' => $style->label())));
// Load the style by the new name with the new weights.
$style = entity_load('image_style', $style_name);
$style = ImageStyle::load($style_name);
// Confirm the new style order was saved.
$effect_edits_order = array_reverse($effect_edits_order);
@ -269,7 +270,7 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
$directory = file_default_scheme() . '://styles/' . $style_name;
$this->assertFalse(is_dir($directory), format_string('Image style %style directory removed on style deletion.', array('%style' => $style->label())));
$this->assertFalse(entity_load('image_style', $style_name), format_string('Image style %style successfully deleted.', array('%style' => $style->label())));
$this->assertFalse(ImageStyle::load($style_name), format_string('Image style %style successfully deleted.', array('%style' => $style->label())));
}
@ -319,7 +320,7 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
$this->drupalGet('node/' . $nid);
// Reload the image style using the new name.
$style = entity_load('image_style', $new_style_name);
$style = ImageStyle::load($new_style_name);
$this->assertRaw($style->buildUrl($original_uri), 'Image displayed using style replacement style.');
// Delete the style and choose a replacement style.
@ -330,7 +331,7 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
$message = t('The image style %name has been deleted.', array('%name' => $new_style_label));
$this->assertRaw($message);
$replacement_style = entity_load('image_style', 'thumbnail');
$replacement_style = ImageStyle::load('thumbnail');
$this->drupalGet('node/' . $nid);
$this->assertRaw($replacement_style->buildUrl($original_uri), 'Image displayed using style replacement style.');
}
@ -448,7 +449,7 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
$staging->delete('image.style.' . $style_name);
$this->configImporter()->import();
$this->assertFalse(entity_load('image_style', $style_name), 'Style deleted after config import.');
$this->assertFalse(ImageStyle::load($style_name), 'Style deleted after config import.');
$this->assertEqual($this->getImageCount($style), 0, 'Image style was flushed after being deleted by config import.');
}

View File

@ -10,6 +10,7 @@ namespace Drupal\image\Tests;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\user\RoleInterface;
use Drupal\image\Entity\ImageStyle;
/**
* Tests the display of image fields.
@ -175,7 +176,7 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
// Ensure the derivative image is generated so we do not have to deal with
// image style callback paths.
$this->drupalGet(entity_load('image_style', 'thumbnail')->buildUrl($image_uri));
$this->drupalGet(ImageStyle::load('thumbnail')->buildUrl($image_uri));
$image_style = array(
'#theme' => 'image_style',
'#uri' => $image_uri,
@ -193,7 +194,7 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
if ($scheme == 'private') {
// Log out and try to access the file.
$this->drupalLogout();
$this->drupalGet(entity_load('image_style', 'thumbnail')->buildUrl($image_uri));
$this->drupalGet(ImageStyle::load('thumbnail')->buildUrl($image_uri));
$this->assertResponse('403', 'Access denied to image style thumbnail as anonymous user.');
}
}

View File

@ -7,6 +7,8 @@
namespace Drupal\image\Tests;
use Drupal\image\Entity\ImageStyle;
/**
* Tests flushing of image styles.
*
@ -79,7 +81,7 @@ class ImageStyleFlushTest extends ImageFieldTestBase {
}
// Load the saved image style.
$style = entity_load('image_style', $style_name);
$style = ImageStyle::load($style_name);
// Create an image for the 'public' wrapper.
$image_path = $this->createSampleImage($style, 'public');

View File

@ -7,6 +7,7 @@
namespace Drupal\rdf\Tests;
use Drupal\image\Entity\ImageStyle;
use Drupal\image\Tests\ImageFieldTestBase;
use Drupal\node\Entity\Node;
@ -96,7 +97,7 @@ class ImageFieldAttributesTest extends ImageFieldTestBase {
// Construct the node and image URIs for testing.
$node_uri = $this->node->url('canonical', ['absolute' => TRUE]);
$image_uri = entity_load('image_style', 'medium')->buildUrl($this->file->getFileUri());
$image_uri = ImageStyle::load('medium')->buildUrl($this->file->getFileUri());
// Test relations from node to image.
$expected_value = array(

View File

@ -8,9 +8,10 @@
namespace Drupal\rdf\Tests;
use Drupal\Core\Url;
use Drupal\image\Entity\ImageStyle;
use Drupal\node\Entity\NodeType;
use Drupal\node\NodeInterface;
use Drupal\simpletest\WebTestBase;
use Drupal\node\Entity\NodeType;
/**
* Tests the RDF mappings and RDFa markup of the standard profile.
@ -165,7 +166,7 @@ class StandardProfileTest extends WebTestBase {
// Set URIs.
// Image.
$image_file = $this->article->get('field_image')->entity;
$this->imageUri = entity_load('image_style', 'large')->buildUrl($image_file->getFileUri());
$this->imageUri = ImageStyle::load('large')->buildUrl($image_file->getFileUri());
// Term.
$this->termUri = $this->term->url('canonical', array('absolute' => TRUE));
// Article.
@ -224,7 +225,7 @@ class StandardProfileTest extends WebTestBase {
// @todo Once the image points to the original instead of the processed
// image, move this to testArticleProperties().
$image_file = $this->article->get('field_image')->entity;
$image_uri = entity_load('image_style', 'medium')->buildUrl($image_file->getFileUri());
$image_uri = ImageStyle::load('medium')->buildUrl($image_file->getFileUri());
$expected_value = array(
'type' => 'uri',
'value' => $image_uri,

View File

@ -16,7 +16,6 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatterBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\image\Entity\ImageStyle;
/**
* Plugin for responsive image formatter.
@ -36,6 +35,13 @@ class ResponsiveImageFormatter extends ImageFormatterBase implements ContainerFa
*/
protected $responsiveImageStyleStorage;
/*
* The image style entity storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $imageStyleStorage;
/**
* Constructs a ResponsiveImageFormatter object.
*
@ -55,11 +61,14 @@ class ResponsiveImageFormatter extends ImageFormatterBase implements ContainerFa
* Any third party settings.
* @param \Drupal\Core\Entity\EntityStorageInterface $responsive_image_style_storage
* The responsive image style storage.
* @param \Drupal\Core\Entity\EntityStorageInterface $image_style_storage
* The image style storage.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityStorageInterface $responsive_image_style_storage) {
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityStorageInterface $responsive_image_style_storage, EntityStorageInterface $image_style_storage) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->responsiveImageStyleStorage = $responsive_image_style_storage;
$this->imageStyleStorage = $image_style_storage;
}
/**
@ -74,7 +83,8 @@ class ResponsiveImageFormatter extends ImageFormatterBase implements ContainerFa
$configuration['label'],
$configuration['view_mode'],
$configuration['third_party_settings'],
$container->get('entity.manager')->getStorage('responsive_image_style')
$container->get('entity.manager')->getStorage('responsive_image_style'),
$container->get('entity.manager')->getStorage('image_style')
);
}
@ -222,7 +232,7 @@ class ResponsiveImageFormatter extends ImageFormatterBase implements ContainerFa
// selected for the smallest screen.
$fallback_image_style = end($image_styles_to_load);
}
$image_styles = ImageStyle::loadMultiple($image_styles_to_load);
$image_styles = $this->imageStyleStorage->loadMultiple($image_styles_to_load);
foreach ($image_styles as $image_style) {
$cache_tags = Cache::mergeTags($cache_tags, $image_style->getCacheTags());
}

View File

@ -8,6 +8,7 @@
namespace Drupal\system\Tests\Entity;
use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
use Drupal\image\Entity\ImageStyle;
use Drupal\simpletest\WebTestBase;
/**
@ -123,7 +124,7 @@ class ConfigEntityImportTest extends WebTestBase {
$name = 'image.style.thumbnail';
/** @var $entity \Drupal\image\Entity\ImageStyle */
$entity = entity_load('image_style', 'thumbnail');
$entity = ImageStyle::load('thumbnail');
$plugin_collection = $entity->getPluginCollections()['effects'];
$effects = $entity->get('effects');