drush migrate command to port D7 Manual Crop module to Media Contextual Crop

Created on 17 January 2024, 10 months ago
Updated 18 January 2024, 10 months ago

Hello and thanks for all Media Contextual Crop modules

Problem/Motivation

I have a site since D4.3 that now is on D7.99.
One of its modules is Manual Crop → where you can reuse previous crops.
I mean that an editor only crops one time and from there you make the derivatives to use in different screen sizes.
The module has no port to D10 and in the issue cue 📌 Migration Needs review , sir_squall → shared his migrate code for a drush command and I adapted it for drush 12.4.3.0.
The migration is for Image Widget Crop that has not implemented that feature and has an issue ✨ Reuse Image Crop Closed: works as designed about it.
Your module allows reusing and has a great way to crop with only marking a point. Thanks!
I would like to make the drush command to migrate here.
I was a programmer, but nowadays I teach mostly.
Maybe you can support this migration since Manual Crop has still 13,097 sites using it.

I would need to know how you save crops in the database.
Or anything that you consider important.
For you is a moment and for me would be hours.

Steps to reproduce

  • Have a D7 site that now can stay with Drupal 7 NES but can decide later to migrate.
  • Use the drush command
  • Have the old crops and if you edit an old page, only positioning the point you can save without more work.

Proposed resolution

Make the drush command.

Thanks again and any improvements I will add to this issue :)

💬 Support request
Status

Closed: works as designed

Version

2.0

Component

Code

Created by

🇪🇸Spain candelas

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @candelas
  • 🇫🇷France DrDam

    Hi,

    First thanks for asking for a such challenge.

    And if it's fit with what you need to do, yes, of course it was a good addition to the collection (maybe in a "specific migration module")

    Before all, did you have allready test the media_contextual_crop collection on a fresh D10 site ? and if it correspond to the use case with media (using media_contextual_crop reference) and image_widget_crop ?

    I just need more details of implementation/configuration to answer you as well as possible. I forgot a large part of how manual_crop works in D7 (not having the technical interest in this subject at the time).

    Based on Basic configuration ( See Here → )

    In "very short" :
    - I have patch "crop" entity to add an new "database field" name "context" (in crop_field_data table) in order to "store" the "context of the crop".
    - This context are "calculated" by the method MediaContextualCropService::getBaseContext

    So in "very theoricaly", you just need :
    0. Migrate value from "D7 field" to a D10-media_reference_with_context" field
    1. "recalculate" the context of the image ( format : EntityType:BundleName:EntityId.FieldName.Delta , it's the same way than media_library_media_modify module context )
    2. Extract data from manual_crop :
    - image style,
    - the local image (file object)
    - crop "settings" ( point X, point Y, Height, Width)
    3. Create a new crop with parameters :

          $values = [
            'type' => $crop_type,
            'entity_id' => $file->id(),
            'entity_type' => 'file',
            'uri' => $field_uri,
            'context' => $context,
          ];
    
          $crop = $cropStorage->create($values);
    
          $axis_coordinate = Drupal::service('image_widget_crop.manager')->getAxisCoordinates(
              ['x' => $crop_values['x'], 'y' => $crop_values['y']],
              ['width' => $crop_values['width'], 'height' => $crop_values['height']]
            );
            $crop->setPosition($axis_coordinate['x'], $axis_coordinate['y']);
            $crop->setSize($crop_values['width'], $crop_values['height']);
            $crop->save();
    

    Et voilà (maybe not, but it's theorical)

  • 🇫🇷France DrDam
  • Assigned to DrDam
  • 🇫🇷France DrDam
  • 🇪🇸Spain candelas

    Merci beaucoup!
    I will be able to continue tomorrow.
    I will report here my progress.
    Bonjour

  • 🇫🇷France DrDam

    I think I have miss something,

    In stage 0 : "Migrate value from "D7 field" to a D10-media_reference_with_context" field"

    The data structure in media_with_contextual field are a little tricky, the basic structure are identical to a reference field type with another "field in table" []_overwriten_property_map which save "contextual data".

    an exemple with Image widget crop, the table node__[field_name] contain :

    So I don't know how data are saved in the D7 field, but we need "image_widget_crop widget RAW value" in the field data.

    In fact (sorry for that), all other stages listed previously are useless. We just need this "value" correctly set, and the image file attache to the correct media ([fieldname]_target_id )

  • 🇫🇷France DrDam

    Hi,

    I continue with some intels for you.

    I confirm what I'm saying in #6 you didn't need populate crop entities, you just need populate the "media_reference_with_context" field and save the node. #2 are totally use-less in this case, Crops will be populated by formaters, so it's in the node rendering will populate them.

  • 🇫🇷France DrDam

    @candelas :

    did you have an idea what you would make in this migration process ?

  • 🇪🇸Spain candelas

    Sorry, I am very very tired, make mistakes and make you loose time.
    I go to rest and then keep working :)
    I have to migrate the D7 field image to D10 field media with contextual modifications.
    I am thinking in not migrating manualcrop module crops since your module with focal point can make the images derivatives for the breakpoints.
    Thanks

  • Status changed to Closed: works as designed 10 months ago
  • 🇫🇷France DrDam

    Ok, i'll close this issue for now

Production build 0.71.5 2024