Back to WebAssetManager class

Method usePresetItems

protected self
usePresetItems
(mixed $name)
Enable list of assets provided by Preset item.
Parameters
  • string $name The asset name
Returns
  • self
Since
  • 4.0.0
-
  • \Joomla\CMS\WebAsset\Exception\UnsatisfiedDependencyException When Asset dependency cannot be found

Method usePresetItems - Source code

/**
 * Enable list of assets provided by Preset item.
 *
 * "Preset" a special kind of asset that hold a list of assets that has to be enabled,
 * same as direct call of useAsset() to each of item in list.
 * Can hold mixed types of assets (script, style, another preset, etc), the type provided after # symbol, after
 * the asset name, example: foo#style, bar#script.
 *
 * The method call useAsset() internally for each of its dependency, this is important for keeping FIFO order
 * of enabled items.
 * The Preset not a strict asset, and each of its dependency can be safely disabled by use of disableAsset() later.
 *
 * @param   string  $name  The asset name
 *
 * @return self
 *
 * @throws  UnsatisfiedDependencyException  When Asset dependency cannot be found
 *
 * @since  4.0.0
 */
protected function usePresetItems($name) : WebAssetManagerInterface
{
    // Get the asset object
    $asset = $this->registry->get('preset', $name);
    // Call useAsset() to each of its dependency
    foreach ($asset->getDependencies() as $dependency) {
        $depType = '';
        $depName = $dependency;
        $pos = strrpos($dependency, '#');
        // Check for cross-dependency "dependency-name#type" case
        if ($pos) {
            $depType = substr($dependency, $pos + 1);
            $depName = substr($dependency, 0, $pos);
        }
        $depType = $depType ?: 'preset';
        // Make sure dependency exists
        if (!$this->registry->exists($depType, $depName)) {
            throw new UnsatisfiedDependencyException(sprintf('Unsatisfied dependency "%s" for an asset "%s" of type "%s"', $dependency, $name, 'preset'));
        }
        $this->useAsset($depType, $depName);
    }
    return $this;
}