Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Drupal 8: Get crop types of image if any
<?php
function minimalplus_preprocess_node(&$variables) {
$field_image = $variables['elements']['field_image'];
if (!empty($field_image) && !empty($field_image['#items']->first()) && !empty($field_image['#items']->first()->target_id)) {
$image_tid = $field_image['#items']->first()->target_id;
$image = File::load($image_tid);
$moduleHandler = \Drupal::service('module_handler');
if (!empty($image) && !empty($image->getFileUri()) && $moduleHandler->moduleExists('crop')) {
$crops = \Drupal::entityTypeManager()->getStorage('crop')->loadByProperties(['uri' => $image->getFileUri()]);
if (!empty($crops)) {
$crop_names = [];
foreach ($crops as $key => $crop_item) {
$crop_names[] = $crop_item->toArray()['type'][0]['target_id'];
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment