How / where can I include a library? - drupal

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.

Related

Drupal 8 add javascript and pass data in a hook with custom module

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.

Is it normal to rewrite all css classes for opencart template?

I started to rewrite Opencart 2.1 template classes with my own classes (I'm front-end and want to use my shop also as portfolio).
When I started to add modules I found out that probably it can be not the best idea taking in consideration that all modules use standard opencart styles to rewrite it via OCMOD. And I will need to make changes in all modules I add also.
I'm bit confused, so my questions are:
Is it good to rewrite opencart standard template with my own classes
and changes postions and different blocks in template?
How to make it in better and smarter way?
What if I use OCMOD to rewrite templates? In this case what going with already rewritten by module templates? Example: I have Extra tab module. It adds more custom tabs in product.tpl cia OCMOD. I need to add my styles in changed tamplate also with OCMOD. How it will work? Whether it will work?
Thank you!
You shouldn't remove the opencart's classes, you should add your extra.
The main reason is that if you going to install any extension that adds a functionality into your eshop, then it won't render properly into your template and you will need to change it's code too.
As for the question of "better and smarter" way, I can't understand what you mean.
OCMOD will rewrite the templates but it will never rewrite the css files. So you will need extra css files.
Suggestion:
Keep the default theme and extend it or create a new one based on the default template.

How to use a free bootstrap template in meteor

How to use a free bootstrap template (e.g., from startbootstrap.com) in meteor. I mean where the resources- html file, css folders and js folders of the free template should be put and what packages are needed to add/remove in meteor project file? I have tried it several times but got errors and the program crashes each time. I also transfer the script and link tags from section to section, but it did not work.
Just add the css of the template to the client of your Meteor project. Also, try using the nemo64:bootstrap package for Bootstrap. This will add some files to your project automatically, one of which will say is editable at the top. You can put your custom css in that file.
You can put the relevant html, css, and js files anywhere on the client. (Sticking it inside a folder called client will do that).
Image and font files should go in a folder called public.
You will need to make meteor templates from the HTML files. As is they will be missing any <template name="foo"> tags.
The css files can go anywhere under /client and they will automatically be added to the project. These are the easy ones.
The js files are the harder ones. If you put these under /client they will be wrapped by Meteor and will not have global scope. In all probability they won't work at all. You can put them under /public and modify your head.html file to include them to get around that problem. Odds are there won't be very many js functions in the free template anyway so you might want to read through them and see which ones you really need and then convert those to be proper template helpers or global functions on the client.

In Drupal, how do I override a template in a core module without modifying the actual core?

I want to edit the search results of a search using the search module. The template in question is search-result.tpl.php. I see that it's in html/modules/search, but I'd like to keep the modification out of the core and keep it in my sites folder. Is there a way to change the place that drupal looks for that specific template? If not, how can I accomplish my goal?
Copy the search-result.tpl.php into your theme's directory, modify it as needed, and clear the theme cache.
You may also want to check out the About overriding themable output handbook page for additional details.

Drupal - General means for finding templates?

Given that I have a URL to a page and I can see the content on it as a user, is there a simple way to find out what template files are producing the page? Ideally I would like to know three things:
The current template file(s) being used
The filename(s) I can create in the local folder to customize them
What variables are available to these file(s)
I just want to know what the general procedure is for finding this information out.
The devel_themer module includes a theme developer function that'll let you click anywhere in the page and determine what templates, theme functions, etc. are used (or can be used) to generate an element.
You can use PHP's get_defined_vars() function to get the variables available in a particular template or function.

Resources