Remove default CSS imports and JS files in Drupal - css

I'm building my first Drupal theme and found out something rather shocking. Drupal 7 imports 10 CSS files by default.
<style type="text/css" media="all">
#import url("http://localhost/drupal/modules/system/system.base.css?oq6xx1");
#import url("http://localhost/drupal/modules/system/system.menus.css?oq6xx1");
#import url("http://localhost/drupal/modules/system/system.messages.css?oq6xx1");
#import url("http://localhost/drupal/modules/system/system.theme.css?oq6xx1");
</style>
<style type="text/css" media="all">
#import url("http://localhost/drupal/modules/comment/comment.css?oq6xx1");
#import url("http://localhost/drupal/modules/field/theme/field.css?oq6xx1");
#import url("http://localhost/drupal/modules/node/node.css?oq6xx1");
#import url("http://localhost/drupal/modules/search/search.css?oq6xx1");
#import url("http://localhost/drupal/modules/user/user.css?oq6xx1");
</style>
<style type="text/css" media="all">
#import url("http://localhost/drupal/themes/stark/layout.css?oq6xx1");
</style>
And then this included too (not sure why or what it does)
<script type="text/javascript">
<!--//--><![CDATA[//><!--
jQuery.extend(Drupal.settings, {"basePath":"\/drupal\/","pathPrefix":"","ajaxPageState":{"theme":"stark","theme_token":"3s2kHk4YAL5BDTRW1hanuB6zKdWug7cniWp_uAyRBKk","js":{"misc\/jquery.js":1,"misc\/jquery.once.js":1,"misc\/drupal.js":1},"css":{"modules\/system\/system.base.css":1,"modules\/system\/system.menus.css":1,"modules\/system\/system.messages.css":1,"modules\/system\/system.theme.css":1,"modules\/comment\/comment.css":1,"modules\/field\/theme\/field.css":1,"modules\/node\/node.css":1,"modules\/search\/search.css":1,"modules\/user\/user.css":1,"themes\/stark\/layout.css":1}},"urlIsAjaxTrusted":{"\/drupal\/node?destination=node":true}});
//--><!]]>
</script>
I'm building a simple Drupal website from scratch and don't need those 2000 lines of css codes in the frontend. How do I proceed and remove it all? I just need my custom-style.css file and that's it.

You can use hook_css_alter api to remove core and crontributed module default css files.
https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_css_alter/7.x
For Javascript check this hook_js_alter: https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_js_alter/7.x
for example:
<?php
function YOUR_THEME_css_alter(&$css) {
// Remove Drupal core css
$exclude = array(
'modules/aggregator/aggregator.css' => FALSE,
'modules/block/block.css' => FALSE,
'modules/book/book.css' => FALSE,
'modules/comment/comment.css' => FALSE,
'modules/dblog/dblog.css' => FALSE,
'modules/field/theme/field.css' => FALSE,
'modules/file/file.css' => FALSE,
'modules/filter/filter.css' => FALSE,
'modules/forum/forum.css' => FALSE,
'modules/help/help.css' => FALSE,
'modules/menu/menu.css' => FALSE,
'modules/node/node.css' => FALSE,
'modules/openid/openid.css' => FALSE,
'modules/poll/poll.css' => FALSE,
'modules/profile/profile.css' => FALSE,
'modules/search/search.css' => FALSE,
'modules/statistics/statistics.css' => FALSE,
'modules/syslog/syslog.css' => FALSE,
'modules/system/admin.css' => FALSE,
'modules/system/maintenance.css' => FALSE,
'modules/system/system.css' => FALSE,
'modules/system/system.admin.css' => FALSE,
'modules/system/system.base.css' => FALSE,
'modules/system/system.maintenance.css' => FALSE,
'modules/system/system.messages.css' => FALSE,
'modules/system/system.menus.css' => FALSE,
'modules/system/system.theme.css' => FALSE,
'modules/taxonomy/taxonomy.css' => FALSE,
'modules/tracker/tracker.css' => FALSE,
'modules/update/update.css' => FALSE,
'modules/user/user.css' => FALSE,
'misc/vertical-tabs.css' => FALSE,
// Remove contrib module CSS
drupal_get_path('module', 'views') . '/css/views.css' => FALSE, );
$css = array_diff_key($css, $exclude);
}
?>

Related

How to override already added meta tags in Drupal 7?

In the theme vendor file I have:
$element = array(
'#tag' => 'meta',
'#attributes' => array(
'name' => 'viewport',
'content' => 'width=device-width, initial-scale=1.0',
),
);
drupal_add_html_head($element, 'bootstrap_responsive');
I do not want to touch that file, but I would like to override this setting somewhere else. I can of course do:
drupal_add_html_head(array(
'#tag' => 'meta',
'#attributes' => array(
'name' => 'viewport',
'content' => 'maximum-scale=1.0' // Change is here
)
), 'bootstrap_responsive_2');
That will work but would also be pretty ugly since it will create 2 meta tags in the end.
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="viewport" content="maximum-scale=1.0" />
Can I simply override the already added meta tag?
From within a custom module implement hook_html_head_alter. Read the comments on that page behind the link, you'll find some useful snippets there.
function MYMODULE_html_head_alter(&$head_elements) {
foreach ($head_elements as $key => $element) {
if (isset($element['#attributes']['name']) && $element['#attributes']['name'] == 'viewport') {
// Override 'content'.
$head_elements[$key]['#attributes']['content'] = 'maximum-scale=1.0';
}
}
}

How do I call custom.css in Prestashop 1.7?

I'm new to Prestashop 1.7. There's a css that I need to overwrite. I've written the css codes on custom.css that comes with Prestashop. When I refreshed the site, the custom css did not apply. There was no link rel pointing to the file.
I'd like to ask how I can call the custom.css. There's a stylesheets.tpl but I'm not sure what code to write.
You can add custom CSS & JS in the latest PrestaShop using setMedia() function which is located in the frontController as shown below:
PATH : mainDir/classes/controller/FrontController.php
public function setMedia()
{
$this->registerStylesheet('theme-main', '/assets/css/theme.css', ['media' => 'all', 'priority' => 50]);
$this->registerStylesheet('theme-custom', '/assets/css/custom.css', ['media' => 'all', 'priority' => 1000]);
if ($this->context->language->is_rtl) {
$this->registerStylesheet('theme-rtl', '/assets/css/rtl.css', ['media' => 'all', 'priority' => 900]);
}
$this->registerJavascript('corejs', '/themes/core.js', ['position' => 'bottom', 'priority' => 0]);
$this->registerJavascript('theme-main', '/assets/js/theme.js', ['position' => 'bottom', 'priority' => 50]);
$this->registerJavascript('theme-custom', '/assets/js/custom.js', ['position' => 'bottom', 'priority' => 1000]);
$assets = $this->context->shop->theme->getPageSpecificAssets($this->php_self);
if (!empty($assets)) {
foreach ($assets['css'] as $css) {
$this->registerStylesheet($css['id'], $css['path'], $css);
}
foreach ($assets['js'] as $js) {
$this->registerJavascript($js['id'], $js['path'], $js);
}
}
// Execute Hook FrontController SetMedia
Hook::exec('actionFrontControllerSetMedia', array());
return true;
}
You just have to copy it to assets/css folder of your theme

Override options in knp menu bundle for symfony2

From the default knp menu bundle template:
{%- elseif matcher.isAncestor(item, options.matchingDepth) %}
{%- set classes = classes|merge([options.ancestorClass]) %}
options.ancestorClass is equal to 'current_ancestor'. Is there a way to override this? I dont want to copy the wohle block item code which covers 50 lines of code, from which I only need to change one value.
To apply default options in all your application, you can set the knp_menu.renderer.twig.options parameter like this:
// app/config/services.yml
parameters:
knp_menu.renderer.twig.options:
currentClass: active
Default options of the Knp\Menu\Renderer\TwigRenderer are:
$this->defaultOptions = array_merge(array(
'depth' => null,
'matchingDepth' => null,
'currentAsLink' => true,
'currentClass' => 'current',
'ancestorClass' => 'current_ancestor',
'firstClass' => 'first',
'lastClass' => 'last',
'template' => $template,
'compressed' => false,
'allow_safe_labels' => false,
'clear_matcher' => true,
'leaf_class' => null,
'branch_class' => null,
), $defaultOptions);
Try
{{ knp_menu_render('AcmeDemoBundle:Builder:mainMenu', {'ancestorClass': 'your-class'}) }}
From this link

Dynamic form content and tinymce in symfony

I have a tinymce issue. I use it for my textarea's in symfony. I realized following steps.
Include the tinymce JS in my base template
2 Set some configuration for tinymce
tinymce.init({
mode : "specific_textareas",
menubar: "format edit",
editor_selector : "rte",
plugins: 'emoticons textcolor',
toolbar: 'emoticons forecolor backcolor',
height : 300,
theme : 'modern'
});
Include class "rte" in my form element of type textarea
:
->add('writeto', 'collection', array(
'type' => new WritetoType() ,
'allow_add' => false,
'allow_delete' => false,
'label' => false,
'options' => array(
'required' => false,
'attr' => array('class' => ''))
))
Now everything works fine. But if i add a new collection (exp. writTo element from above) then this have not the tinymce classes. What do I wrong?
Should i start a tinymce event for dynamic content?
This part
array('class' => '')
Should contain the rte class, ie:
array('class' => 'rte')
I solved my issue.
I create a function for init my tiny mce and call this on document load:
function initRTE()
{
// Integrate tinymce into textarea
tinymce.init({
mode : "specific_textareas",
menubar: "format edit",
editor_selector : "rte",
plugins: 'emoticons textcolor',
toolbar: 'emoticons forecolor backcolor',
height : 300,
theme : 'modern'
});
}
After i add a new collection i call the init function again and my new items get the tinymce functions

How to change the "current" class to "active" in KNPMenuBundle

I was wondering, what is the best way to change the "current" class to be "active" so Bootstrap will work correctly with it?
I thought about copying and overriding the knp_menu_html.twig but i think its not the best way...
Anyway better doing it?
To apply default options in all your application, you can set the knp_menu.renderer.twig.options parameter like this:
// app/config/services.yml
parameters:
knp_menu.renderer.twig.options:
currentClass: active
Default options of the Knp\Menu\Renderer\TwigRenderer are:
$this->defaultOptions = array_merge(array(
'depth' => null,
'matchingDepth' => null,
'currentAsLink' => true,
'currentClass' => 'current',
'ancestorClass' => 'current_ancestor',
'firstClass' => 'first',
'lastClass' => 'last',
'template' => $template,
'compressed' => false,
'allow_safe_labels' => false,
'clear_matcher' => true,
'leaf_class' => null,
'branch_class' => null,
), $defaultOptions);
You can pass it like that:
{{ knp_menu_render('AcmeDemoBundle:Builder:mainMenu', {'currentClass': 'active'}) }}

Resources