hryvinskyi / magento2-head-tag-manager
N/A
Installs: 4
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:magento2-module
Requires
- magento/framework: *
This package is auto-updated.
Last update: 2025-05-09 13:01:40 UTC
README
A Magento 2 module for managing HTML head tags. This module allows you to dynamically add, modify, and render various HTML head elements like meta tags, stylesheets, scripts, and other elements.
Overview
The HeadTagManager module provides a flexible API for managing HTML head elements in Magento 2 applications. It allows you to:
- Add preconnect and prefetch links
- Add link elements (stylesheets, canonical, etc.)
- Add inline and external scripts
- Add inline styles
- Supporting Magento CSP (Content Security Policy)
Installation
Composer Installation
composer require hryvinskyi/magento2-head-tag-manager bin/magento setup:upgrade bin/magento setup:di:compile bin/magento cache:clean
Manual Installation
- Create directory
app/code/Hryvinskyi/HeadTagManager
- Download and extract module contents to this directory
- Enable the module:
bin/magento module:enable Hryvinskyi_HeadTagManager bin/magento setup:upgrade bin/magento setup:di:compile bin/magento cache:clean
Usage
Via ViewModel
<block name="some.block" template="...."> <arguments> <argument name="head_tag_manager_view_model" xsi:type="object">Hryvinskyi\HeadTagManager\ViewModel\HeadTagManagerViewModel</argument> </arguments> </block>
<?php /** @var \Hryvinskyi\HeadTagManager\ViewModel\HeadTagManagerViewModel $headTagManagerViewModel */ $headTagManagerViewModel = $block->getData('head_tag_manager_view_model'); $headManager = $headTagManagerViewModel->getManager(); // Add preconnect link $headTagManagerViewModel->getManager()->addLink([ 'rel' => 'preload', 'href' => 'https://example.com/image.jpg', 'as' => 'image', ]); // Add prefetch link $headTagManagerViewModel->getManager()->addLink([ 'rel' => 'prefetch', 'href' => '/landing-page', ]); // Add DNS prefetch link $headTagManagerViewModel->getManager()->addLink([ 'rel' => 'dns-prefetch', 'href' => 'https://fonts.googleapis.com/', ]); // Add stylesheets (CSP compatible) $headManager->addStylesheet($block->getViewFileUrl('css/some-custom.css')); // Add external scripts (CSP compatible) $headManager->addExternalScript($block->getViewFileUrl('js/some-custom.js')); // Add inline script (CSP compatible) $headManager->addInlineScript('console.log("Hello, world!");'); // Add inline styles (CSP compatible) $headManager->addStyle('body { background-color: #f0f0f0; }');
API Reference
Main Interfaces
HeadTagManagerInterface
- Main service for managing head elementsHeadElementInterface
- Interface for all head elements
Head Element Types
MetaElement
- HTML meta tagsLinkElement
- HTML link tags (stylesheets, favicons, etc.)ScriptElement
- External and inline scriptsStyleElement
- Inline CSS styles
Common Methods
addMetaName(string $name, string $content, ?string $key = null)
- Add meta tag with name attributeaddMetaProperty(string $property, string $content, ?string $key = null)
- Add meta tag with property attributeaddCharset(string $charset = 'UTF-8')
- Add charset meta tagaddStylesheet(string $href, array $attributes = [], ?string $key = null)
- Add stylesheet linkaddInlineStyle(string $content, array $attributes = [], ?string $key = null)
- Add inline styleaddExternalScript(string $src, array $attributes = [], ?string $key = null)
- Add external scriptaddInlineScript(string $content, array $attributes = [], ?string $key = null)
- Add inline script
Integration
The module automatically injects head elements at the <!-- {{HRYVINSKYI:PLACEHOLDER:HEAD_ADDITIONAL}} -->
placeholder in your HTML output.
License
Copyright © 2025 Volodymyr Hryvinskyi. All rights reserved.