Back to WebAssetRegistry class

Method add

public self
add
(string $type, \Joomla\CMS\WebAsset\WebAssetItemInterface $asset)
Add Asset to registry of known assets
Parameters
  • string $type Asset type, script or style
  • \Joomla\CMS\WebAsset\WebAssetItemInterface $asset Asset instance
Returns
  • self
Since
  • 4.0.0

Method add - Source code

/**
 * Add Asset to registry of known assets
 *
 * @param   string                 $type   Asset type, script or style
 * @param   WebAssetItemInterface  $asset  Asset instance
 *
 * @return  self
 *
 * @since   4.0.0
 */
public function add(string $type, WebAssetItemInterface $asset) : WebAssetRegistryInterface
{
    $type = strtolower($type);
    if (!array_key_exists($type, $this->assets)) {
        $this->assets[$type] = [];
    }
    $eventChange = 'new';
    $eventAsset = $asset;
    // Use "old" asset for "Changed" event, a "new" asset can be loaded by a name from the registry
    if (!empty($this->assets[$type][$asset->getName()])) {
        $eventChange = 'override';
        $eventAsset = $this->assets[$type][$asset->getName()];
    }
    $this->assets[$type][$asset->getName()] = $asset;
    $this->dispatchAssetChanged($type, $eventAsset, $eventChange);
    return $this;
}