View source
<?php
namespace Drupal\styleguide\Plugin\Styleguide;
use Drupal\styleguide\GeneratorInterface;
use Drupal\styleguide\Plugin\StyleguidePluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\image\Entity\ImageStyle;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\File\FileSystemInterface;
class ImageStyleguide extends StyleguidePluginBase {
protected $generator;
protected $moduleHandler;
protected $fileSystem;
public function __construct(array $configuration, $plugin_id, $plugin_definition, GeneratorInterface $styleguide_generator, ModuleHandlerInterface $module_handler, FileSystemInterface $file_system) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->generator = $styleguide_generator;
$this->moduleHandler = $module_handler;
$this->fileSystem = $file_system;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('styleguide.generator'), $container
->get('module_handler'), $container
->get('file_system'));
}
public function items() {
$items = [];
if ($this->moduleHandler
->moduleExists('image')) {
$preview_img_path = 'public://styleguide-preview.jpg';
if ($this->fileSystem
->getDestinationFilename($preview_img_path, FileSystemInterface::EXISTS_ERROR) !== FALSE) {
$this->fileSystem
->copy($this->generator
->image('vertical'), $preview_img_path, FileSystemInterface::EXISTS_ERROR);
}
foreach (ImageStyle::loadMultiple() as $stylename => $style) {
$details = [];
foreach ($style
->getEffects() as $effect) {
$summary = $effect
->getSummary();
$summary = render($summary);
$label = $effect
->label();
if ($summary) {
$details[] = new FormattableMarkup('%label: @summary', [
'%label' => $label,
'@summary' => $summary,
]);
}
else {
$details[] = new FormattableMarkup('%label', [
'%label' => $label,
]);
}
}
$title = $this
->t('Image style, @stylename', [
'@stylename' => $style
->get('label'),
]);
$items['image_' . $stylename] = [
'title' => $title,
'description' => [
'#theme' => 'item_list',
'#items' => $details,
],
'content' => [
'#theme' => 'image_style',
'#uri' => $preview_img_path,
'#style_name' => $stylename,
'#alt' => $title,
'#title' => $title,
],
'group' => $this
->t('Media'),
];
}
}
return $items;
}
}