Sylius Media Alt Text: Extending
Extending Sylius Media Alt Text: resolver and manager as services, custom fallback providers with tag and priority, additional image types through the registry, and the normalizer priority.
Sylius Media Alt Text: Extending
This documentation always describes the latest published version.
Services
| Service / interface | Purpose |
|---|---|
Mmd\SyliusMediaAltTextPlugin\Resolver\AltTextResolverInterface |
Resolves the text of an image (resolve, resolveWithFallback, isDecorative) |
Mmd\SyliusMediaAltTextPlugin\Manager\AltTextManagerInterface |
Writes the texts and flag of an image (apply), validating language and length in the process |
Both interfaces are registered as public alias services and can be injected through autowiring. AltTextManagerInterface::apply() replaces all texts of an image and persists them, but does not call flush(); that is up to the caller. MediaAltTextSettingsProvider::isActive() tells you whether the "Output alt texts" switch is on.
Fallback providers
A fallback provider supplies the text when no maintained text exists. The plugin ships OwnerNameFallbackProvider: it uses the product or category name, and for avatars the name of the admin user.
Custom providers implement the interface:
use Mmd\SyliusMediaAltTextPlugin\Resolver\AltTextFallbackProviderInterface;
use Sylius\Component\Core\Model\ImageInterface;
final class MyFallbackProvider implements AltTextFallbackProviderInterface
{
public function supports(ImageInterface $image): bool
{
return $image instanceof MyImage;
}
public function fallbackFor(ImageInterface $image, string $locale, bool $exactLocale = false): ?string
{
return 'My text';
}
}
The plugin applies the tag mmd_sylius_media_alt_text.fallback_provider only to its own services. You have to tag a provider in your app yourself:
services:
App\Alt\MyFallbackProvider:
tags:
- { name: mmd_sylius_media_alt_text.fallback_provider, priority: 10 }
The providers are asked by priority (higher first), and the first non-empty text wins. OwnerNameFallbackProvider has the default priority 0 and answers with the name for product and category images as soon as one exists. A custom provider for these image types is only reached if you give it a higher priority. $exactLocale = true requires a name in exactly the given language (this is how the backfill command works).
Additional image types
The mapping "image interface => alt text entity" lives in the service mmd_sylius_media_alt_text.registry (ImageAltTextTypeRegistry). It is a plain constructor argument; there is no tag for registering a type from the outside.
To cover another image type, you need:
- your own entity that extends
AbstractImageAltTextand implementsImageAltTextInterface(with mapping and migration for your table) - an override of the service
mmd_sylius_media_alt_text.registryin your app that extends both arrays with your type (interface => entity and short name => interface)
The short name is the value the backfill command accepts for --type.
Normalizer and persist processor
- The normalizer
mmd_sylius_media_alt_text.normalizer.imageruns with priority 65, before Sylius's image normalizer (priority 64). If you add your own normalizers for images, take this order into account. mmd_sylius_media_alt_text.state_processor.persistdecorates API Platform's persist processor for all Doctrine operations. It acts only on images known to the registry and passes every other object through unchanged.
The plugin does not document a backward compatibility promise for interfaces and service IDs. For your own code, prefer extending the interfaces mentioned above rather than concrete classes.