If I were to save an entity programmatically with a new image field value, this image will not be assigned a focal point until the content is submitted through the UI. I wanted to ensure that we at least saved a record for the image in this case.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Dave Reid created an issue. See original summary.

Dave Reid’s picture

Title: Ensure a default focal point is set for all images » Ensure a default focal point is set for all images on programmatic saves
Status: Active » Needs review
FileSize
1.39 KB

Here's what saves default focal points for images on programmatic saves, and pulls out some code out of the loops because they don't need to be re-declared every time as they are the same value.

Status: Needs review » Needs work

The last submitted patch, 2: 2856988-set-default-focal-point.patch, failed testing.

Dave Reid’s picture

Status: Needs work » Needs review
FileSize
1.39 KB

Ok, the above version is for 8.x-1.0-beta4. Updated patch for 8.x-1.x

The last submitted patch, 2: 2856988-set-default-focal-point.patch, failed testing.

bleen’s picture

Status: Needs review » Needs work
+++ b/focal_point.module
@@ -62,14 +62,18 @@ function focal_point_entity_update(EntityInterface $entity) {
+            // Store a default focal point at the center of the image.
+            \Drupal::service('focal_point.manager')->saveCropEntity(50, 50, $item->width, $item->height, $crop);

The default value should be derived from

\Drupal::config('focal_point.settings')->get('default_value');

Otherwise this looks good to me

Dave Reid’s picture

Oh, I forgot about that default value. Updated for that change, which helps with code flow a bit more now.

emek’s picture

I need this functionality too and found this old issue, the patch in #7 works for me.

  • bleen committed 7f07466 on 8.x-1.x authored by Dave Reid
    Issue #2856988 by Dave Reid: Ensure a default focal point is set for all...
bleen’s picture

Status: Needs review » Fixed

I made a tiny change by using the short hand ternary operator:

$focal_point = $item->focal_point ?: \Drupal::config('focal_point.settings')->get('default_value');

but otherwise ... this should have gone in a LONG time ago. Thanks for sticking it back on my radar.

chr.fritsch’s picture

This breaks the integration with default content 😕
So before we were able to import focal_points by the default_content module. Now the default content gets imported but the x and y properties are getting overwritten then.

bleen’s picture

@chr.fritsch ... would you create a new issue with exact steps to reproduce?

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.