Back to WebAssetManager class

Method useAsset

public self
useAsset
(string $type, string $name)
Enable an asset item to be attached to a Document
Parameters
  • string $type The asset type, script or style
  • string $name The asset name
Returns
  • self
Since
  • 4.0.0
-
  • \Joomla\CMS\WebAsset\Exception\UnknownAssetException When Asset cannot be found
  • \Joomla\CMS\WebAsset\Exception\InvalidActionException When the Manager already attached to a Document

Method useAsset - Source code

/**
 * Enable an asset item to be attached to a Document
 *
 * @param   string  $type  The asset type, script or style
 * @param   string  $name  The asset name
 *
 * @return self
 *
 * @throws  UnknownAssetException  When Asset cannot be found
 * @throws  InvalidActionException When the Manager already attached to a Document
 *
 * @since  4.0.0
 */
public function useAsset(string $type, string $name) : WebAssetManagerInterface
{
    if ($this->locked) {
        throw new InvalidActionException('WebAssetManager is locked, you came late');
    }
    // Check whether asset exists
    $asset = $this->registry->get($type, $name);
    if (empty($this->activeAssets[$type])) {
        $this->activeAssets[$type] = [];
    }
    // For "preset" need to check the dependencies first
    if ($type === 'preset') {
        $this->usePresetItems($name);
    }
    // Asset already enabled
    if (!empty($this->activeAssets[$type][$name])) {
        // Set state to active, in case it was ASSET_STATE_DEPENDENCY
        $this->activeAssets[$type][$name] = static::ASSET_STATE_ACTIVE;
        return $this;
    }
    $this->activeAssets[$type][$name] = static::ASSET_STATE_ACTIVE;
    // To re-check dependencies
    if ($asset->getDependencies()) {
        $this->dependenciesIsActual = false;
    }
    return $this;
}