class LoadLibraryController

A page to facilitate library loading.

Hierarchy

Expanded class hierarchy of LoadLibraryController

File

core/modules/system/tests/modules/modernizr_deprecation_test/src/Controller/LoadLibraryController.php, line 14

Namespace

Drupal\modernizr_deprecation_test\Controller
View source
class LoadLibraryController implements ContainerInjectionInterface {
    
    /**
     * The module extension list.
     *
     * @var \Drupal\Core\Extension\ModuleExtensionList
     */
    protected $moduleExtensionList;
    
    /**
     * The library discovery service.
     *
     * @var \Drupal\Core\Asset\LibraryDiscoveryInterface
     */
    protected $libraryDiscovery;
    
    /**
     * A simple page with a library attached.
     *
     * @return array
     *   A render array.
     */
    public function build($extension, $library) {
        // Confirm the extension and library are valid before proceeding.
        $extension_list = array_keys($this->moduleExtensionList
            ->getList());
        array_push($extension_list, 'core');
        assert(in_array($extension, $extension_list), "Extension {$extension} not available.");
        $available_libraries = $this->libraryDiscovery
            ->getLibrariesByExtension($extension);
        assert(isset($available_libraries[$library]), "Library {$library} not available in extension {$extension}");
        $build = [
            '#markup' => "Attaching {$extension}/{$library}",
            '#cache' => [
                'max-age' => 0,
            ],
        ];
        $build['#attached']['library'][] = "{$extension}/{$library}";
        if ($library === 'modernizr') {
            $build['#attached']['library'][] = 'modernizr_deprecation_test/access_unsupported_property';
        }
        return $build;
    }
    
    /**
     * Constructs a new LoadLibraryController.
     *
     * @param \Drupal\Core\Extension\ModuleExtensionList $module_extension_list
     *   The module extension list.
     * @param \Drupal\Core\Asset\LibraryDiscoveryInterface $library_discovery
     *   The library discovery service.
     */
    public function __construct(ModuleExtensionList $module_extension_list, LibraryDiscoveryInterface $library_discovery) {
        $this->moduleExtensionList = $module_extension_list;
        $this->libraryDiscovery = $library_discovery;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get('extension.list.module'), $container->get('library.discovery'));
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
LoadLibraryController::$libraryDiscovery protected property The library discovery service.
LoadLibraryController::$moduleExtensionList protected property The module extension list.
LoadLibraryController::build public function A simple page with a library attached.
LoadLibraryController::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
LoadLibraryController::__construct public function Constructs a new LoadLibraryController.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.