0
$hide_submit_settings = array('hide_submit' => array(
  'hide_submit_status' => variable_get('hide_submit_status', TRUE),
  'hide_submit_method' => variable_get('hide_submit_method', 'disable'),
  'hide_submit_css' => variable_get('hide_submit_css', 'hide-submit-disable'),
  'hide_submit_abtext' => t(variable_get('hide_submit_abtext', '')),
  'hide_submit_atext' => t(variable_get('hide_submit_atext', '')),
  'hide_submit_hide_css' => variable_get('hide_submit_hide_css', 'hide-submit-processing'),
  'hide_submit_hide_text' => t(variable_get('hide_submit_hide_text', 'Processing...')),
  'hide_submit_indicator_style' => t(variable_get('hide_submit_indicator_style', 'expand-left')),
  'hide_submit_spinner_color' => t(variable_get('hide_submit_spinner_color', '#000')),
  'hide_submit_spinner_lines' => (int)variable_get('hide_submit_spinner_lines', 12),
  'hide_submit_hide_fx' => t(variable_get('hide_submit_hide_fx', FALSE)),
  'hide_submit_reset_time' => (int)variable_get('hide_submit_reset_time', 5000),
);

drupal_add_js($hide_submit_settings, 'setting'); 

How do I use it in Drupal 8?

4
  • This is no longer supported. Every CSS or JS have to be a physical file and defined as library. If you need some dynamic interactions with JS, use drupalSettings in #attach attribute and access them in your JS file.
    – user21641
    Jul 6, 2016 at 8:35
  • 1
    I can't see the reason why people vote down this question.
    – Jimmy Ko
    Jul 6, 2016 at 11:14
  • I didn't vote on this question, but it seems that the attached code is for drupal settings not inline JavaScript. It is still possible to attach drupal settings as @Ivan Jaros mentions.
    – mradcliffe
    Jul 6, 2016 at 13:00
  • I would expect the question was voted down because there was no actual question. Just some code and a question "how to do this in Drupal 8?" That makes this a bad question because if someone doesn't know what that code is doing they cannot answer the question even if then know how to do this in Drupal 8. Jul 12, 2017 at 15:22

3 Answers 3

0

You should read this article, how you can adding stylesheets (CSS) and JavaScript (JS) to a Drupal 8 module:

https://www.drupal.org/developing/api/8/assets

3
  • 1
    Would be good to include a relevant example snippet from there (drupalSettings)
    – Berdir
    Jul 6, 2016 at 18:04
  • 1
    There is no relevat information about adding inline css on that page. Feb 17, 2017 at 12:42
  • 1
    Why is this answer accepted? Jul 12, 2017 at 15:20
0

Create custom hook_theme twig file for adding CSS and JS

function mymodule_theme() {
    return [
        'css' => [
            'render element' => 'children',
            'variables' => array(
                'cssdata' => NULL,
            ),
        ],
        'js' => [
            'render element' => 'children',
            'variables' => array(
                'jsdata' => NULL,
            ),
        ],
    ];
}

js.html.twig

<style>{{ cssdata }}</style>

js.html.twig

<script>{{ jsdata}}</script>

In render array.

$output[] = [

'#theme' => 'js',

'#jsdata' => 'alert("Hello World")',

];
0

Warning: this may have security implications, make fully sure the $criticalCss variable contains only css you trust.

Just leaving a useful snippet related to inline CSS that needs to be added in <head>; it can be done like this:

use Drupal\Core\Render\Markup;

function mymodule_preprocess_html(&$variables) {
    // **WARNING**: make sure critical.css is safe (e.g. written/generated by you).
    $criticalCss = file_get_contents('path/to/critical.css');
    $variables['page']['#attached']['html_head'][] = [
    [
        '#tag' => 'style',
        '#value' => Markup::create($criticalCss),
    ],
    'critical-css',
  ];

}

This snippet was adapted from critical_css.module: https://git.drupalcode.org/project/critical_css/blob/8.x-1.x/critical_css.module

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.