Created
January 29, 2019 14:07
-
-
Save vkechagias/7f3de43ed70998725e99783a4fc7dfb1 to your computer and use it in GitHub Desktop.
Drupal 8: Get crop types of image if any
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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