You are here

function social_core_update_8026 in Open Social 10.1.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_core/social_core.install \social_core_update_8026()
  2. 8 modules/social_features/social_core/social_core.install \social_core_update_8026()
  3. 8.2 modules/social_features/social_core/social_core.install \social_core_update_8026()
  4. 8.3 modules/social_features/social_core/social_core.install \social_core_update_8026()
  5. 8.4 modules/social_features/social_core/social_core.install \social_core_update_8026()
  6. 8.5 modules/social_features/social_core/social_core.install \social_core_update_8026()
  7. 8.6 modules/social_features/social_core/social_core.install \social_core_update_8026()
  8. 8.7 modules/social_features/social_core/social_core.install \social_core_update_8026()
  9. 8.8 modules/social_features/social_core/social_core.install \social_core_update_8026()
  10. 10.3.x modules/social_features/social_core/social_core.install \social_core_update_8026()
  11. 10.0.x modules/social_features/social_core/social_core.install \social_core_update_8026()
  12. 10.2.x modules/social_features/social_core/social_core.install \social_core_update_8026()

Automatically try to set a new crop for the anonymous hero image.

File

modules/social_features/social_core/social_core.install, line 809
Install, update and uninstall functions for the social_core module.

Code

function social_core_update_8026() {

  // Load the anonymous hero block.
  $block_storage = \Drupal::entityTypeManager()
    ->getStorage('block_content');
  $block = $block_storage
    ->loadByProperties([
    'uuid' => '8bb9d4bb-f182-4afc-b138-8a4b802824e4',
  ]);
  $block = current($block);
  if ($block instanceof BlockContent) {

    // Get the hero image file ID.
    $hero_image = $block
      ->get('field_hero_image')
      ->getValue();

    // Check if we already have a crop for the new hero_an style.
    $query = \Drupal::entityQuery('crop')
      ->condition('entity_type', 'file')
      ->condition('entity_id', $hero_image[0]['target_id'])
      ->condition('type', 'hero_an');
    $hero_an_crop = $query
      ->execute();

    // No crop style found yet. Let's try to set one automatically.
    if (!$hero_an_crop) {

      // Find and load the current hero crop to get the dimensions.
      $query = \Drupal::entityQuery('crop')
        ->condition('entity_type', 'file')
        ->condition('entity_id', $hero_image[0]['target_id'])
        ->condition('type', 'hero')
        ->sort('cid')
        ->range(0, 1);
      $crop = $query
        ->execute();

      /** @var \Drupal\crop\Entity\Crop $crop */
      $crop = Crop::load(current($crop));

      // Crop object found, set the crop type to hero_an.
      if ($crop instanceof Crop) {
        $size = $crop
          ->size();

        // We need to adjust the width, as this is the safest way to set the new
        // crop automatically.
        // Aspect ratio was 2.836879433 (width 1200, height 423). It became
        // 2.448979592 (width 1200, height 490).
        $crop
          ->set('width', $size['width'] / 2.836879433 * 2.448979592);
        $crop
          ->set('type', 'hero_an');
        $crop
          ->save();
      }
    }
  }
}