Problem/Motivation

In preparing another issue, I ran into the non OO function image_filter_keyword() in image.module.

Besides, image_filter_keyword() can return float despite its doc says

 * @return int|string
 *   The offset from the anchor, in pixels, or the anchor itself, if its value
 *   isn't one of the accepted values.

This causes deprecations in PHP 8.1 if the returned value is used in GD functions that expect ints instead.

Proposed resolution

This patch:

  • Moves the function image_filter_keyword() to a static method in \Drupal\Component\Utility\Image.
  • Adds test coverage for it.

Remaining tasks

User interface changes

None.

API changes

image_filter_keyword() deprecated
new \Drupal\Component\Utility\Image::getKeywordOffset() method

CommentFileSizeAuthor
remove-image-filter-keyword.patch9.97 KBfietserwin

Issue fork drupal-2350849

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

fietserwin’s picture

Title: move image_filter_keyword() to Imageutility » move image_filter_keyword() to ImageUtility
Issue summary: View changes

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jhedstrom’s picture

Status: Needs review » Needs work
+++ b/core/modules/image/image.module
@@ -300,29 +300,6 @@ function template_preprocess_image_style(&$variables) {
- * Accepts a keyword (center, top, left, etc) and returns it as a pixel offset.
- *
- * @param $value
- * @param $current_pixels
- * @param $new_pixels
- */
-function image_filter_keyword($value, $current_pixels, $new_pixels) {
-  switch ($value) {
-    case 'top':
-    case 'left':
-      return 0;
-
-    case 'bottom':
-    case 'right':
-      return $current_pixels - $new_pixels;
-
-    case 'center':
-      return $current_pixels / 2 - $new_pixels / 2;
-  }
-  return $value;
-}
-
-/**

We couldn't remove the funciton in 8.x, but we could mark it @deprecated for 9.x and convert the code within to call the new utility method.

The patch no longer applies, so setting to NW.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
mondrake’s picture

mondrake’s picture

Version: 9.3.x-dev » 9.4.x-dev

mondrake’s picture

Status: Needs work » Needs review

Rerolled and modernized...

mondrake’s picture

ankithashetty made their first commit to this issue’s fork.

ankithashetty’s picture

Just rebased the MR and fixed phpcs errors, thanks!

mondrake’s picture

Title: move image_filter_keyword() to ImageUtility » Deprecate image_filter_keyword()
Issue summary: View changes
mondrake’s picture

Assigned: Unassigned » mondrake
Status: Needs review » Needs work

On this.

mondrake’s picture

Assigned: mondrake » Unassigned
Status: Needs work » Needs review

Thanks @ankithashetty and @longwave!

longwave’s picture

Status: Needs review » Reviewed & tested by the community

Looks good, nice to finally resolve the @todo about test coverage :)

mondrake’s picture

Component: image system » image.module
Issue summary: View changes

Edited CR.

quietone’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record updates

The change record is the same as the deprecation message in the patch. At least it should have before and after code samples, similar to https://www.drupal.org/node/3220952.

Thanks.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mondrake’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs change record updates

Updated CR.

mondrake’s picture

Status: Reviewed & tested by the community » Needs work

NW for test failure

longwave’s picture

Version: 9.5.x-dev » 10.1.x-dev

This will have to go into 10.1.x now.

mondrake’s picture

rebased

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.