Change cache settings programmatically in Drupal 7 - drupal

I have a block which displays list of RSS feed from an external site. I want to keep caching other blocks except the mentioned block. Howto do that?
For example, I have blockA, blockB and blockC. I only want to change the blockB's cache settings permamently to DRUPAL_NO_CACHE and leave other blocks as they are and I want to do that programmatically.

You can change the caching roles in the specific module that creates youre block.
In the block info like beneath:
function pref_block_info() {
return array(
'pref_main' => array(
'info' => t('Display flash game for auth. users'),
'cache' => DRUPAL_NO_CACHE,
),
'pref_winner' => array(
'info' => t('Show the winner of the last week.'),
'cache' => DRUPAL_NO_CACHE,
),
'pref_leader' => array(
'info' => t('Show the leader of the current week.'),
'cache' => DRUPAL_NO_CACHE,
),
'pref_top' => array(
'info' => t('Show the top 10 of the current week.'),
'cache' => DRUPAL_NO_CACHE,
),
);
}

The answer given by Jurgo is perfectly right, if you are defining the block within your own module.
In case if you want to change the caching behavior of a block written by some other module then you can use the function mymodule_block_list_alter
function mymodule_block_list_alter(&$blocks, $theme, $code_blocks) {
// Remove the caching on rss feeds block.
// Here rss-feeds is the unique key for the block
$blocks['rss-feeds']['cache'] = DRUPAL_NO_CACHE;
}

Where do the blocks come from? That's important. As Jurgo said, you can specify it in hook_block_info if it's a custom module. If they are views blocks, there is a caching setting per display within views that handles this. If they are blocks provided by some other module, you'd need to directly query the database to change the block's caching setting.
As a general note, to display RSS feeds, just use Feeds and Views. Then you don't write custom code at all for any of this.

This will reduce the work by going to performance settings page (admin/settings/performance) & clicking "cleared cached data" by scrolling down.
But make sure that this page is only accessed by administrator.
For Drupal 7 is same as Drupal 6:
<?php
drupal_flush_all_caches();
drupal_set_message('cache flushed.');
?>

Related

How to add/Modify Redux Framework options in Child Theme

I am using a paid theme which is using Redux Framework. I am trying to add a new field in Footer Options. I am able to add this field in options but only in print_r() function it is showing not showing in Themes Options Panel.
function add_another_section_bl($sections){
$sections[12]['fields'][] = array(
'id' => 'rd_footer_message_1',
'type' => 'textarea',
'title' => __('Text 2 to display in footer under bar', 'thefoxwp'),
'subtitle' => __('write your copyright information or anything you\'d like.', 'thefoxwp'),
'validate' => 'html', //see http://codex.wordpress.org/Function_Reference/wp_kses_post
'default' => 'Copyright 2015 Tranmautritam\'s team | All Rights Reserved'
);
return $sections;}
add_filter("redux/options/rd_data/register", 'add_another_section_bl');
In array data it is showing the required data but not in Options Panel in wordpress dashboard.
Kindly get me out of this.
There's a much simpler approach if you use the Redux API:
https://docs.redux.io/configuration/redux-api.html
You can modify, add, update any section or field before it is rendered. Please note, you may have to put your code within hooks. Also please note the Redux 4 method names have changed from setArgs to set_args (camelcase to non).
Otherwise, you will have all you need.

How to make a custom php output in drupal and fix it on a specific relative path?

I am trying to make a module which generates output at a specific relative path say mysite.com/newcomment/
What I am trying to do:
On client side I have coded JS which makes ajax request to "mysite.com/newcomment/". If there is any new comment, on "mysite.com/newcomment/" output " have done comment on " is generated and same is shown on client side in a pop-up.
What I have done previously:
If I am making a page/article, header and footer code is coming with output.
I have also made a endpoint for it via web service but i don't want that-much complexity.
Am I doing it the right way any pointers or clues will be helpful.
Your question is very confusing but from the title it sounds like your looking for hook_menu
Check out: api.drupal.org
Your custom url will be created with code that looks like:
function my_module_menu() {
$items['example/feed'] = array(
'title' => 'Example RSS feed',
'page callback' => 'my_module_page',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
Then output the code on the page callback:
my_module_page {
return '<h2 class=test>Hello World</h2>';
}
While you normally want html to be generated using theme functions for better caching this should at least get you started.
Edit:
To print only the data such as in an ajax call the function should look like this. Of course only include the code for your version of Drupal:
my_module_page {
// Drupal 7
print 'string';
drupal_exit();;
// Drupal 6
print 'string';
module_invoke_all('exit');
exit;
}

Menu path with wildcard

You can't use wildcards in menu paths? A quick summary of my problem (which I've made sure makes sense, so you're not wasting your time): I have a menu which i'm showing on node pages of a certain content-type. My path to a node page would be like...
events/instal2010
...where instal2010 would be the name of an event (event is the content-type).
I'm using the Context and Menu Block modules to place a menu in the sidebar on that page...
Event (the default active item)
Programme
Visitor info
Book tickets
... where the path for Programme would be
events/instal2010/programme
So for this to work for many different events, those menu items need a wildcard in their path, e.g.
events/*/programme
Perhaps it's time to ditch menus and just use a block with php to determine what page we're on from the URL.
Any advice from experienced hands would be awsome, thanks.
You cannot create menu items with wildcards from the administrative interface of Drupal, but you can create menu items with wildcards in a module. I would recommend creating a custom module that uses hook_menu() to create the menu items. An example implementation would look something like:
function YOURMODULE_menu() {
$items = array();
$items['events/%/programme'] = array(
'title' => 'Programme',
'description' => 'Loads a program page',
'page callback' => 'YOUR CUSTOM FUNCTION NAME', // Custom function used to perform any actions, display the page, etc
'page arguments' => array(1), // Passes wildcard (%) to your page callback function
'access callback' => TRUE, // Change if you want to control access
'type' => MENU_NORMAL_ITEM, // Creates a link in the menu
'menu_name' => 'primary-links' // Adds the link to your primary links menu, change if needed
);
return $items;
}
In $items['events/%/programme'] = array(, the % is the wildcard and it will be passed to your page callback function. It may be helpful to read more about hook_menu() and the Anatomy of hook_menu may also help as well.

Drupal 6 - force page view to display a different view?

Kind-of a crazy question here...
I have a view display that's set up as a page. It looks great in theme A (desktop), but terrible in theme B (mobile). So I made a different version of the view for theme B. Since the desktop/mobile 'sites' are the same just with different themes, the url for this page will be the same regardless of hte theme selected.
So I would like to be able to point the user to:
mysite/this_crazy_view
and have the returned page select the proper view depending on which theme it's in. I know that if I were using blocks I would just assign the appropriate blocks to the page in question on a theme-by-theme basis, but since the displays are using page display I don't know what the right approach would be.
I would rather not rebuild the views as blocks if I can help it (if it can't be helped, so be it...) so I was hoping there was some way to conditionally load the view via the tpl.php file or something like that...
The code I'm using in my module (per #Clive 's recommendation below) is:
<?php
function bandes_custom_hook_menu() {
$items['charley_test'] = array(
'title' => 'Title',
'access arguments' => array('access content'),
'page callback' => 'bandes_custom_set_page_view',
'type' => MENU_NORMAL_ITEM );
return $items;
}
function bandes_custom_set_page_view() {
global $theme_key;
$view_name = $theme_key == 'mobile_jquery' ? 'course_views_mobile' : 'course_views';
$display_id = 'page_5';
return views_embed_view($view_name, $display_id);
}
?>
I've cleared the cache a number of times and tried a variety of different paths in the $items array. The course_views and course_views_mobile both definitely work on their own.
I was also wondering if I could just create a views-view--course-views--page-5.tpl.php which contains almost nothing aside from the views_embed_view(course_views_mobile, page_5) part? (Only on one of the two themes...)
Actually I think the answer was simpler than all of the above. The redirect thing was giving me fits, so I removed the module, reset the paths to what I had been using, and tried the template/theme approach instead.
This is: views-view--course-views--page-5.tpl.php, only used on the mobile theme, but referring to the non-mobile view (kinda gives me a headache, but it works)
<?php
//get the view
print "IM IN YR VUE, MESSING THNGZ UP!"; //yeah, I'm going to remove this part...
$view_name="course_views_mobile";
$display_id="page_5";
print views_embed_view($view_name, $display_id);
?>
Any reason that shouldn't work? (Or why it is a really bad idea?)
Me again :)
I just thought of an easy-ish way around this actually; if you can change the URL of your views to something other than the path you want to access them at (any path would do) you could implement a hook_menu() function in a custom module for that path, to make the choice depending on your theme:
function MYMODULE_hook_menu() {
$items['this_crazy_view'] = array(
'title' => 'Title',
'access arguments' => array('access content'),
'page callback' => 'MYMODULE_crazy_view_page',
'type' => MENU_CALLBACK // or MENU_NORMAL_ITEM if you want it to appear in menus as usual
);
return $items; // Forgot to add this orginally
}
function MYMODULE_crazy_view_page() {
global $theme_key;
$view_name = $theme_key == 'foo' ? 'theView' : 'theOtherView';
$display_id = 'page_1'; // Or whatever the page display is called
return views_embed_view($view_name, $display_id);
}
That should do the trick

Autocomplete without using a callback

I want to use autocomplete-fields that link to an external source for their autocomplete-data. Drupal seems to refuse all autocomplete_paths that are not reachable within Drupal. Any ideas how to circumvent that problem? The form field looks like that:
$form['business_city'] = array(
'#type' => 'textfield',
'#size' => 30,
'#title' => t('city'),
'#autocomplete_path' => '_/city?=',
'#default_value' => $userProfile->field_address_business_city[0]['value'],
);
_/city is not reachable within Drupal for performance reasons. The script bootstraps Drupal up to session-level to check for a valid login.
UPDATE:
If I create an autcomplete-field by attaching the needed markup manually to the field it works but it is awkward to maintain:
'#attributes' => array('class' => 'form-autocomplete'),
'#suffix' => '<input type="hidden" disabled="disabled" value="/_/city?n=" id="edit-private-city-autocomplete" class="autocomplete">',
Instead of hacking, you could make sure that the path you are querying "/_/city?n=" is a valid menu_hook item. That way it will validate against the drupal_valid_path() inside theme_textfield(). From within the menu hook function callback you could then forward the request to your external data source.
Drupal 6 validates in theme_textfield() if the autocomplete path is a valid (internal) path.
So, you can't work around this unless you override that theme function.

Resources