Working with Yii framework 2.0 I include a css file with the code below inside assets/AppAsset.php.
public $css = [
'css/style.css',
];
When I inspect element on the web browser I see the following code in the header tag:
<link href="/locahost/mywebsite/css/style.css" rel="stylesheet">
I would like to add the print.css to the web page with media='print'. How could I add the attribute media='print' into the css link within Yii framework 2.0?
There is a cssOptions attribute for the Assetsbundle, where you can define the media, but it will add all of your css file registered in that asset.
If you want to use it separately, you need to create a new asset for that.
Within the URL below you can find, how to make a custom asset and how to register it to your app (layout):
http://www.ramirezcobos.com/2013/12/28/adding-fontawesome-to-your-yii2-project-via-composer/
You could add your css to the assets bundle in the following way
public $css = [
'css/site.css',
['css/print.css', 'media' => 'print'],
];
You just have to use the $options Array of View::registerCssFile(), like this:
$this->registerCssFile('myfile.css', ['media' => 'print']);
Related
I am facing problem with "#markup" in form API.
In Drupal 7 we can use "#markup" form element which look like this:
<?php
$form['test'] = array(
'#type' => 'markup',
'#markup' => '<div><script src="http://localhost/drupal7/sites/all/libraries/test.js"></script></div>',
);
?>
//Here is my custom test.js
(function($) {
Drupal.behaviors.test = {
attach: function(context, settings) {
console.log('testttt');
document.write('*Hello, there!*');
}
};
})(jQuery);
and above code will print "Hello, there!" when form will be render.
Now in Drupal 8 I am using below code but it prints nothing.
<?php
$form['test'] = array(
'#markup' => '<div><script src="http://localhost/project8/sites/all/libraries/test.js"></script></div>',
);
?>
So how can implement this functionality in Drupal 8, which is already working in Drupal 7 .
Under script tag it can be local script or external script..
Please help...
Thanks
In Drupal 8, using "#markup" is not the proposed method to attach javascript files.
You can define libraries in your custom module or theme and attach the library to your form. The library can contain multiple js and (or) css files.
To define a library in your module:
Suppose your module name is "my_module", create a file "my_module.libraries.yml" in your module folder and specify the js and css files like this
form-script:
version: 1.x
css:
theme:
css/form.css: {}
js:
js/form.js: {}
js/form-ajax.js: {}
dependencies:
- core/jquery
In order to attach this library to your form:
$form['#attached']['library'][] = 'my_module/form-script';
Then clear cache. The js and css files will be loaded in the same order as you mentioned in the libraries.yml file.
You can define multiple libraries in the same "my_module.libraries.yml" file.
#markup still works in Drupal 8, but now it is filtered before output. As it is stated in Render API overview:
#markup: Specifies that the array provides HTML markup directly. Unless the markup is very simple, such as an explanation in a paragraph tag, it is normally preferable to use #theme or #type instead, so that the theme can customize the markup. Note that the value is passed through \Drupal\Component\Utility\Xss::filterAdmin(), which strips known XSS vectors while allowing a permissive list of HTML tags that are not XSS vectors. (I.e, and are not allowed.) See \Drupal\Component\Utility\Xss::$adminTags for the list of tags that will be allowed. If your markup needs any of the tags that are not in this whitelist, then you can implement a theme hook and template file and/or an asset library. Aternatively, you can use the render array key #allowed_tags to alter which tags are filtered.
As an alternative you may use FormattableMarkup:
'#markup' => new FormattableMarkup('<div><script src="http://localhost/project8/sites/all/libraries/test.js"></script></div>', []),
though it is not recommended in this case.
How do I add meta tags or remove them, for example the default ones added by Drupal core?
I have added the meta tags currently just to the html.html.twig file.
There seems to be module which is in beta stage, but if possible, I would like to avoid using third-party modules for this, at the moment.
The Meta tags module requires you to install three modules:
Meta tags: http://drupal.org/project/metatag
CTools: http://drupal.org/project/ctools
Token: http://drupal.org/project/token
You will see that the Meta Tags module comes with additional modules you can enable it.
Configure Meta Tags Module
Click on Configuration in the black admin menu bar
Click on Meta tags in the Search and Metadata box on the left side of the screen.
Notice there are four global settings.
Click on each and observe what you get by default. All nodes, terms, and users will have the same configuration by default. But that doesn't have to be the case.
Now add default Meta Tag.
Click on Add a meta tag default.
Selet Article from the Type dropdown. Enter the token [node:content-type:description] in the abstract field. Save When you create an Article node from now on, the description of the Article content type will appear as such:
Now its time to enter the meta tag data with in the site.
These are some of the basic steps you can follow to add Meta Tag in Drupal. Please follow the steps and let me know if there are any other issue you get.
(Credit: https://www.ostraining.com/blog/drupal/meta-tags/)
Enable the modules
The Meta tags module requires you to install three modules:
Meta tags: http://drupal.org/project/metatag
CTools: http://drupal.org/project/ctools
Token: http://drupal.org/project/token
You will see that the Meta Tags module comes with additional modules you can enable, depending on the needs of your site.
You can also just use the THEME_preproces_html hook in your theme's .theme file to add a META-TAG
for example:
function THEME_preprocess_html(&$variables) {
$viewport = [
'#tag' => 'meta',
'#attributes' => [
'name' => 'viewport',
'content' => 'user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1',
],
];
$variables['page']['#attached']['html_head'][] = [$viewport, 'viewport'];
}
Will get you this:
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1">
A bit late maybe. But if you just want to get rid of these meta-tags, you can do it simply in your THEMENAME.theme file:
function YOURTHEMENAME_page_attachments_alter(array &$page) {
$unset_meta = [
'system_meta_generator', // Generator tag
'MobileOptimized', // MobileOptimized tag
'HandheldFriendly', // HandheldFriendly tag
];
foreach ($page['#attached']['html_head'] as $key => $value) {
if (in_array($value[1], $unset_meta)) unset($page['#attached']['html_head'][$key]);
}
}
I am using CakePHP 2.0 and I want to load a concrete CSS style in a Page. (with no controller but using the Layout).
I know it can be done with scripts using:
$this->Html->script('photos', array('inline' => false));
And then on the Layout:
<?php echo $scripts_for_layout; ?>
But i have no idea if it exists or not something similar to $scripts_for_layout; for CSS style.
Do you know how can i do it?
If you can upgrade to 2.1 then you can use this:
// in your view
$this->Html->script('carousel', array('block' => 'scriptBottom'));
// in your layout
echo $this->fetch('scriptBottom');
http://book.cakephp.org/2.0/en/views.html#using-blocks-for-script-and-css-files
update:
In earlier versions you could do it like you do with scripts:
$this->Html->css('some.css', null, array('inline' => false));
And it will be placed to $scripts_for_layout.
I'm trying to figure out where the <head> for all of the pages in Drupal is (I'm using Orange theme if it matters). I have to add analytics code into the <head>.
Inside which file would I find the <head>?
Use drupal_set_html_head() by placing this in your themes template.php file. If the function MYTHEMENAME_preprocess_page() already exists insert what is inside the {closure} brackets below (before the $vars['head'] if that exists in it as well) :
function MYTHEMENAME_preprocess_page(&$vars, $hook) {
// say you wanted to add Google Webmaster Tools verification to homepage.
if (drupal_is_front_page()) {
drupal_set_html_head('<meta name="google-site-verification" content="[string from https://www.google.com/webmasters/verification/verification]" />');
$vars['head'] = drupal_get_html_head();
}
}
In template.php of your theme folder :
function your_theme_preprocess_html(&$variables) {
$appleIcon57px = array(
'#tag' => 'link',
'#attributes' => array(
'rel' => 'apple-touch-icon',
'href' => '/images/ICONE-57.png',
'type' => 'image/png',
'media' => 'screen and (resolution: 163dpi)'
)
);
drupal_add_html_head($appleIcon57px, 'apple-touch-icon57');
}
If you look in your theme folder you'll see page.tpl.php, that is the template for the site. You can add the code there most likely.
How to change a page meta description and keywords in Drupal 6
One another solution is to use blocks in header these can also managed very effectively using css.
All you have to do is to go to Structure->Blocks then create new block.
Then select theme corresponding to it and position in which section you want to show that block.
Custom html like can be added. And can be handled from that id.
It allow me to handle page structure very easily.
In the theme folder (e.g. themes/orange) there is a templates folder with the file html.tpl.php
In this file you can freely add what you need to the head section and it will be added to every page.
there is a google analyitics module that will accomplish this for you with just your key.
I'm working on a new Moodle Assignment plugin.
How can I include a custom CSS to my plugin?
I'm using Moodle 1.9.7.
Thanks in advance.
just add a file called styles.php into the Module's folder.
the file should output CSS code when it is parsed.
Moodle goes into each Module's folder and looks for that file, when the page's CSS layout is constructed.
Some themes can ignore these files using a special setting in the theme's config.php file
$THEME->modsheets = true;
/// When this is enabled, then this theme will search for
/// files named "styles.php" inside all Activity modules and
/// include them. This allows modules to provide some basic
/// layouts so they work out of the box.
/// It is HIGHLY recommended to leave this enabled.
here is a short sample of what could be the content of this styles.php file:
/* simple CSS code */
.left { float:left; }
/* php that generate CSS code */
< ?php if ( right_to_left() ) {echo ".right:text-align:left;"}
else {echo ".right:text-align:right;"} ?>
Perhaps could take a look at the function print_header_simple() defined in lib/weblib.php.
If you are writing a module which generates module specific pages, and they use the normal moodle libraries then stick something in $meta to be added to the <head> section while the page is being generated.
in lib/weblib.php:
function print_header_simple($title='', $heading='', $navigation='', $focus='', $meta='', $cache=true, $button=' ', $menu='', $usexml=false, $bodytags='', $return=false) {
This means that you probably want something like the following in your module:
$extra_css = $CFG->wwwroot.'/theme/SUPA4/supa_report.css';
$extra_css_meta = '<link rel="stylesheet" type="text/css" href="'.$extra_css.'" />';
print_header_simple($mytitle, "", $navigation, "", "", true, "", navmenu($course), '', $extra_css_meta);