0

I'm creating my own module and I need to programmatically create an image style to use with a new media type.

I found the core/profile/standard/config/optional/image.style.max_1300x1300.yml file whose content is the following one.

name: max_1300x1300
label: 'Max 1300x1300'
effects:
  04caae9a-fa3e-4ea6-ae09-9c26aec7d308:
    id: image_scale
    data:
      width: 1300
      height: 1300
      upscale: false
    weight: 1
    uuid: 04caae9a-fa3e-4ea6-ae09-9c26aec7d308
langcode: en
dependencies:
  module:
    - responsive_image
  enforced:
    module:
      - responsive_image

I take I need to create a file in config/install, named image.style.<image_style_id>.yml, with those attributes.

What UUID values should I use?

3
  • It's in the admin under configuration.
    – Kevin
    Jun 11, 2021 at 11:07
  • @Kevin thanks but not my question, I don't want to use the UI
    – Superhetja
    Jun 11, 2021 at 12:00
  • Writing out the yaml by hand is pointless. Make them in the UI and export them.
    – Kevin
    Jun 11, 2021 at 13:34

1 Answer 1

1

In answering this question, I assume that you have generated your configuration via UI, and then exported it, as Kevin mentioned in his comment. This is the best way to create the initial config for inclusion in your project.

Note that all examples are shown using procedural code for simplicity's sake; be sure to follow best practices and inject your services when building your project.


ImageStyle entity

Once that configuration file is installed, it becomes an ImageStyle configuration entity, and can be manipulated like any configuration entity.

// Get the storag ehandler for ImageStyle entities.
$image_style_storage = \Drupal::entityTypeManager()->getStorage("image_style");
// Get an instance of our ImageStyle entity.
$my_image_style = $image_style_storage->load("my_image_style");

The effects that are specified in the ImageStyle definition are plugins managed by the plugin.manager.image.effect plugin manager service.

As luck would have it, the ImageStyle entity offers the addImageEffect() public method, which accepts an array of effects info. This method actually takes care of generating and assigning the UUID, which is then returned to you.

public function addImageEffect(array $configuration) {
    $configuration['uuid'] = $this->uuidGenerator()->generate();
    $this->getEffects()->addInstanceId($configuration['uuid'], $configuration);
    return $configuration['uuid'];
  }

So, using your image.style.max_1300x1300 example, assigning the image effect would look like:

$effect_uuid = $my_image_style->addImageEffect([
  'id' => 'image_scale',
  'data' => [
      'width' => '1300',
      'height' => '1300',
      'upscale' => FALSE,
   ],
   'weight' => 1,
]);

You can find all of the image effects registered on your site by executing: \Drupal::service('plugin.manager.image.effect')->getDefinitions();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.