-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow modules to add classes and styles to page layout
- Loading branch information
Showing
4 changed files
with
107 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Omeka\Service\ViewHelper; | ||
|
||
use Omeka\View\Helper\PageLayout; | ||
use Laminas\ServiceManager\Factory\FactoryInterface; | ||
use Interop\Container\ContainerInterface; | ||
|
||
class PageLayoutFactory implements FactoryInterface | ||
{ | ||
public function __invoke(ContainerInterface $services, $requestedName, array $options = null) | ||
{ | ||
return new PageLayout($services->get('EventManager')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
namespace Omeka\View\Helper; | ||
|
||
use Omeka\Api\Representation\SitePageRepresentation; | ||
use Laminas\EventManager\Event; | ||
use Laminas\EventManager\EventManager; | ||
use Laminas\View\Helper\AbstractHelper; | ||
|
||
class PageLayout extends AbstractHelper | ||
{ | ||
protected $eventManager; | ||
|
||
public function __construct(EventManager $eventManager) | ||
{ | ||
$this->eventManager = $eventManager; | ||
} | ||
|
||
public function render(SitePageRepresentation $page) | ||
{ | ||
$view = $this->getView(); | ||
|
||
// Allow modules to add classes for styling the layout. | ||
$eventArgs = $this->eventManager->prepareArgs(['classes' => []]); | ||
$this->eventManager->triggerEvent(new Event('page_layout.classes', $page, $eventArgs)); | ||
$classes = $eventArgs['classes']; | ||
|
||
// Allow modules to add inline styles for styling the layout. | ||
$eventArgs = $this->eventManager->prepareArgs(['inline_styles' => []]); | ||
$this->eventManager->triggerEvent(new Event('page_layout.inline_styles', $page, $eventArgs)); | ||
$inlineStyles = $eventArgs['inline_styles']; | ||
|
||
// Prepare the page layout. | ||
switch ($page->layout()) { | ||
case 'grid': | ||
$view->headLink()->appendStylesheet($view->assetUrl('css/page-grid.css', 'Omeka')); | ||
$classes[] = 'page-layout-grid'; | ||
$inlineStyles[] = sprintf( | ||
'grid-template-columns: repeat(%s, 1fr);', | ||
(int) $page->layoutDataValue('grid_columns') | ||
); | ||
$inlineStyles[] = sprintf( | ||
'column-gap: %spx;', | ||
(int) $page->layoutDataValue('grid_column_gap', 10) | ||
); | ||
$inlineStyles[] = sprintf( | ||
'row-gap: %spx;', | ||
(int) $page->layoutDataValue('grid_row_gap', 10) | ||
); | ||
break; | ||
case '': | ||
default: | ||
$classes[] = 'page-layout-normal'; | ||
break; | ||
} | ||
echo sprintf( | ||
'<div class="blocks-inner %s" style="%s">', | ||
$view->escapeHtml(implode(' ', $classes)), | ||
$view->escapeHtml(implode(' ', $inlineStyles)) | ||
); | ||
$layouts = []; | ||
foreach ($page->blocks() as $block) { | ||
if (!array_key_exists($block->layout(), $layouts)) { | ||
// Prepare render only once per block layout type. | ||
$layouts[$block->layout()] = null; | ||
$view->blockLayout()->prepareRender($block->layout()); | ||
} | ||
// Render each block according to page layout. | ||
switch ($page->layout()) { | ||
case 'grid': | ||
$gridColumns = (int) $page->layoutDataValue('grid_columns'); | ||
$blockLayoutData = $block->layoutData(); | ||
$getValidPosition = fn($columnPosition) => in_array($columnPosition, ['auto',...range(1, $gridColumns)]) ? $columnPosition : 'auto'; | ||
$getValidSpan = fn($columnSpan) => in_array($columnSpan, range(1, $gridColumns)) ? $columnSpan : $gridColumns; | ||
echo sprintf( | ||
'<div style="grid-column: %s / span %s">%s</div>', | ||
$getValidPosition($blockLayoutData['grid_column_position'] ?? 'auto'), | ||
$getValidSpan($blockLayoutData['grid_column_span'] ?? $gridColumns), | ||
$view->blockLayout()->render($block) | ||
); | ||
break; | ||
case '': | ||
default: | ||
echo $view->blockLayout()->render($block); | ||
break; | ||
} | ||
} | ||
echo '</div>'; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1 @@ | ||
<?php | ||
// Prepare the page layout. | ||
switch ($page->layout()) { | ||
case 'grid': | ||
$pageLayoutClass = 'page-layout-grid'; | ||
$gridColumns = (int) $page->layoutDataValue('grid_columns'); | ||
$gridColumnGap = (int) $page->layoutDataValue('grid_column_gap'); | ||
$gridRowGap = (int) $page->layoutDataValue('grid_row_gap'); | ||
$this->headLink()->appendStylesheet($this->assetUrl('css/page-grid.css', 'Omeka')); | ||
$this->headStyle()->appendStyle(<<<CSS | ||
.page-layout-grid { | ||
grid-template-columns: repeat({$gridColumns}, 1fr); | ||
column-gap: {$gridColumnGap}px; | ||
row-gap: {$gridRowGap}px; | ||
} | ||
CSS); | ||
break; | ||
case '': | ||
default: | ||
$pageLayoutClass = 'page-layout-normal'; | ||
break; | ||
} | ||
echo sprintf('<div class="blocks-inner %s">', $pageLayoutClass); | ||
$layouts = []; | ||
foreach ($page->blocks() as $block) { | ||
if (!array_key_exists($block->layout(), $layouts)) { | ||
// Prepare render only once per block layout type. | ||
$layouts[$block->layout()] = null; | ||
$this->blockLayout()->prepareRender($block->layout()); | ||
} | ||
// Render each block according to page layout. | ||
switch ($page->layout()) { | ||
case 'grid': | ||
$gridColumns = (int) $page->layoutDataValue('grid_columns'); | ||
$blockLayoutData = $block->layoutData(); | ||
$getValidPosition = fn($columnPosition) => in_array($columnPosition, ['auto',...range(1, $gridColumns)]) ? $columnPosition : 'auto'; | ||
$getValidSpan = fn($columnSpan) => in_array($columnSpan, range(1, $gridColumns)) ? $columnSpan : $gridColumns; | ||
echo sprintf( | ||
'<div style="grid-column: %s / span %s">%s</div>', | ||
$getValidPosition($blockLayoutData['grid_column_position'] ?? 'auto'), | ||
$getValidSpan($blockLayoutData['grid_column_span'] ?? $gridColumns), | ||
$this->blockLayout()->render($block) | ||
); | ||
break; | ||
case '': | ||
default: | ||
echo $this->blockLayout()->render($block); | ||
break; | ||
} | ||
} | ||
echo '</div>'; | ||
<?php echo $this->pageLayout()->render($page); ?> |