Back to WebAssetManager class

Method addInline

public self
addInline
(string $type, mixed $content, array $options = [], array $attributes = [], array $dependencies = [])
Add a new inline content asset.
Parameters
  • string $type The asset type, script or style
  • \Joomla\CMS\WebAsset\WebAssetItem|string $content The content to of inline asset
  • array $options Additional options for the asset
  • array $attributes Attributes for the asset
  • array $dependencies Asset dependencies
Returns
  • self
Since
  • 4.0.0
-
  • \InvalidArgumentException

Method addInline - Source code

/**
 * Add a new inline content asset.
 * Allow to register WebAssetItem instance in the registry, by call addInline($type, $assetInstance)
 * Or create an asset on fly (from name and Uri) and register in the registry, by call addInline($type, $content, $options ....)
 *
 * @param   string               $type          The asset type, script or style
 * @param   WebAssetItem|string  $content       The content to of inline asset
 * @param   array                $options       Additional options for the asset
 * @param   array                $attributes    Attributes for the asset
 * @param   array                $dependencies  Asset dependencies
 *
 * @return  self
 *
 * @since  4.0.0
 *
 * @throws \InvalidArgumentException
 */
public function addInline(string $type, $content, array $options = [], array $attributes = [], array $dependencies = []) : self
{
    if ($content instanceof WebAssetItemInterface) {
        $assetInstance = $content;
    } elseif (is_string($content)) {
        $name = $options['name'] ?? 'inline.' . md5($content);
        $assetInstance = $this->registry->createAsset($name, '', $options, $attributes, $dependencies);
        $assetInstance->setOption('content', $content);
    } else {
        throw new \InvalidArgumentException(sprintf('%s(): Argument #2 ($content) must be a string or an instance of %s, %s given.', __METHOD__, WebAssetItemInterface::class, \is_object($content) ? \get_class($content) : \gettype($content)));
    }
    // Get the name
    $asset = $assetInstance->getName();
    // Set required options
    $assetInstance->setOption('type', $type);
    $assetInstance->setOption('inline', true);
    // Add to registry
    $this->registry->add($type, $assetInstance);
    // And make active
    $this->useAsset($type, $asset);
    return $this;
}