Skip to content
Snippets Groups Projects

Issue #3494558: Copy image style effects optional when editing an image style effect.

1 file
+ 71
0
Compare changes
  • Side-by-side
  • Inline
@@ -155,3 +155,74 @@ function responsive_image_style_builder_form_image_style_add_parent_effect_submi
$derivativeStyle->save();
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function responsive_image_style_builder_form_image_effect_form_alter(&$form, FormStateInterface $form_state) {
// Get current image style from route
$current_style = \Drupal::routeMatch()->getParameter('image_style')->id();
if (!str_ends_with($current_style, '_default')) {
return;
}
$form['copy_to_children'] = [
'#type' => 'checkbox',
'#title' => t('Copy to derivative image styles'),
'#description' => t('Apply these effect settings to all derivative image styles.'),
'#default_value' => FALSE,
'#weight' => 99,
];
$form['#submit'][] = 'responsive_image_style_builder_effect_form_submit';
}
/**
* Custom submit handler for the image effect form.
*/
function responsive_image_style_builder_effect_form_submit(array $form, FormStateInterface $form_state) {
if (!$form_state->getValue('copy_to_children')) {
return;
}
// Get the current effect data
$effect_data = $form_state->getValue('data');
$effect_id = $form_state->getValue('id');
$effect_weight = $form_state->getValue('weight');
// Get current image style and its derivatives
$current_style = \Drupal::routeMatch()->getParameter('image_style');
$derivative_styles = \Drupal::service('risb.builder')->getDerivativeImageStyles($current_style->id());
$updated_styles = 0;
foreach ($derivative_styles as $derivative_style) {
// Find if effect already exists in derivative
$existing_effect = NULL;
foreach ($derivative_style->getEffects() as $existing) {
if ($existing->getConfiguration()['id'] === $effect_id) {
$existing_effect = $existing;
break;
}
}
if ($existing_effect) {
// Update only if effect exists
$configuration = $existing_effect->getConfiguration();
$configuration['data'] = $effect_data;
$configuration['weight'] = $effect_weight;
$derivative_style->deleteImageEffect($existing_effect);
$derivative_style->addImageEffect($configuration);
$derivative_style->save();
$updated_styles++;
}
}
if ($updated_styles > 0) {
\Drupal::messenger()->addMessage(t('Effect configuration has been updated in @count derivative image styles.', [
'@count' => $updated_styles
]));
} else {
\Drupal::messenger()->addWarning(t('No derivative styles found with this effect to update.'));
}
}
Loading