Needs review
Project:
Crop API
Version:
8.x-2.x-dev
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
29 Jun 2017 at 08:46 UTC
Updated:
10 Mar 2026 at 22:17 UTC
Jump to comment: Most recent
Comments
Comment #2
woprrr commentedYes, that sound good !! I try to found time to purpose a prototype to test it fasT.
Comment #3
berdyshev commentedThanks.
The first point is not a problem of Crop module but JSON API. Here is similar issue #2826813: Access checks failed for entity block:views_block !
Comment #4
gilles.koffmann commentedHello, I also need the image-crop relationship!!
Comment #5
woprrr commentedI come back to this subject today, I'll try to purpose a relationship fast.
Comment #6
woprrr commentedhii @berdyshev
This seem's not be an Crop API issue because this storage entity isn't not designed to only work with "focal_point" concept but more Crop zone in space. I see more anything like (/jsonapi/crop/{uuid?}) or (/jsonapi/crop/{crop_type}) for final endpoint ?
I need to test more but normally all entities 'standard' does work with jsonapi discover no ? I come back fast with information about that ...
Another thing the possibility to create crops for given uri sound great IMHO with jsonapi / graphQL integration ? We need to see what you need to achieve that wonderfull feature for API-first app building by Drupal.
Comment #7
berdyshev commentedhi @woprrr, yes, permission issue is not an Crop API issue, as I already wrote in #3.
I didn't succeed getting crop api information attached to image field of media entity referenced by the node via jsonapi relationships. Maybe I missed something.
Comment #9
woprrr commentedI test Crop api basic integration. Actually Crops are available with "api/crop/{crop_type}" and "api/crop/{crop_type}/{uuid}"
Here you have a small representation of what that endpoint can display :
I admit in this state this is not really pretty and easy to use in contenta context or simple jsonApi context.
We need to have more reflexion about where we need to access Crop ressources ? IMHO the best place is a relationship onto File representation ? or directly at basis of field_xxxx data ? Like
I'm really open to all suggestions about how crops can be integrated ?
Comment #10
berdyshev commentedYes, I know about that endpoints to get the crop details, but this requires an additional request.
In my case, I have a media entity attached to the node and I can get the file entity through the relationships. But it looks like this:
As you can see, there are no relationship or attributes related to the Crop data. That's why I need to make another request to
/jsonapi/crop/focal_point?filter[entityId][condition][path]=entity_id&filter[entityId][condition][value]=651and get these details:(Plus, for now this requires certain permissions which is not good).
I think it would be good to provide the crop details as the relationship into the file entity OR put the crop-specific data in the file's attributes. What do you think?
Comment #13
woprrr commentedThe main problem of that, is this work only in Focal Point approach and here we need more robust and common implementation.
In #decoupled slack room we have started to found solutions because Crop are a hard problem to be managed with JsonAPI (@see this excellent article ==> https://www.lullabot.com/articles/decoupled-drupal-hard-problems-image-s... ).
Here you are the discuss from Slack on that subject :
First thing with @dawehner
Second part on #decoupled room :
Another suggestion is why not expose ALL crop types effects and filter crops by specific rights ? (Only a suggestion to found better solution...).
@gabesullice Have you an example or whatever usefull to illustrate your idea ?
Comment #14
gabesullice@woprrr, unfortunately, I don't have an example for creating a computed field that I can share. To do so, you will need to implement hook_entity_type_build_alter and add a new field definitions to all the entities which have image fields. To be clear, I think this is a bit of a hack, but it would "get the job done".
If you read @e0ipso's blog, you might be able to take some inspiration from his module, https://www.drupal.org/project/consumer_image_styles.
This overrides the image field normalizer to add "meta" information about image styles. This is most like your proposal for the JSON output. Unfortunately, this method only works when one module overrides the normalizer. I.e., multiple modules trying to add different information under the "meta" key will conflict with one another.
Comment #15
e0ipso> This overrides the image field normalizer to add "meta" information about image styles. This is most like your proposal for the JSON output. Unfortunately, this method only works when one module overrides the normalizer. I.e., multiple modules trying to add different information under the "meta" key will conflict with one another.
That is the sad truth. However we can use service decorators to play nicely. I'd love to review a patch in that direction if that's not the case (I can't remember now).
Comment #16
grimreaperHi,
For information, there is a related issue in Entity Share #3220320: Support Crop API.
Comment #17
sickness29 commentedComment #19
sickness29 commentedCreated MR to add computed field which contains crop data for image. Now it should be available in jsonapi relationships under "crop"
Comment #20
pbabin commentedThank you @sickness29!
We're running Crop 8.x-2.4 and Drupal 10.3.2. We applied MR !9 and tested @grimreaper's #3220320: Support Crop API. Our setup is now bringing over the crops and displaying images as our users desire. We did have a locked policy that needed updating in order for the shares to bring over the content.
Comment #21
pbabin commentedBumping for inclusion - we have tested in our instance for over 8 months now. Additionally, two other members of the community have confirmed that this is working for our use in conjunction with Entity Share - https://www.drupal.org/project/entity_share/issues/3220320
Comment #22
dieterholvoet commentedNote: the
administer croppermission is required to fetch information about crops through JSON:API. Ideally, there would be a separate permission to view crops.Comment #23
dieterholvoet commentedThere's still an issue here: if a file has multiple crops of the same type, all these crops are returned. In
CropStorageInterface::getCrop()a database query fetches all crops keyed by type, making sure that only the last crop of each type is returned. The Crop API module currently has no way to expose old crops, so I think we should keep that consistent here.Comment #27
pbabin commentedThis merge request introduces several improvements and fixes related to Crop API and its integration with other systems like Entity Share:
Notes and caveats
This is my first major patch, so I expect some tests to break and anticipate that new functional tests will be required. I’m very open to feedback and eager to make this release-ready — I’m happy to adjust the implementation based on reviewer guidance.
Regarding the parent entity timestamp logic: this was the only reliable way I found to get crops to re-import through Entity Share after updates (initial imports worked, but subsequent updates did not). I explored adding a dedicated changed field, but the current timestamp update was the most effective solution. That said, it still feels like a workaround that might be better addressed more directly in this or a related update.
For more background, see:
Also note that I applied the following patch for compatibility in my environment:
This work has been submitted as a separate merge request to avoid disrupting any existing workflows depending on the previous patch.
Comment #28
pbabin commentedRan into issues after implementing my initial patch. Entity Share still needed admin access to view the crops.
Problem: Exposing crop data via JSON:API for use with Entity Share required granting anonymous users administer crop types and administer crop — full admin permissions — because no read-only access path existed.
Changes made in the most recent patch
Access control (CropAccessControlHandler, CropTypeAccessControlHandler, CropType.php): Added a new view crop permission that grants read-only view/view label access to both crop content entities and crop_type config entities. CropType previously had no custom access handler at all, so all operations fell through to the admin permission. A new CropTypeAccessControlHandler was added and registered on the entity. CropAccessControlHandler was also corrected — its view fallback was pointing to administer crop types instead of the crop entity's own administer crop permission, and checkCreateAccess was added to prevent JSON:API schema introspection from requiring admin access.
Entity ID reconciliation (Crop::preSave): When Entity Share imports a crop from a remote site, the crop's entity_id (an integer FK) holds the source site's ID, which differs from the local entity ID for the same file/media. This caused the crop to be invisible to the local media entity's computed field, and image_widget_crop would create a new default full-size crop on top of it — producing the wrong (full-size) crop on the client. preSave now detects when entity_id does not resolve to a real local entity and reconciles it to the correct local ID by looking up the file by URI.
URI fallback in CropFieldItemList: The computed crop field queried only by entity_id + entity_type. Added a URI-based fallback query (the same key used by CropStorage::getCrop() and CropEffect) so imported crops are always surfaced even if entity_id hasn't been reconciled yet.
Display visibility (crop.module): Removed setDisplayOptions('view', ...) and set setDisplayConfigurable('view', FALSE) so the computed field is never rendered in entity view displays and never written into display config exports.
I also added tests for the functionality that was added.