function ResponsiveImageStyle::sortMappings

Same name and namespace in other branches
  1. 11.x core/modules/responsive_image/src/Entity/ResponsiveImageStyle.php \Drupal\responsive_image\Entity\ResponsiveImageStyle::sortMappings()

Sort mappings by breakpoint ID and multiplier.

1 call to ResponsiveImageStyle::sortMappings()
ResponsiveImageStyle::addImageStyleMapping in core/modules/responsive_image/src/Entity/ResponsiveImageStyle.php
Adds an image style mapping to the responsive image configuration entity.

File

core/modules/responsive_image/src/Entity/ResponsiveImageStyle.php, line 150

Class

ResponsiveImageStyle
Defines the responsive image style entity.

Namespace

Drupal\responsive_image\Entity

Code

protected function sortMappings() : void {
  $this->keyedImageStyleMappings = NULL;
  $breakpoints = \Drupal::service('breakpoint.manager')->getBreakpointsByGroup($this->getBreakpointGroup());
  if (empty($breakpoints)) {
    return;
  }
  usort($this->image_style_mappings, static function (array $a, array $b) use ($breakpoints) : int {
    $breakpoint_a = $breakpoints[$a['breakpoint_id']] ?? NULL;
    $breakpoint_b = $breakpoints[$b['breakpoint_id']] ?? NULL;
    $first = (double) mb_substr($a['multiplier'], 0, -1) * 100;
    $second = (double) mb_substr($b['multiplier'], 0, -1) * 100;
    return [
      $breakpoint_b ? $breakpoint_b->getWeight() : 0,
      $first,
    ] <=> [
      $breakpoint_a ? $breakpoint_a->getWeight() : 0,
      $second,
    ];
  });
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.