diff --git a/focal_point.effects.inc b/focal_point.effects.inc index c6b9a17..f6d2826 100644 --- a/focal_point.effects.inc +++ b/focal_point.effects.inc @@ -37,14 +37,26 @@ function focal_point_image_effect_info() { * @return array * Image effects keyed by effect machine name, containing an array of: * - optionally: 'focal_point_disabled_by_default' = TRUE - * - any other key => value pairs are info which should be overwritten in - * hook_image_effect_info_alter(). + * - 'altered_info': an array of properties which should be overwritten + * in hook_image_effect_info_alter(). + * - 'original_info': the original property values. (This is used to + * determine if another module already altered them. + * All callbacks which are altered must be defined here. + * + * @see focal_point_image_effect_info_alter() + * @see _focal_point_original_effect_callback() */ function focal_point_image_effect_alter_info() { $alter_info = array( 'image_scale_and_crop' => array( - 'effect callback' => 'focal_point_image_scale_and_crop_effect', - 'form callback' => 'focal_point_image_scale_and_crop_form', + 'original_info' => array( + 'effect callback' => 'image_scale_and_crop_effect', + 'form callback' => 'image_resize_form', + ), + 'altered_info' => array( + 'effect callback' => 'focal_point_image_scale_and_crop_effect', + 'form callback' => 'focal_point_image_scale_and_crop_form', + ), ), 'image_crop' => array( /* This is disabled by default: enabling it would mean existing Crop @@ -66,8 +78,14 @@ function focal_point_image_effect_alter_info() { * 'no setting' and 'saved setting of 50,50'. */ 'focal_point_disabled_by_default' => TRUE, - 'effect callback' => 'focal_point_image_crop_effect', - 'form callback' => 'focal_point_image_crop_form', + 'original_info' => array( + 'effect callback' => 'image_crop_effect', + 'form callback' => 'image_crop_form', + ), + 'altered_info' => array( + 'effect callback' => 'focal_point_image_crop_effect', + 'form callback' => 'focal_point_image_crop_form', + ), ), ); @@ -92,24 +110,53 @@ function focal_point_default_enabled_effects() { function focal_point_image_effect_info_alter(&$effects) { // Add focal_point capabilities to the standard effects. (This may deprecate // our own effects, eventually.) - foreach (focal_point_image_effect_alter_info() as $effect => $info) { - if (isset($effects[$effect])) { + $pre_altered_info = array(); + foreach (focal_point_image_effect_alter_info() as $effect => $alter_info) { + if (isset($effects[$effect]) && isset($alter_info['altered_info'])) { // Alter values for this effect - foreach ($info as $key => $value) { - if ($key != 'off_by_default') { - $effects[$effect][$key] = $value; + foreach ($alter_info['altered_info'] as $key => $value) { + if (isset($alter_info['original_info']) + && $effects[$effect][$key] !== $alter_info['original_info']) { + // This value was already altered by something else; keep it. + $pre_altered_info[$effect][$key] = $effects[$effect][$key]; } + $effects[$effect][$key] = $value; } } } + + // Do we have any pre-altered values which are not equal to the original ones? + // Then keep them for later use. + if ($pre_altered_info) { + variable_set('focal_point_effect_pre_altered_info', $pre_altered_info); + } + elseif (variable_get('focal_point_effect_pre_altered_info')) { + variable_del('focal_point_effect_pre_altered_info'); + } +} + +/** + * Returns an 'original' (before we altered the info) callback for an effect. + */ +function _focal_point_original_effect_callback($callback_type, $effect_name) { + $pre_altered_info = variable_get('focal_point_effect_pre_altered_info', array()); + if (isset($pre_altered_info[$effect_name]["$callback_type callback"])) { + // The callback was already altered before we altered it; return that. + return $pre_altered_info[$effect_name]["$callback_type callback"]; + } + + // Return the original function name. + $info = focal_point_image_effect_alter_info(); + return $info[$effect_name]['original_info']["$callback_type callback"]; } /** * Form builder for the 'image_scale_and_crop' form (overridden). */ function focal_point_image_scale_and_crop_form($data = array()) { - // The original form callback: - $form = image_resize_form($data); + + $function = _focal_point_original_effect_callback('form', 'image_scale_and_crop'); + $form = $function($data); $form['focal_point_use'] = array( '#type' => 'select', @@ -130,8 +177,9 @@ function focal_point_image_scale_and_crop_form($data = array()) { * Form builder for the 'image_crop' form (overridden). */ function focal_point_image_crop_form($data = array()) { - // The original form callback: - $form = image_crop_form($data); + + $function = _focal_point_original_effect_callback('form', 'image_crop'); + $form = $function($data); $form['focal_point_use'] = array( '#type' => 'select', @@ -155,11 +203,12 @@ function focal_point_image_crop_form($data = array()) { * Image effect callback for the 'image_scale_and_crop' form (overridden). */ function focal_point_image_scale_and_crop_effect(&$image, $data) { - if (focal_point_effect_use_focal_point($data, 'image_scale_and_crop')) { + if (focal_point_effect_uses_focal_point($data, 'image_scale_and_crop')) { return focal_point_scale_and_crop_effect($image, $data); } - return image_scale_and_crop_effect($image, $data); + $function = _focal_point_original_effect_callback('effect', 'image_scale_and_crop'); + return $function($image, $data); } /** @@ -178,14 +227,16 @@ function focal_point_scale_and_crop_effect(&$image, $data) { // At worst use the default effect and let Drupal handle the errors that // likely exist at this point. - return image_scale_and_crop_effect($image, $data); + $function = _focal_point_original_effect_callback('effect', 'image_scale_and_crop'); + return $function($image, $data); } /** * Form builder for the crop form. */ function focal_point_crop_form($data = array()) { - $form = image_crop_form($data); + $function = _focal_point_original_effect_callback('form', 'image_crop'); + $form = $function($data); unset($form['anchor']); return $form; @@ -195,11 +246,12 @@ function focal_point_crop_form($data = array()) { * Image effect callback for the 'image_crop' form (overridden). */ function focal_point_image_crop_effect(&$image, $data) { - if (focal_point_effect_use_focal_point($data, 'image_crop')) { + if (focal_point_effect_uses_focal_point($data, 'image_crop')) { return focal_point_crop_effect($image, $data); } - return image_crop_effect($image, $data); + $function = _focal_point_original_effect_callback('effect', 'image_crop'); + return $function($image, $data); } /** @@ -212,7 +264,8 @@ function focal_point_crop_effect(&$image, $data) { // At worst use the default effect and let Drupal handle the errors that // likely exist at this point. - return image_crop_effect($image, $data); + $function = _focal_point_original_effect_callback('effect', 'image_crop'); + return $function($image, $data); } /** @@ -309,7 +362,7 @@ function focal_point_effect_resize_data($image_width, $image_height, $crop_width * * @return bool */ -function focal_point_effect_use_focal_point($data, $effect) { +function focal_point_effect_uses_focal_point($data, $effect) { if (!empty($data['focal_point_use'])) { // There is an attribute in this effect's config: // Value should be 'y' or 'n'. For unknown values, disable focal point. diff --git a/focal_point.module b/focal_point.module index 55036a9..c656c41 100644 --- a/focal_point.module +++ b/focal_point.module @@ -629,7 +629,7 @@ function _focal_point_get_image_styles($altered_only = FALSE) { $focal_point_style = in_array($effect['name'], $focal_point_effects, TRUE); if (!$focal_point_style) { // Is this effect configured to use focal point? - $focal_point_style = focal_point_effect_use_focal_point($effect['data'], $effect['name']); + $focal_point_style = focal_point_effect_uses_focal_point($effect['data'], $effect['name']); } if ($focal_point_style) {