How to add an "onload" attribute to css links as like as generated by advagg in Drupal 7 - drupal-7

I want to add attribute rel ='preload' and onload="this.rel='stylesheet'" as done in advagg ?I have done in hook_css_later function but no luck . Here is my code
function page_speed_css_alter(&$css) {
foreach ($css as $key=>&$item) {
if (file_exists($item['data'])) {
$item['preprocess'] = FALSE;
$item['attributes']['rel'] = "preload";
$item['attributes']['onload'] = "this.rel='stylesheet'";
}
}
}

You’ll have to patch core if you don’t want to use advagg. Any reason why you don’t want to use it?

Related

how can I load a joomla module as a link?

this is my problem...
I have some of images and links that I want to load different joomla modules when user click on them.
mean each hyperlink can load another module|position
thanks all
In case that you just want to call a module's content from a url the following answer will help you.
If you just want to show / hide a module in the same page you could use something similar to my previous answer: Joomla 3 Show different modules on same position depending on toggler
Joomla provides the functionality to call a specific file of the active template by adding the tmpl=FILENAME key/value to the url's query string.
All built-in templates have a component.php file if user wants to load the template with the component only. You could check the following link for more details: Adding print pop-up functionality to a component.
You could do something similar to only show the modules that you want to load.
You could copy the component.php to a new file (I have used custom.php) and added the following php code in the <body> ... </body> part.
<?php
$jinput = JFactory::getApplication()->input;
$selectedPosition = $jinput->getString("position", "");
$selectedModule = $jinput->getString("module", "");
$selectedModuleTitle = $jinput->getString("title");
if($selectedPosition !== "") {
$modules = JModuleHelper::getModules($selectedPosition);
foreach ($modules as $module) {
echo JModuleHelper::renderModule($module);
}
} elseif ($selectedModule !== "") {
$module = JModuleHelper::getModule($selectedModule, $selectedModuleTitle);
echo JModuleHelper::renderModule($module);
}
?>
So with a similar way as loadposition / loadmodule works you could call the new template file using:
index.php?tmpl=custom&position=MODULE_POSITION
or
index.php?tmpl=custom&module=MODULE_TYPE
or
index.php?tmpl=custom&module=MODULE_TYPE&title=MODULE_TITLE
Optionally if you want to load the module with a specific style, you could pass it to the second paramter of the renderModule method like:
echo JModuleHelper::renderModule($module, array("style" => "xhtml"));
Hope this helps

MDL: Set Switch state to "on" via JS

I can access my switch via let iSwitch = document.getElementById('interestedSwitch');
I looked at github and found the method MaterialSwitch.on(), but iSwitch.MaterialSwitch.on(); is undefined. iSwitch.checked = true; does not help either.
I am using AngularJS, if this could be a source of the problem.
Assuming interestedSwitch is the input element with class mdl-switch__input you need to access the parent element. This should be the label with mdl-js-switch (with MaterialSwitch component). Then you can try something like:
if(parent) {
if(parent.MaterialSwitch && parent.MaterialSwitch.checkToggleState) {
/* update MDL state if it was changes form javascript */
parent.MaterialSwitch.checkToggleState();
}
}

Drupal 7 Unable to clear view cache in custom module

I have a view and it is responding fine for the filters I give. However when I run this multiple times in a for loop in my module, I get the same response for whichever filter I apply.
I searched the web and found code to turn off view caching. I have also disabled views data caching from structure->views->settings->advanced. But that is not working.
Below is the example code:
foreach ($term_ids as $term_id) {
$view2 = test_generate_view($view_name, $display_handler, $page, $count, $term_id);
echo "<pre>";
print_r($view2);
}
function test_generate_view($view_name, $display_handler, $page, $count, $term_id = null) {
$view = views_get_view($view_name, TRUE);
$view->set_display($display_handler);
if (!empty($term_id)) {
$term_item = $view->get_item($display_handler, 'filter', 'field_ref_issue_target_id');
$term_item['value']['value'] = $term_id;
$view->set_item($display_handler, 'filter', 'field_ref_issue_target_id', $term_item);
}
$view->init_pager();
$view->pager['items_per_page'] = $count;
$view->pager['use_pager'] = true;
$view->display_handler->options['use_pager'] = true;
$view->set_items_per_page($count);
$view->pager['current_page'] = $page;
$view->is_cacheable = FALSE;
$view->pre_execute();
$view->execute();
return $view;
}
If I don't run them in a loop and try separately for every term-id its working fine. But if I run them in a loop like above, the output is same for any term-id.
The code doesn't look so bad and because the filter changes, the caching should deliver a different result even if turned on. Because the code is working without the loop, maybe you should look into that. is $term_ids really an array of integer values or an array of term objects? If so, the function call would fall back to default which is null for term_ids and would not add a filter.
By the way: You should have a look at contextual filters which you can use really easily.

CakePHP - Changing default Flash layout

I know that I can replace the flash markup by creating something like custom_flash.ctp in Elements folder and call it like:
$this->Session->setFlash('Hello', custom_flash)
But how can I use custom layout when not adding the second parameter?
$this->Session->setFlash('Hello')
I thought I can replace the default by having a file named default.ctp inside Elements folder. But I can't.
I want to keep the code as short as possible. That's why I'm looking a way to do this
Any solution? Thanks
Try to create your Component:
class MySessionComponent extends Session {
public function setFlash($message) {
return $this->setFlash($message, 'custom_flash');
}
}
and than in your controller just use:
public $components = array('MySession');
$this->MySession->setFlash('Hello');
I found the answer from this question.
We need to add this codes in app/Controller/AppController.php
function beforeRender(){
if ($this->Session->check('Message.flash')) {
$flash = $this->Session->read('Message.flash');
if ($flash['element'] == 'default') {
$flash['element'] = 'fileNameOfYourCustomFlash';
$this->Session->write('Message.flash', $flash);
}
}
}
It basically add element parameter in flash when it doesn't exist yet.
This is explained on the cakephp website here

Add microdata to a Drupal site

Is there a way to add microdata to a Drupal 7 site by editing the field.tpl files? My site is built using panels and views so http://drupal.org/project/microdata isn't working. Is there another way to go about adding microdata by hard coding?
Thanks.
The best way to add microdata is by editing the field.tpl files and then checking - use file templates on views. For panels I used the module http://drupal.org/project/panels_extra_styles to add coding around the pane.
I managed to override the page template file and wrap my panel in a code.
The code I used was
template.php:
function ThemeName_preprocess_page(&$vars) {
// if this is a panel page, add template suggestions
if($panel_page = page_manager_get_current_page()) {
// add a generic suggestion for all panel pages
$variables['theme_hook_suggestions'][] = 'page__panel';
// add the panel page machine name to the template suggestions
$variables['theme_hook_suggestions'][] = 'page__' . $panel_page['name'];
$object = $panel_page['contexts']['argument_entity_id:node_1'];
$result_array = get_object_vars($object);
$value = $result_array['restrictions']['type']['0'];
if($panel_page['name'] == 'node_view' AND $value == 'product' ) {
$vars['theme_hook_suggestions'][] = 'page__node_view_product';
}
if($panel_page['name'] == 'node_view' AND $value == 'artist' ) {
$vars['theme_hook_suggestions'][] = 'page__node_view_artist';
}
and I created a files under ThemeName/templates
page--node_view_artist.tpl.php and
page--node_view_product.tpl.php
I hope this helps someone, it took me a long time to figure it out!

Resources