Jump to content

Featured Replies

Posted

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.

What is the code you're currently using?

  • 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 by h_ybrid

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.

  • Author

Beautiful, thank you very much sir.

Oh spoke to soon, it runs but shows everywhere.

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?

  • 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&amp;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 by h_ybrid

  • Author

So thats all good but also displays in forum app.

d2255c67f2d30ba061d15b3f45b6833d.png

Strange indeed. 🤔

  • Author

Thank you.

Recently Browsing 0

  • No registered users viewing this page.