Download the PHP package hryvinskyi/magento2-head-tag-manager without Composer

On this page you can find all versions of the php package hryvinskyi/magento2-head-tag-manager. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package magento2-head-tag-manager

Adobe Commerce / Magento 2 Head Tag Manager Module

Latest Stable Version Total Downloads License

This module provides a robust solution for managing HTML head elements in Adobe Commerce / Magento 2 applications. This module allows you to dynamically add, modify, and render various HTML head elements like meta tags, stylesheets, scripts, and other elements.

Features:

Key Features

Installation

Composer Installation

Manual Installation

  1. Create directory app/code/Hryvinskyi/HeadTagManager
  2. Download and extract module contents to this directory
  3. Enable the module:

Usage

Via ViewModel

Architecture

Cache Strategy

A block's head elements are stored inside the block's own block HTML cache entry, so they come back with its HTML when the block is served from the cache and its code does not run.

When a cacheable block renders, the elements it adds (nested blocks included) are appended to its HTML as one comment: <!--hryvinskyi-head-elements:1:, a 128-bit HMAC of the payload made with the installation's crypt key, :, the base64 of their JSON, and -->. The JSON names each element's type (short_type), and its class only when that type would not find the same factory. The comment is written even when the block added no elements, and an element whose data cannot be stored is left out of it with a warning. Magento's own save action stores the entry under its cache lock, with the block's key, tags and lifetime. There is no second entry that could go missing, be evicted on its own, or be written after another request has read the HTML.

On a hit the comment is cut off the HTML and the elements go back into the manager. Elements that _afterToHtml() adds are added on a hit and on a miss alike, and are never stored.

An entry without a valid comment (written by 3.x, while the module was disabled, or ending with text that only looks like the comment) counts as a miss. The block renders again under the lock and the entry is rewritten, so such an entry is never served without its elements and upgrading needs no flush.

A before plugin on Magento\Framework\Cache\LockGuardedCacheLoader::lockedLoadData() does this. It is declared in the global etc/di.xml and runs in every area, so a block rendered outside the storefront (in an e-mail sent by cron, for instance) strips the comment too. The same loader serves the configuration cache, so the plugin touches only a load whose lock name is a block cache key: it starts with one of the prefixes in the plugin's blockCacheKeyPrefixes argument, which lists the framework's BLOCK_ and CUSTOM_BLOCK_ and the CMS module's CMS_BLOCK prefixes. A module whose block class defines its own CACHE_KEY_PREFIX adds it as an item of that argument in its di.xml. A block loads through it when the block_html cache type is enabled and the block's cache lifetime is not null; while the cache type is disabled nothing is stored or restored. A lifetime of 0 or false also loads through it: the comment is built and Magento then saves nothing, so such a block is better given a null lifetime.

If the module is removed or disabled without bin/magento module:disable, which cleans the caches, clean the block_html cache yourself. Otherwise the comment keeps printing in cached block output until the entries expire. It is an HTML comment, so the page shows nothing.

An element that fails to render is logged with its key and left out; the rest of the head still renders.

Attribute values: true renders the boolean form, false and null leave the attribute out, numbers and Stringable objects render as text. Arrays render on meta and on a link or an external script written as text (data and class forms); an inline script or a style refuses them. A remote link or script with an array attribute is written as text, so its host has to be in a csp_whitelist.xml.

Content Security Policy

Inline scripts, styles and a script whose src points at a remote host render through Magento's secure renderer; a script with no content and a relative, protocol-relative or upper-case-scheme src is written as text, because the policy's processor would throw on it. So does a link that loads a remote stylesheet render through the secure renderer: its rel has the token stylesheet, or preload with as="style", and its href starts with a lower-case http:// or https:// and a host. When Magento's Content Security Policy module is enabled, its processor adds that host to style-src. The tag comes out as the renderer writes it, for example <link href="https://fonts.example.com/a.css" rel="stylesheet" />, and a true attribute prints as crossorigin="".

Every other link is written as text and adds nothing to the policy: preloads of fonts and scripts, icons, preconnect and prefetch links, a relative or protocol-relative href, an upper-case scheme (HTTPS://…), an empty host, and any link with an array attribute. A host such a link needs has to be in a csp_whitelist.xml. A relative href or an upper-case scheme stays a text link on purpose: the policy's processor throws on it, which would drop the stylesheet from the page.

Common Methods

Element Sort Order

You can control the rendering order of head elements using the sort_order parameter. Elements are sorted in ascending order (lower values render first). The default sort order is 10 for inline styles and 100 for every other element.

Elements with the same sort order maintain their insertion order (stable sort).

Advanced Usage

Custom Elements

To add support for a new head element type:

  1. Create Element Class: extend AbstractHeadElement. Its constructor takes $attributes and $sortOrder and no renderer; take the renderer your element writes its tag with (AttributeRendererInterface for a tag written as text, SecureHtmlRenderer for a tag that carries a nonce or hash) and pass the rest on
  2. Create Factory: extend AbstractHeadElementFactory; it creates the element through Magento's generated factory with no data, then applies attributes and sort order through the element's setters
  3. Create Strategy: extend AbstractSerializationStrategy, so the element survives the block cache
  4. Register via DI: add both to di.xml

Example Custom Element

Custom Factory

Custom Serialization Strategy

A strategy with a constructor of its own passes the factory registry and a StorableAttributesInterface on to parent::__construct(). When several strategies handle the same class, the one with the highest getPriority() is used; among equal priorities, the one registered last.

Custom Factory Registration

To register your custom element and factory, add the following to your di.xml:

Usage

To use your custom element, simply call the factory from the HeadTagManager:

Integration

The module injects head elements at the <!-- {{HRYVINSKYI:PLACEHOLDER:HEAD_ADDITIONAL}} --> placeholder in your HTML output, and removes the placeholder when there is nothing to inject.

Upgrading from 3.x

Version History

See CHANGELOG.md for detailed version history and migration notes.

Testing

Unit tests, from the Magento root:

The -c matters: the package's bootstrap provides Magento's generated factories when the installation has not been compiled.

Integration tests are in Test/Integration. Magento's integration configuration only collects app/code/*/*/Test/Integration, so for a Composer install point a copy of dev/tests/integration/phpunit.xml at vendor/hryvinskyi/magento2-head-tag-manager/Test/Integration.

Static analysis runs at PHPStan level 10 with the package's phpstan.neon.dist from the package directory.

Contributing

Contributions are welcome! Please ensure:

License

Copyright © 2025 Volodymyr Hryvinskyi. All rights reserved.


All versions of magento2-head-tag-manager with dependencies

PHP Build Version
Package Version
Requires magento/framework Version *
magento/module-page-cache Version *
magento/module-cms Version *
hryvinskyi/magento2-base Version ^3.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package hryvinskyi/magento2-head-tag-manager contains the following files

Loading the files please wait ...