Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Load and Save custom.css from panel admin

Started by Pedro M 2 years ago · 4 replies · 176 views
2 years ago

Is it possible to load a .css file within a theme administration, in a field defined in an editor type blueprint, and be able to write the modifications in the file? I have some tests to insert the code inline, with:

PHP
public function onAssetsInitialized()
{
$themeConfig = $this->config->get('themes.mytheme');
$this->grav['assets']->addInlineCss($themeConfig['css_inline']);
}

But I can't do it by modifying the custom.css file.
Any help?

last edited 09/26/24 by Pedro M
2 years ago

Hi, @pmoreno, can you clarify the scenario?

2 years ago

Of course. I would like the content of the assets/css/custom.css file to be displayed in an editor-type field (codemirror by default), and to be able to save the modifications from the admin panel itself.
When clicking on save theme modifications, all the fields defined in blueprints.yaml would be saved in the theme's .yaml file, except for this field, which would save the changes directly to the custom.css file.
I suppose it can be done, but I don't know how to do it.

2 years ago

I use a simpler approach. Hopefully it will work for you and benefit the community. Similarly, you can include JavaScript code.

<details>
<summary>1. theme blueprints.yaml</summary>

YAML
inline_css:
   type: editor
   label: Add your custom CSS
   codemirror:
       mode: 'css'
       indentUnit: 2
       autofocus: true
       indentWithTabs: true
       lineNumbers: true
       styleActiveLine: true

</details>

<details>
<summary>2. base.html.twig</summary>

TWIG
{# Inline CSS #}
{%- if theme_var('inline_css') is not empty -%}
    {%- set inline_css -%}
        {%- include 'partials/inline.css.twig' -%}
    {%- endset -%}
    {%- do assets.addInlineCss(inline_css) -%}
{%- endif -%}

</details>

<details>
<summary>3. and partials/inline.css.twig</summary>

TWIG
{% set css = theme_var('inline_css')|e %}
{% if css is defined and css is not empty %}
  {{ css|e }}
{% endif %}

</details>

If you need more control over CSS, 🤔 worth checking the editor plugin code and other available resources.

2 years ago

Hi @b.da

It's true that your approach is simpler, but this way you save the css styles in a .yaml file, and then insert them as inline (with addInlineCss), but that's not what I'm looking for.
The Editorial theme, for example, already has code in the main theme file like this:

PHP
public function onTwigSiteVariables()
{
if ($this->isAdmin() && ($this->grav['config']->get('plugins.shortcode-core.enabled'))) {
}

$themeConfig = $this->config->get('themes.editorial');

if (isset($themeConfig['custom_css']) && $themeConfig['custom_css'] && file_exists(__DIR__ . '/assets/css/custom.css')) { $this->grav['assets']->addCss('theme://assets/css/custom.css', ['priority' => 5]);
 }
}

which loads the custom.css file from the theme path /assets/css/custom.css.
I don't want to load the inline styles, that would be fine for use with a few styles.

The idea of ​​the 'Editor' plugin is what I'm looking for, but the approach of this plugin is very broad, including Twig, CSS, JS, etc, and all folders of the site, not just the theme. I will have to investigate this plugin to extract the code needed to load only the custom.css file in the theme and be able to modify it and save it from the theme itself (not in a .yaml file).

Thanks for your help.

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 85 13 hours ago
General · by pamtbaau, 18 hours ago
1 60 18 hours ago
General · by Andy Miller, 1 day ago
0 47 1 day ago
General · by Marcel, 12 months ago
6 350 5 days ago
General · by Duc , 6 days ago
3 44 5 days ago