Skip to content

Commit

Permalink
New Trait for fetchPriority attribute in images (#3850)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmoreno-rodriguez authored Oct 25, 2024
1 parent c55f2be commit 346d194
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
11 changes: 11 additions & 0 deletions system/blueprints/config/system.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,17 @@ form:
auto: Auto
sync: Sync
async: Async

images.defaults.fetchpriority:
type: select
size: small
label: PLUGIN_ADMIN.IMAGES_FETCHPRIORITY
help: PLUGIN_ADMIN.IMAGES_FETCHPRIORITY_HELP
highlight: auto
options:
auto: Auto
high: High
low: Low

images.seofriendly:
type: toggle
Expand Down
1 change: 1 addition & 0 deletions system/config/system.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ images:
defaults:
loading: auto # Let browser pick [auto|lazy|eager]
decoding: auto # Let browser pick [auto|sync|async]
fetchpriority: auto # Let browser pick [auto|high|low]
watermark:
image: 'system://images/watermark.png' # Path to a watermark image
position_y: 'center' # top|center|bottom
Expand Down
40 changes: 40 additions & 0 deletions system/src/Grav/Common/Media/Traits/ImageFetchPriorityTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* @package Grav\Common\Media
* @author Pedro Moreno https://github.com/pmoreno-rodriguez
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Common\Media\Traits;

use Grav\Common\Grav;

/**
* Trait ImageFetchPriorityTrait
* @package Grav\Common\Media\Traits
*/

trait ImageFetchPriorityTrait
{
/**
* Allows to set the fetchpriority attribute from Markdown or Twig
*
* @param string|null $value
* @return $this
*/
public function fetchpriority($value = null)
{
if (null === $value) {
$value = Grav::instance()['config']->get('system.images.defaults.fetchpriority', 'auto');
}

// Validate the provided value (similar to loading and decoding attributes)
if ($value !== null && $value !== 'auto') {
$this->attributes['fetchpriority'] = $value;
}

return $this;
}

}
2 changes: 2 additions & 0 deletions system/src/Grav/Common/Page/Medium/ImageMedium.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Grav\Common\Media\Interfaces\MediaLinkInterface;
use Grav\Common\Media\Traits\ImageLoadingTrait;
use Grav\Common\Media\Traits\ImageDecodingTrait;
use Grav\Common\Media\Traits\ImageFetchPriorityTrait;
use Grav\Common\Media\Traits\ImageMediaTrait;
use Grav\Common\Utils;
use Gregwar\Image\Image;
Expand All @@ -32,6 +33,7 @@ class ImageMedium extends Medium implements ImageMediaInterface, ImageManipulate
use ImageMediaTrait;
use ImageLoadingTrait;
use ImageDecodingTrait;
use ImageFetchPriorityTrait;

/**
* @var mixed|string
Expand Down

0 comments on commit 346d194

Please sign in to comment.