Invision Community 4: SEO, prepare for v5 and dormant account notifications Matt November 11, 2024Nov 11
Posted February 9Feb 9 Im using LoaderAbstract to add some custom CSS, cant seem to figure out how to limit the file inject to a specific app.Dispatcher doesn't initialize.Thanks.
February 9Feb 9 Author <?php namespace IPS\galleryai\extensions\core\Loader; use IPS\Extensions\LoaderAbstract; use function defined; /* To prevent PHP errors (extending class does not exist) revealing path */ if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) ) { header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' ); exit; } class CustomCSS extends LoaderAbstract { public function css(): array { // Manually construct the URL using the base URL from IPS settings $baseUrl = \IPS\Settings::i()->base_url; $url = rtrim($baseUrl, '/') . '/applications/galleryai/dev/css/front/custom.css'; return [ [(string) $url] ]; } }That works but is system wide.public function getLocationData(): array { $app = Dispatcher::i()->application ? Dispatcher::i()->application->directory : null; $module = Dispatcher::i()->module ? Dispatcher::i()->module->key : null; $controller = Dispatcher::i()->controller ?? null; $id = \IPS\Request::i()->id ? (int)\IPS\Request::i()->id : null; return array( 'app' => $app, 'module' => $module, 'controller' => $controller, 'id' => $id ); }That retruns null. Edited February 9Feb 9 by h_ybrid
February 9Feb 9 This is the code I use in one of my apps, I've adjusted it to use the values you posted above: /** * Additional CSS files to load * * @return array<string|Url> */ public function css(): array { $cssFiles = []; # Only add to the frontend, and for a specific app if ( \IPS\Dispatcher::checkLocation( 'front', 'galleryai' ) ) { $cssFiles[] = \IPS\Theme::i()->css( 'custom.css', 'galleryai', 'front' ); } return $cssFiles; }This will only add the CSS file inside the galleryai app pages. If you want it inside another application, for example, gallery, you must change the IF check to:if ( \IPS\Dispatcher::checkLocation( 'front', 'gallery' ) )And so on.You can also further specify a module and controller value in the checkLocation() call.
February 9Feb 9 Author Beautiful, thank you very much sir.Oh spoke to soon, it runs but shows everywhere.
February 9Feb 9 With the checkLocation line it shouldn't show everywhere. 🤔On what page(s) do you want to include the CSS file and how did you edit the line to match?
February 9Feb 9 Author Yes, bizarre. https://develop.emtbw.com/album/9-bike-stuff/<link rel='stylesheet' href='//develop.emtbw.com/applications/core/interface/css/css.php?css=applications/galleryai/dev/css/front/custom.css&v=1c233302a31739115726'><?php namespace IPS\galleryai\extensions\core\Loader; use IPS\Dispatcher; use IPS\Extensions\LoaderAbstract; use function defined; /* To prevent PHP errors (extending class does not exist) revealing path */ if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) ) { header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' ); exit; } class CustomCSS extends LoaderAbstract { public function css(): array { $cssFiles = []; # Only add to the frontend, and for a specific app if ( \IPS\Dispatcher::checkLocation( 'front', 'gallery' ) ) { $cssFiles[] = \IPS\Theme::i()->css( 'custom.css', 'galleryai', 'front' ); } return $cssFiles; } } Edited February 9Feb 9 by h_ybrid