Installation
Add the bundle to your composer.json:
composer require contextualcode/utility-bundle
Register it in config/bundles.php (if not auto-discovered):
return [
// ...
ContextualCode\UtilityBundle\ContextualCodeUtilityBundle::class => ['all' => true],
];
First Use Case
The bundle provides utilities for eZ Publish (now eZ Platform). Start with the ContextualCodeUtilityBundle core utilities:
use ContextualCode\UtilityBundle\Utility\EzPlatformUtility;
class MyService
{
private $ezPlatformUtility;
public function __construct(EzPlatformUtility $ezPlatformUtility)
{
$this->ezPlatformUtility = $ezPlatformUtility;
}
public function getCurrentUserLocation()
{
return $this->ezPlatformUtility->getCurrentUserLocation();
}
}
Key Entry Points
EzPlatformUtility: Core utilities for eZ Platform (e.g., user, content, siteaccess).ContentUtility: Simplified content operations (e.g., fetching, updating).SiteAccessUtility: Siteaccess-aware logic (e.g., switching contexts).LoggerUtility: Structured logging helpers.Check the GitLab repository for the latest API docs.
Content Operations
Use ContentUtility to abstract eZ Platform content API calls:
$content = $this->contentUtility->loadContent($contentId);
$this->contentUtility->updateContent($content, ['fields' => ['title' => 'New Title']]);
Siteaccess-Aware Logic
Leverage SiteAccessUtility to handle multi-site logic:
$currentSiteaccess = $this->siteAccessUtility->getCurrentSiteaccess();
$this->siteAccessUtility->switchSiteaccess('custom_siteaccess');
User Context
Fetch user data or permissions via EzPlatformUtility:
$user = $this->ezPlatformUtility->getCurrentUser();
if ($this->ezPlatformUtility->hasPermission('content_edit', $user)) {
// Allow edit
}
Dependency Injection Prefer constructor injection for utilities:
public function __construct(
EzPlatformUtility $ezPlatformUtility,
ContentUtility $contentUtility
) {
$this->ezPlatformUtility = $ezPlatformUtility;
$this->contentUtility = $contentUtility;
}
ContentPublishEvent).ContextualCode\UtilityBundle\BaseUtility.config/packages/contextualcode_utility.yaml:
contextualcode_utility:
default_siteaccess: 'main'
content_cache_lifetime: 3600
eZ Platform Dependency
ezplatform package is installed and configured.Siteaccess Context
getCurrentSiteaccess() rely on Symfony’s RequestStack. Test in a non-CLI environment (e.g., HTTP kernel).Content Loading
ContentUtility::loadContent() may return null if the content doesn’t exist. Always check:
$content = $this->contentUtility->loadContent($id);
if (!$content) {
throw new \RuntimeException('Content not found');
}
Permission Checks
hasPermission() uses eZ Platform’s permission system. Ensure your roles and policy definitions are correctly set up.ezplatform.debug: true in config/packages/ezplatform.yaml for detailed logs.LoggerUtility to trace utility calls:
$this->loggerUtility->debug('Content loaded', ['content_id' => $content->id]);
debug:event-dispatcher command.Custom Utilities
Extend BaseUtility to add project-specific helpers:
namespace App\Utility;
use ContextualCode\UtilityBundle\BaseUtility;
class AppUtility extends BaseUtility
{
public function customLogic()
{
// ...
}
}
Override Services
Replace bundle services in config/services.yaml:
services:
ContextualCode\UtilityBundle\Utility\ContentUtility:
class: App\Utility\CustomContentUtility
Add Twig Extensions
Register custom Twig functions/filters in services.yaml:
services:
app.twig.content_extension:
class: App\Twig\ContentExtension
tags: ['twig.extension']
How can I help you explore Laravel packages today?