I'm trying to load an easy Javascript for the Entity Print module. It should load a library defined in my theme, and a JS File.
I tried some hooks, (_page, _preprocess_html, etc) but had no chance to load the libraries for entity-print.html.twig. Also using {{ attach_library('module/library') }} didn't worked for entity print.
The only thing that worked for now was to use hook_theme to add a variable to render a variable, similar to the one used for css "entity_print_css".
Is there a way to load a library in a variable, or any way to load a library in entity print?
Related
We use fractal.js with a twig engine to prototype websites. Fractal.js brings a handy functionality to be able to use handles in includes.
So instead of writing {% include 'templates/components/teaser/basicTeaser.twig' %}, you can just write {% include '#basicteaser' %}. See: https://fractal.build/guide/core-concepts/naming.html#referencing-other-items
Of course that relies on unique components names, which I am happy to have anyway.
Could anybody point me in the right direction on how to extend timber or twig to use such handles?
Many thanks!
Twig already offers the option to load templates from an array of directories rather than just one.
$loader = new \Twig\Loader\FilesystemLoader([$templateDir1, $templateDir2]);
Plus, before passing the loader to the environment object, you can add paths for custom namespaces, i.e.:
$loader->addPath($templateDir, 'admin');
Then you can use you namespace like this:
$twig->render('#admin/index.html', []);
https://twig.symfony.com/doc/3.x/api.html#built-in-loaders
I know this is an old question and this might not be relevant to the OP anymore, but we needed the same thing and developed a WordPress plugin to bridge Timber + Fractal. You can export your Fractal components to the components-map.json file, then the loader will read the map and convert those component names to file paths.
I am building a custom module with Drupal 8. One of the requirements of the module is that it sets up a javascript file based on the configuration settings, which the user of the module sets up in module configuration.
The javascript which needs to be added to the page depends on the configuration settings. Hence I can not add it using Libraries as mentioned in this article: Adding assets to Drupal module.
I first implemented it using a block. I use Twig templates to pass the configuration variables in PHP to the twig file, and in the twig file, I have a tag to add the javascript based on the config variables. Refer Using twig templates.
The problem with this approach is that user needs to add the block on the page, and there is no UI facing element on that block. I also find it very messy.
Is there a cleaner way to add my javascript using hook and pass variables to it? I looked around and found hook_install and hook_page_attachments. So I can add Javascript, but don't know how I can pass any php variables to it.
I am new to Drupal development. Any help with this is really appreciated.
TL;DR I need to find a way to add Javascript using Drupal hook and pass some PHP variables to it.
Use in hook_page_attachments:
$attachments['#attached']['drupalSettings']['myvar'] = $myVariable;
Then in your js (I assume you already know how to attach js library):
(function ($, Drupal, drupalSettings) {
Drupal.behaviors.my_custom_behavior = {
attach: function (context, settings) {
var strings = drupalSettings.myvar;
...
}
}
})(jQuery, Drupal, drupalSettings);
See nice Acquia tutorial for more examples.
I just started using the feature that allows you to compile all TypeScript to a single .js file. The problem I ran into, as you'd expect, is that the entire .js file is executed, and not just the module(s) that I need my page to execute.
Are there any built in utilities that allow me to specify this? If not, what else can I use? I work with a team using source control, so a solution like a NuGet package that can automatically get installed for my colleagues when they use the project is preferable.
The problem I ran into, as you'd expect, is that the entire .js file is executed, and not just the module(s) that I need my page to execute.
You shouldn't have code randomly put at a global level. The global level of your code be just functions and class that are inert by them selves.
Then have the UI call these functions / classes based on current html. Some UI framework (like angular) can help you do this seperation in a neat way (example : https://www.youtube.com/watch?v=Km0DpfX5ZxM&feature=youtu.be)
While we are using Assetic with Twig templating, we want to delay the actual processing until the last moment for various reasons. So instead of using the javascripts, css tags of assetic we created something like this
{{ add_asset (['public/js/prototype-handler.js', 'public/js/shipping-method.js'], 'js') }}
Anyhow, the idea is that at the end of the response event we will process and inject all assets to the content. However, right now I'm stuck at how to add these assets to assetic so they can be processed and returned with the result file(s).
I have checked some other bundles and what they are doing right now is to render the assets via twig like this:
AssetManagementBundle
However, it doesn't seem to be an optimal approach to this. I wonder if there is a better way or not?
For people who may encounter this same need, you want to use createAsset of the AssetFactory.
Then you can loop through the created assets and do what you want with them.
For more information and working code, please check our bundle here
I'm not sure to completely understand your needs but you could be interested by the AsseticInjectorBundle, it allows you to tag your resources files in a configuration file and add it by adding the tag where you want in your assetic markup, in your layout.
I don't think dealing with resources in php is a great idea and it's better to do this directly in your layout coupled with some configuration file.
how can I include a custom library in Drupal ?
For example I need to insert this statement somewhere, to use the dompdf library:
require_once("dompdf_config.inc.php");
Once the this file is included, i can trigger custom php code from my drupal back-end custom actions.
thanks
You want to include it, in order to use it, not?
If so, just include it at the place where you are going to use it.
E.g. in your own dompdf.module.
. That module would then distribute the dompdf funcitonality trough your Drupal site.
Another, common route is to place the include in /sites/all/libraries/
and then to include it from your dompdf.module.
libraries in /sites/all/libraries/ are often libraries that will be used in more then only your module: probably in your theme too, or in other modules too.
2 Places to do it would be
In a module's .module file, if you always want it included
Wrapped in a function that you can call, if you manually want to include it.