1

How to add JavaScript inside controller? I don't want to call build form. I can call css or js in src/Form/Form.php but I want to call css or js inside Controller src/Controller/Controller.php This process I am using to call css and js in Form page

public function buildForm(array $form, FormStateInterface $form_state,$cid = NULL)
{

$form['#attached']['library'][] = 'cssjs/val_master';

}

Could anyone write some example to call css or js inside Controller

Thanks.

1 Answer 1

3

The same you can use in Controller also:

<?php

   namespace Drupal\cssjs\Controller;

   use Drupal\Core\Controller\ControllerBase;

   class CssJs extends ControllerBase{
     public function css_js(){
       $build['#attached']['library'][] = 'cssjs/val_master';
       return $build;
     }

   }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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