Skip to content
Snippets Groups Projects
Commit 7eaad8d3 authored by Jess Straatmann's avatar Jess Straatmann
Browse files

Issue #3505852 by jastraat, grimreaper: Make label in 'import from library' more configurable

parent f338ef84
No related branches found
No related tags found
1 merge request!42Issue #3505852: Move section link label generation to its own function
Pipeline #427653 passed
......@@ -2,6 +2,7 @@
namespace Drupal\section_library\Controller;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Core\Ajax\AjaxHelperTrait;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Config\ImmutableConfig;
......@@ -15,8 +16,10 @@ use Drupal\Core\Url;
use Drupal\layout_builder\Context\LayoutBuilderContextTrait;
use Drupal\layout_builder\LayoutBuilderHighlightTrait;
use Drupal\layout_builder\SectionStorageInterface;
use Drupal\section_library\Entity\SectionLibraryTemplateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\section_library\Entity\SectionLibraryTemplate;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Render\Markup;
/**
......@@ -67,6 +70,13 @@ class ChooseSectionFromLibraryController implements ContainerInjectionInterface
*/
protected ImmutableConfig $config;
/**
* The Drupal Logger Factory.
*
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
*/
protected $loggerFactory;
/**
* ChooseSectionFromLibraryController constructor.
*
......@@ -80,13 +90,16 @@ class ChooseSectionFromLibraryController implements ContainerInjectionInterface
* The file url generator service.
* @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
* The configuration entity manager.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $loggerFactory
* The Drupal Logger Factory.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandler $module_handler, Renderer $renderer, FileUrlGenerator $file_url_generator, ConfigManagerInterface $config_manager) {
public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandler $module_handler, Renderer $renderer, FileUrlGenerator $file_url_generator, ConfigManagerInterface $config_manager, LoggerChannelFactoryInterface $loggerFactory) {
$this->entityTypeManager = $entity_type_manager;
$this->moduleHandler = $module_handler;
$this->renderer = $renderer;
$this->fileUrlGenerator = $file_url_generator;
$this->config = $config_manager->getConfigFactory()->get('section_library.settings');
$this->loggerFactory = $loggerFactory;
}
/**
......@@ -99,6 +112,7 @@ class ChooseSectionFromLibraryController implements ContainerInjectionInterface
$container->get('renderer'),
$container->get('file_url_generator'),
$container->get('config.manager'),
$container->get('logger.factory'),
);
}
......@@ -185,6 +199,9 @@ class ChooseSectionFromLibraryController implements ContainerInjectionInterface
if ($templateDisplay && $templateDisplay->getRenderer('image')->getSetting('image_style')) {
$image_style = $templateDisplay->getRenderer('image')->getSetting('image_style');
}
if (!isset($image_style)) {
$image_style = '';
}
// Get default image path.
// Separate method to allow extending this class to use a different image.
......@@ -202,49 +219,30 @@ class ChooseSectionFromLibraryController implements ContainerInjectionInterface
// Add type of template as a data attribute to allow filtering.
$attributes['data-section-type'] = strtolower($section->get('type')->value);
if ($fid = $section->get('image')->target_id) {
$file = $this->entityTypeManager->getStorage('file')->load($fid);
$img_path = $file->getFileUri();
// Use the configured image style if it exists.
if (isset($image_style)) {
$img_render_array = [
'#theme' => 'image_style',
'#style_name' => $image_style,
'#uri' => $img_path,
];
}
else {
// Use the original image.
$img_render_array = [
'#theme' => 'image',
'#uri' => $img_path,
];
}
$img = $this->renderer->render($img_render_array)->__toString();
}
else {
// Fallback: use default library image from module.
$img = '<img src="' . $default_path . '"/>';
}
$link_params = [];
if ($this->moduleHandler->moduleExists('layout_builder_iframe_modal')) {
$link_params = ['query' => ['destination' => Url::fromRoute('layout_builder_iframe_modal.redirect')->toString()]];
}
$link = [
'title' => Markup::create('<span class="section-library-link-img">' . $img . '</span><span class="section-library-link-label">' . $section->label() . '</span>'),
'url' => Url::fromRoute('section_library.import_section_from_library',
[
'section_library_id' => $section_id,
'section_storage_type' => $section_storage->getStorageType(),
'section_storage' => $section_storage->getStorageId(),
'delta' => $delta,
],
$link_params,
),
'attributes' => $attributes,
];
try {
$link = [
'title' => $this->getSectionLinkLabel($section, $default_path, $image_style),
'url' => Url::fromRoute('section_library.import_section_from_library',
[
'section_library_id' => $section_id,
'section_storage_type' => $section_storage->getStorageType(),
'section_storage' => $section_storage->getStorageId(),
'delta' => $delta,
],
$link_params,
),
'attributes' => $attributes,
];
$links[] = $link;
$links[] = $link;
}
catch (\Exception $e) {
$this->loggerFactory->get('section_library')->error($e->getMessage());
}
}
return [
'#theme' => 'links',
......@@ -257,6 +255,52 @@ class ChooseSectionFromLibraryController implements ContainerInjectionInterface
];
}
/**
* Get the label displayed for section links.
*
* This default label display can be overridden in a subclass.
*
* @param \Drupal\section_library\Entity\SectionLibraryTemplateInterface $section
* The section being linked.
* @param string $default_path
* The default image path.
* @param string $image_style
* The configured image style if it exists.
*
* @return \Drupal\Component\Render\MarkupInterface|string
* Rendered link label.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function getSectionLinkLabel(SectionLibraryTemplateInterface $section, string $default_path = '', string $image_style = ''): MarkupInterface|string {
if ($fid = $section->get('image')->target_id) {
$file = $this->entityTypeManager->getStorage('file')->load($fid);
$img_path = $file->getFileUri();
// Use the configured image style if it exists.
if (!empty($image_style)) {
$img_render_array = [
'#theme' => 'image_style',
'#style_name' => $image_style,
'#uri' => $img_path,
];
}
else {
// Use the original image.
$img_render_array = [
'#theme' => 'image',
'#uri' => $img_path,
];
}
$img = $this->renderer->render($img_render_array)->__toString();
}
else {
// Fallback: use default library image from module.
$img = '<img src="' . $default_path . '"/>';
}
return Markup::create('<span class="section-library-link-img">' . $img . '</span><span class="section-library-link-label">' . $section->label() . '</span>');
}
/**
* Get the path for the default image when no thumbnail is available.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment