Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
stream_wrapper.server_style_guide:
class: Drupal\server_style_guide\StreamWrapper\StyleGuideStream
tags:
- { name: stream_wrapper, scheme: server-style-guide }
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Drupal\server_general\ThemeTrait\QuoteThemeTrait;
use Drupal\server_general\ThemeTrait\SearchThemeTrait;
use Drupal\server_general\WebformTrait;
use Drupal\server_style_guide\ThemeTrait\Enum\IdTypeEnum;
use Drupal\server_style_guide\ThemeTrait\StyleGuideElementWrapThemeTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;

Expand Down Expand Up @@ -618,9 +619,10 @@ protected function getTextStyles(): array {
*/
protected function getHeroImage(): array {
$url = Url::fromRoute('<front>');
$image = $this->buildResponsiveImage('hero', 'server-style-guide://sand-1980x1060.jpg');

return $this->buildElementHeroImage(
$this->buildImage($this->getPlaceholderImage(1600, 400)),
$image,
$this->getRandomTitle(),
$this->getRandomTitle(),
Link::fromTextAndUrl('Learn more', $url),
Expand Down Expand Up @@ -772,6 +774,34 @@ protected function buildImage(string $url) {
];
}

/**
* Build a responsive image render array using responsive_image theme.
*
* Uses the server_style_guide:// stream wrapper which maps to the module's
* images/ directory, allowing Drupal image styles to process module-bundled
* images without copying them to public://.
*
* @param string $responsive_image_style_id
* The responsive image style ID (e.g. 'hero').
* @param string $uri
* The image uri.
* @param string $alt
* Alt text for the image.
*
* @return array
* A render array using the responsive_image theme.
*/
private function buildResponsiveImage(string $responsive_image_style_id, string $uri, string $alt = ''): array {
return [
'#theme' => 'responsive_image',
'#uri' => $uri,
'#responsive_image_style_id' => $responsive_image_style_id,
'#attributes' => [
'alt' => $alt,
],
];
}

/**
* Build text with HTML.
*
Expand Down Expand Up @@ -814,15 +844,15 @@ protected function buildProcessedText(string $text) {
* The height of the image.
* @param string $id
* The ID of the image. Or a seed.
* @param string $id_type
* @param \Drupal\server_style_guide\ThemeTrait\Enum\IdTypeEnum $id_type
* The type of the ID, either 'id' or 'seed'.
*
* @return string
* URL with placeholder.
*/
protected function getPlaceholderImage(int $width, int $height, string $id = '', string $id_type = 'id') {
protected function getPlaceholderImage(int $width, int $height, string $id = '', IdTypeEnum $id_type = IdTypeEnum::Id) {
if (!empty($id)) {
return "https://picsum.photos/{$id_type}/{$id}/{$width}/{$height}.jpg";
return "https://picsum.photos/{$id_type->value}/{$id}/{$width}/{$height}.jpg";
}
return "https://picsum.photos/{$width}/{$height}.jpg";
}
Expand Down Expand Up @@ -881,7 +911,7 @@ protected function getRelatedContent(int $num = 5, bool $is_featured = FALSE): a
for ($i = 0; $i < $num; $i++) {
$elements[] = call_user_func(
[$this, $func],
$this->buildImage($this->getPlaceholderImage(300, 200, "card_image_$i", 'seed')),
$this->buildImage($this->getPlaceholderImage(300, 200, "card_image_$i", IdTypeEnum::Seed)),
$this->getRandomTitle(),
Url::fromRoute('<front>'),
$this->buildProcessedText('Decorate one package of cauliflower in six teaspoons of plain vinegar. Try flavoring the crême fraîche gingers with clammy rum and fish sauce, simmered.'),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Drupal\server_style_guide\StreamWrapper;

use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\StreamWrapper\LocalReadOnlyStream;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
* Defines the server-style-guide:// stream wrapper.
*
* Provides read-only access to images bundled within the server_style_guide
* module, allowing Drupal image styles to process them without requiring files
* to be in public:// or private://.
*/
class StyleGuideStream extends LocalReadOnlyStream {

use StringTranslationTrait;

/**
* {@inheritdoc}
*/
public function getName() {
return $this->t('Style guide module files');
}

/**
* {@inheritdoc}
*/
public function getDescription() {
return $this->t('Read-only image files bundled with the server_style_guide module.');
}

/**
* {@inheritdoc}
*/
public function getDirectoryPath(): string {
// Two levels up from src/StreamWrapper/ lands at the module root.
return dirname(__DIR__, 2) . '/images';
}

/**
* {@inheritdoc}
*/
public function getExternalUrl(): string {
global $base_url;
$path = str_replace('\\', '/', $this->getTarget());
$path = UrlHelper::encodePath($path);
$module_dir = dirname(__DIR__, 2);
$relative_path = substr($module_dir, strlen(DRUPAL_ROOT) + 1);
return "{$base_url}/{$relative_path}/images/{$path}";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Drupal\server_style_guide\ThemeTrait\Enum;

/**
* Enum for image type.
*/
enum IdTypeEnum: string {
case Id = 'id';
case Seed = 'seed';
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ server_theme.xl:
- 2x
# 2xl breakpoint - 1536px and up.
server_theme.2xl:
label: xl
label: 2xl
mediaQuery: 'all and (min-width: 1536px)'
weight: 5
multipliers:
Expand Down
Loading