diff --git a/image_replace.install b/image_replace.install index 8772f89..68506ad 100644 --- a/image_replace.install +++ b/image_replace.install @@ -85,7 +85,6 @@ function image_replace_update_8102(&$sandbox) { ->fetchAll(); $schema = $database->schema(); - $schema->dropPrimaryKey('image_replace'); $schema->addField('image_replace', 'target_uri_hash', [ 'type' => 'varchar', 'length' => '64', @@ -104,6 +103,7 @@ function image_replace_update_8102(&$sandbox) { $image_replace_storage->add($data->target_style, $data->target_uri, $data->replacement_uri); } + $schema->dropPrimaryKey('image_replace'); $schema->dropField('image_replace', 'target_uri'); $schema->addPrimaryKey('image_replace', ['target_style', 'target_uri_hash']); } diff --git a/image_replace.module b/image_replace.module index 7d8bfe0..5eb201e 100644 --- a/image_replace.module +++ b/image_replace.module @@ -5,9 +5,12 @@ * Provides an image style effect replacing the whole image with another one. */ +use Drupal\Component\Utility\Html; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityInterface; +use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; +use Drupal\image\Entity\ImageStyle; use Drupal\image\ImageStyleInterface; /** @@ -29,15 +32,13 @@ function image_replace_form_field_config_edit_form_alter(&$form, &$form_state, $ '#element_validate' => ['image_replace_form_field_config_edit_form_element_validate'], ]; - $helper_service = \Drupal::service('image_replace.helper'); - $available_image_fields = $helper_service->getImageFields($field->getTargetEntityTypeId(), $field->getTargetBundle()); - $image_field_options = array_map(function ($replacement_field) { + $image_field_options = array_map(function($replacement_field) { return $replacement_field->label(); - }, $available_image_fields); + }, _image_replace_image_fields($field->getTargetEntityTypeId(), $field->getTargetBundle())); unset($image_field_options[$field->getName()]); $image_style_map = $field->getThirdPartySetting('image_replace', 'image_style_map', []); - foreach ($helper_service->getStyleOptions() as $image_style => $label) { + foreach (_image_replace_style_options() as $image_style => $label) { $default_value = isset($image_style_map[$image_style]['source_field']) ? $image_style_map[$image_style]['source_field'] : NULL; $form['third_party_settings']['image_replace']['image_style_map'][$image_style]['source_field'] = [ '#type' => 'select', @@ -67,7 +68,7 @@ function image_replace_form_field_config_edit_form_element_validate($element, &$ if ($field_storage->hasData()) { $changed = FALSE; - foreach (array_keys(\Drupal::service('image_replace.helper')->getStyleOptions()) as $image_style) { + foreach (array_keys(_image_replace_style_options()) as $image_style) { $current_value = $element['image_style_map'][$image_style]['source_field']['#default_value']; $new_value = $element['image_style_map'][$image_style]['source_field']['#value']; if (!(empty($current_value) && empty($new_value)) && $current_value != $new_value) { @@ -99,11 +100,8 @@ function image_replace_entity_presave(EntityInterface $entity) { $involved_fields = []; $target_map = []; - $helper_service = \Drupal::service('image_replace.helper'); - $available_image_fields = $helper_service->getImageFields($entity->getEntityTypeId(), $entity->bundle()); - foreach ($available_image_fields as $target_field => $field) { - $source_map = $field->getThirdPartySetting('image_replace', 'image_style_map', []); - if ($field && $source_map) { + foreach (_image_replace_image_fields($entity->getEntityTypeId(), $entity->bundle()) as $target_field => $field) { + if ($field && $source_map = $field->getThirdPartySetting('image_replace', 'image_style_map', [])) { $involved_fields[$target_field] = $target_field; foreach ($source_map as $target_style => $record) { $source_field = $record['source_field']; @@ -160,3 +158,49 @@ function image_replace_image_style_presave(ImageStyleInterface $image_style) { } $image_style->set('effects', $effects); } + +/** + * Collect info for all image field instances on a given entity_type/bundle. + * + * @param string $entity_type + * The entity type, e.g. node, for which the info shall be returned. + * @param string $bundle + * The bundle name for which to return instances. + * + * @return array + * An associative array of instance arrays keyed by the field name. + */ +function _image_replace_image_fields($entity_type, $bundle) { + $image_fields = []; + + $field_definitions = \Drupal::service('entity_field.manager') + ->getFieldDefinitions($entity_type, $bundle); + foreach ($field_definitions as $field_name => $field_definition) { + if ($field_definition->getType() == 'image') { + $image_fields[$field_name] = FieldConfig::loadByName( + $entity_type, $bundle, $field_name); + } + } + + return $image_fields; +} + +/** + * Return a list of image replace enabled style => label map. + * + * @return array + * A key-value list where keys are style names and values are style labels of + * image styles having a replace effect configured. + */ +function _image_replace_style_options() { + $styles = ImageStyle::loadMultiple(); + $result = []; + foreach ($styles as $style_name => $style) { + foreach ($style->getEffects() as $effect) { + if ($effect->getPluginId() == 'image_replace') { + $result[$style_name] = Html::escape($style->get('label')); + } + } + } + return $result; +} diff --git a/image_replace.services.yml b/image_replace.services.yml index 92d22f2..bf4b52d 100644 --- a/image_replace.services.yml +++ b/image_replace.services.yml @@ -1,7 +1,4 @@ services: image_replace.storage: - class: Drupal\image_replace\ImageReplaceStorage + class: Drupal\image_replace\ImageReplaceDatabaseStorage arguments: ['@database'] - image_replace.helper: - class: Drupal\image_replace\ImageReplaceHelper - arguments: ['@entity_field.manager'] diff --git a/src/ImageReplaceStorage.php b/src/ImageReplaceDatabaseStorage.php similarity index 58% rename from src/ImageReplaceStorage.php rename to src/ImageReplaceDatabaseStorage.php index 0f6928f..ecf44d0 100644 --- a/src/ImageReplaceStorage.php +++ b/src/ImageReplaceDatabaseStorage.php @@ -5,9 +5,9 @@ namespace Drupal\image_replace; use Drupal\Core\Database\Connection; /** - * Defines a class for image_replace storage operations. + * Defines a class for image_replace database storage operations. */ -class ImageReplaceStorage { +class ImageReplaceDatabaseStorage implements ImageReplaceStorageInterface { /** * Active database connection. @@ -17,7 +17,7 @@ class ImageReplaceStorage { protected $database; /** - * Constructs a ImageReplaceStorage object. + * Constructs a ImageReplaceDatabaseStorage object. * * @param \Drupal\Core\Database\Connection $database * The database connection to be used. @@ -27,16 +27,7 @@ class ImageReplaceStorage { } /** - * Determine replacement image uri for the given original filename. - * - * @param string $target_style - * The target image style name. - * @param string $target_uri - * The uri of the image for which to find a replacement. - * - * @return string|null - * The replacement uri when a mapping for the given uri/style combination - * exists. + * {@inheritdoc} */ public function get($target_style, $target_uri) { $target_uri_hash = hash('sha256', $target_uri); @@ -49,14 +40,7 @@ class ImageReplaceStorage { } /** - * Add an image replacement mapping. - * - * @param string $target_style - * The target image style name. - * @param string $target_uri - * The uri of the image for which to set a replacement. - * @param string $replacement_uri - * The replacement uri to set for the given uri/style combination. + * {@inheritdoc} */ public function add($target_style, $target_uri, $replacement_uri) { $target_uri_hash = hash('sha256', $target_uri); @@ -70,12 +54,7 @@ class ImageReplaceStorage { } /** - * Remove the given image replacement mapping if it exists. - * - * @param string $target_style - * The target image style name. - * @param string $target_uri - * The uri of the image for which to remove the replacement. + * {@inheritdoc} */ public function remove($target_style, $target_uri) { $target_uri_hash = hash('sha256', $target_uri); diff --git a/src/ImageReplaceHelper.php b/src/ImageReplaceHelper.php deleted file mode 100644 index 970b9ad..0000000 --- a/src/ImageReplaceHelper.php +++ /dev/null @@ -1,76 +0,0 @@ -entityFieldManager = $entity_field_manager; - } - - /** - * Collect info for all image field instances on a given entity_type/bundle. - * - * @param string $entity_type - * The entity type, e.g. node, for which the info shall be returned. - * @param string $bundle - * The bundle name for which to return instances. - * - * @return array - * An associative array of instance arrays keyed by the field name. - */ - public function getImageFields($entity_type, $bundle) { - $image_fields = []; - - $field_definitions = $this->entityFieldManager->getFieldDefinitions($entity_type, $bundle); - foreach ($field_definitions as $field_name => $field_definition) { - if ($field_definition->getType() == 'image') { - $image_fields[$field_name] = FieldConfig::loadByName($entity_type, $bundle, $field_name); - } - } - - return $image_fields; - } - - /** - * Return a list of image replace enabled style => label map. - * - * @return array - * A key-value list where keys are style names and values are style labels - * of image styles having a replace effect configured. - */ - public function getStyleOptions() { - $styles = ImageStyle::loadMultiple(); - $result = []; - foreach ($styles as $style_name => $style) { - foreach ($style->getEffects() as $effect) { - if ($effect->getPluginId() == 'image_replace') { - $result[$style_name] = Html::escape($style->get('label')); - } - } - } - return $result; - } - -} diff --git a/src/ImageReplaceStorageInterface.php b/src/ImageReplaceStorageInterface.php new file mode 100644 index 0000000..79e1c82 --- /dev/null +++ b/src/ImageReplaceStorageInterface.php @@ -0,0 +1,46 @@ +setConfiguration($configuration); diff --git a/src/Tests/AdminTest.php b/src/Tests/AdminTest.php index bc19fa5..bfba630 100644 --- a/src/Tests/AdminTest.php +++ b/src/Tests/AdminTest.php @@ -107,8 +107,8 @@ class AdminTest extends ImageReplaceTestBase { $contains_image_original = FALSE; $contains_image_replacement = FALSE; foreach ($items as $item) { - $contains_image_original |= $item->getValue() == 'image_original'; - $contains_image_replacement |= $item->getValue() == 'image_replacement'; + $contains_image_original = $contains_image_original || $item->getValue() == 'image_original'; + $contains_image_replacement = $contains_image_replacement || $item->getValue() == 'image_replacement'; } $this->assertFalse($contains_image_original, 'Original image field is not in the list of options.'); diff --git a/src/Tests/ImageReplaceTestBase.php b/src/Tests/ImageReplaceTestBase.php index 576237a..04c5c28 100644 --- a/src/Tests/ImageReplaceTestBase.php +++ b/src/Tests/ImageReplaceTestBase.php @@ -14,6 +14,11 @@ use Drupal\Tests\BrowserTestBase; */ abstract class ImageReplaceTestBase extends BrowserTestBase { + /** + * {@inheritdoc} + */ + protected $defaultTheme = 'stark'; + /** * Create a new image field. *