Skip to content

Instantly share code, notes, and snippets.

@vkechagias
Created January 29, 2019 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vkechagias/7f3de43ed70998725e99783a4fc7dfb1 to your computer and use it in GitHub Desktop.
Save vkechagias/7f3de43ed70998725e99783a4fc7dfb1 to your computer and use it in GitHub Desktop.
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