Set Drupal Module Weight - drupal

When developing a custom module, what is the correct way to set a module's weight?

This is the correct way to do it in Drupal 7
/**
* Implements hook_enable()
*/
function YOUR_MODULE_enable() {
db_update('system')
->fields(array('weight' => 1))
->condition('type', 'module')
->condition('name', 'YOUR_MODULE')
->execute();
}

The standard way is to do it in a query in the install hook.
From the devel module:
/**
* Implementation of hook_install()
*/
function devel_install() {
drupal_install_schema('devel');
// New module weights in core: put devel as the very last in the chain.
db_query("UPDATE {system} SET weight = 88 WHERE name = 'devel'");
...
}

if for some reason you have to stick it in an update hook, you will want to properly return the result from update_sql, lest you get nasty-looking innocuous errors.
function mymodule_update_6000(&$sandbox) {
$res[] = update_sql("UPDATE {system} SET weight = 1 WHERE name = 'mymodule'");
return $res;
}

Related

How to remove query string from static resource?

I've tried it with
https://www.drupal.org/project/remove_querystring_from_static_resource
But it doesn't work well for me .
How can I achieve that programmatically?
The following is the test result:
This trouble is usually encountered when static resources (eg. images, css & javascript files) are accessed using a query string.
Eg: http://example.com/image.png?something=test
Those query strings are used for avoiding browser caching. Their values are changed, so the browser should do a new request instead of getting cached resource.
You should remove those query strings (?something=test in my example) and use some suitable Cache-Control headers.
Edit:
Try this code.
Replace THEMENAME with your theme name.
/**
* Implements template_process_html().
* Remove Query Strings from CSS & JS filenames
*/
function THEMENAME_process_html( &$variables) {
$variables['styles'] = preg_replace('/\.css\?[^"]+/', '.css', $variables['styles']);
$variables['scripts'] = preg_replace('/\.js\?[^"]+/', '.js', $variables['scripts']);
}
/**
* Implement hook_image_style
* Override theme image style to remove query string.
* #param $variables
*/
function THEMENAME_image_style($variables) {
// Determine the dimensions of the styled image.
$dimensions = array(
'width' => $variables['width'],
'height' => $variables['height'],
);
image_style_transform_dimensions($variables['style_name'], $dimensions);
$variables['width'] = $dimensions['width'];
$variables['height'] = $dimensions['height'];
// Determine the URL for the styled image.
$variables['path'] = image_style_url($variables['style_name'], $variables['path']);
// Remove query string for image.
$variables['path'] = preg_replace('/\?.*/', '', $variables['path']);
return theme('image', $variables);
}
Finally I solved the issue with this code:
use Drupal\Core\Asset\AttachedAssetsInterface;
/**
* Implements hook_css_alter().
*/
function bootstrap_css_alter(&$css, AttachedAssetsInterface $assets){
foreach ($css as &$file) {
if ($file['type'] != 'external') {
$file['type'] = 'external';
$file['data'] = '/' . $file['data'];
}
}
}
/**
* Implements hook_js_alter().
*/
function bootstrap_js_alter(&$javascript, AttachedAssetsInterface $assets){
foreach ($javascript as &$file) {
if ($file['type'] != 'external') {
$file['type'] = 'external';
$file['data'] = '/' . $file['data'];
}
}
}
Put this code to your yourthemename.theme file.
it works perfect on drupal 8
Hope this help you guys.

Drupal: cannot unset js file and use new js in theme directory

I want to copy a js file in my theme folder instead of hacking the module. This is my code:
/*update js files */
$scripts = drupal_add_js();
unset($scripts['module']['sites/all/modules/imagefield_crop/imagefield_crop.js']);
$scripts['module']['sites/all/themes/zen/zen/js/imagefield_crop.js'] = array('preprocess' => 1, 'cache' => 1);
$vars['scripts'] = drupal_get_js('header', $scripts);
IT works for lightbox2 but it doesn't work for imagefield_crop.js
I've cleaned all Drupal caches and browser cache but my browser continues to load the original js in the module directory.
thanks
Update: This is the array $scripts
['module']
...
[sites/all/modules/imagefield_crop/Jcrop/js/jquery.Jcrop.js] => Array
(
[cache] => 1
[defer] =>
[preprocess] => 1
)
Given the updated question after the discussion in the comments, it seems like you are mixing up the involved js files. Imagefield_crop adds two different ones:
jquery.Jcrop.js, which is an imported library file providing the crop functionality in general (in context of jquery) - normally, you should not have a reason to replace this.
'imagefield_crop.js', which is the one providing the 'bridging' to allow the above library to work properly in the Drupal context - my understanding was that you wanted to replace this one.
Both are needed for the functionality to work. Your posted code would only replace the second one, and unless you accidentally posted the wrong code snippet in your question update, it seems to work.
If you wanted to replace both (or only the first one), you'd need to extend/adjust your unsetting logic to do so.
Hello here is the possible solutions it might help though I've never done this before
/**
* Implementation of hook_theme_registry_alter().
* Based on the jquery_update module.
*
* Make this page preprocess function runs *last*,
* so that a theme can't call drupal_get_js().
*/
function MYMODULE_theme_registry_alter(&$theme_registry) {
if (isset($theme_registry['page'])) {
// See if our preprocess function is loaded, if so remove it.
if ($key = array_search('MYMODULE_preprocess_page',
$theme_registry['page']['preprocess functions'])) {
unset($theme_registry['page']['preprocess functions'][$key]);
}
// Now add it on at the end of the array so that it runs last.
$theme_registry['page']['preprocess functions'][] = 'MYMODULE_preprocess_page';
}
}
/**
* Implementation of moduleName_preprocess_hook().
* Based on the jquery_update module functions. *
* Strips out JS and CSS for a path.
*/
function MYMODULE_preprocess_page(&$variables, $arg = 'my_page', $delta=0) {
// I needed a one hit wonder. Can be altered to use function arguments
// to increase it's flexibility.
if(arg($delta) == $arg) {
$scripts = drupal_add_js();
$css = drupal_add_css();
// Only do this for pages that have JavaScript on them.
if (!empty($variables['scripts'])) {
$path = drupal_get_path('module', 'admin_menu');
unset($scripts['module'][$path . '/admin_menu.js']);
$variables['scripts'] = drupal_get_js('header', $scripts);
}
// Similar process for CSS but there are 2 Css realted variables.
// $variables['css'] and $variables['styles'] are both used.
if (!empty($variables['css'])) {
$path = drupal_get_path('module', 'admin_menu');
unset($css['all']['module'][$path . '/admin_menu.css']);
unset($css['all']['module'][$path . '/admin_menu.color.css']);
$variables['styles'] = drupal_get_css($css);
}
}
}
http://www.mediacurrent.com/blogs/remove-or-replace-jscss-page

Drupal: automatically add new nodes to a nodequeue

Can I somehow automatically add a node to a specific nodequeue when it is created ?
(I'm using nodequeue module: drupal.org/project/nodequeue)
thanks
I needed this feature for a drupal 7 site and took the custom module solution. Let's say the setup is one nodequeue, and every 'project' nodes should be automatically added and removed to the queue. Create an empty nodequeue_auto_add directory in sites/all/modules/. This contains these two files
nodequeue_auto_add.info
name = Nodequeue auto add/remove
description = Automatically adds and remove nodes when they are created and deleted.
core = 7.x
version = 7.x-dev
package = Nodequeue
dependencies[] = nodequeue
nodequeue_auto_add.module
<?php
/**
* Implements hook_node_insert().
*/
function nodequeue_auto_add_node_insert($node) {
$nid = $node->nid;
$type = $node->type;
// only process project node
if ($type != 'project') {
return FALSE;
}
// I've only one nodequeue where a specific node type should always be
// added so this is taken from the mysql nodequeue_queue table
$queue_id = 1;
// subqueue id, exists even if we created a really basic nodequeue (from nodequeue_subqueue table)
$sqid = 1;
$queue = nodequeue_load($queue_id);
$subqueue = nodequeue_load_subqueue($sqid);
if (function_exists('views_invalidate_cache')) {
views_invalidate_cache();
}
nodequeue_subqueue_add($queue, $subqueue, $nid);
}
/**
* Implements hook_node_delete().
*/
function nodequeue_auto_add_node_delete($node) {
$nid = $node->nid;
$type = $node->type;
// only process project node
if ($type != 'project') {
return FALSE;
}
if (function_exists('views_invalidate_cache')) {
views_invalidate_cache();
}
// I've only one nodequeue where a specific node type should always be
// added so this is taken from the mysql nodequeue_queue table
$queue_id = 1;
// subqueue id, exists even if we created a really basic nodequeue (from nodequeue_subqueue table)
$sqid = 1;
nodequeue_subqueue_remove_node($sqid, $nid);
}
There is an action "Add to Nodequeue" in Rules. I've solved by creating a new rule.
There's a simple module made just for this purpose, for both Drupal 6 and Drupal 7:
http://drupal.org/project/auto_nodequeue
I'm using drupal 5 which doesn't have rules. This is how I accomplished it, I'm not using any subqueues:
if($op == 'insert'){
if($node->type == 'node_type'){
$queue = nodequeue_load(4);
$subqueue = nodequeue_load_subqueue(4);
nodequeue_subqueue_add($queue, $subqueue, $node->nid);
}
}
You cannot set it up within the admin interface, but you can do it in a custom module using hook_nodeapi op insert.
There is a module for that. Check it out and see if it helps. https://www.drupal.org/project/auto_nodequeue/project/auto_nodequeue
While this module does not exactly meet the OP "auto add" request it does allow you to configure the content type so that you can add it directly to the queue: https://www.drupal.org/sandbox/rlhawk/1444496 It is a sandbox but very stable and I use it all the time and love it.

Is there a way (other than sql) to get the mlid for a given nid in drupal?

I've got a node, I want it's menu. As far as I can tell, node_load doesn't include it. Obviously, it's trivial to write a query to find it based on the path node/nid, but is there a Drupal Way to do it?
if the menu tree has multiple levels sql seems a better option.
a sample for drupal 7 is given bellow where path is something like 'node/x'
function _get_mlid($path, $menu_name) {
$mlid = db_select('menu_links' , 'ml')
->condition('ml.link_path' , $path)
->condition('ml.menu_name',$menu_name)
->fields('ml' , array('mlid'))
->execute()
->fetchField();
return $mlid;
}
The Menu Node module exposes an API to do this.
You can read the documentation (Doxygen) in the code. I think the functionality you need is provided by the menu_node_get_links($nid, $router = FALSE) method:
/**
* Get the relevant menu links for a node.
* #param $nid
* The node id.
* #param $router
* Boolean flag indicating whether to attach the menu router item to the $item object.
* If set to TRUE, the router will be set as $item->menu_router.
* #return
* An array of complete menu_link objects or an empy array on failure.
*/
An associative array of mlid => menu object is returned. You probably only need the first one so it might look like something like this:
$arr = menu_node_get_links(123);
list($mlid) = array_keys($arr);
Otherwise, you can try out the suggestion in a thread in the Drupal Forums:
Use node/[nid] as the $path argument for:
function _get_mlid($path) {
$mlid = null;
$tree = menu_tree_all_data('primary-links');
foreach($tree as $item) {
if ($item['link']['link_path'] == $path) {
$mlid = $item['link']['mlid'];
break;
}
}
return $mlid;
}

OR operator in Drupal View Filters

I need to implement an OR operator between some filters in a Drupal View.
By default, Drupal AND's every filter together.
By using
hook_views_query_alter(&$view, &$query)
I can access the query ( var $query ) , and I can change either :
$query->where[0]['type']
to 'OR', or
$query->group_operator
to 'OR'
The problem is however, that I do not need OR's everywhere. I've tried changing both of them to OR seperately, and it doesn't yield the desired result.
It seems changing those values, puts OR's everywhere, while I need => ( filter 1 AND filter 2 ) OR ( filter 3 ), so just 1 OR.
I could just check the Query of the View, copy it, modify it, and run it through db_query, but that's just dirty ..
Any suggestions ?
Thx in advance.
If you are using Views 3 / Drupal 7 and looking for the answer to this question, it is baked right into Views. Where it says "add" next to filters, click the dropdown, then click "and/or; rearrange". It should be obvious from there.
Unfortunately this is still a missing feature in Views2. It has long been asked for and was promised a while ago, but seems to be a tricky piece of work and is now scheduled for Views3.
In the meantime you could try the Views Or module mentioned in that thread. As of today, it is still in dev status, but seems to be actively maintained and the issue queue does not look to bad, so you might want to give it a try.
if you want do it with view_query_alter hook, you should use $query->add_where() where you can specify if it's AND or OR. From views/include/query.inc
/**
* Add a simple WHERE clause to the query. The caller is responsible for
* ensuring that all fields are fully qualified (TABLE.FIELD) and that
* the table already exists in the query.
*
* #param $group
* The WHERE group to add these to; groups are used to create AND/OR
* sections. Groups cannot be nested. Use 0 as the default group.
* If the group does not yet exist it will be created as an AND group.
* #param $clause
* The actual clause to add. When adding a where clause it is important
* that all tables are addressed by the alias provided by add_table or
* ensure_table and that all fields are addressed by their alias wehn
* possible. Please use %d and %s for arguments.
* #param ...
* A number of arguments as used in db_query(). May be many args or one
* array full of args.
*/
function add_where($group, $clause)
I added it by concatenating the string.
It is relatively specific to the implementation - people would need to play with field to match for OR - node.title in the following code and the field to match it with - node_revisions.body in this case.
Extra piece of code to make sure that node_revisions.body is in the query.
/**
* Implementation of hook_views_api().
*/
function eventsor_views_api() { // your module name into hook_views_api
return array(
'api' => 2,
// might not need the line below, but in any case, the last arg is the name of your module
'path' => drupal_get_path('module', 'eventsor'),
);
}
/**
*
* #param string $form
* #param type $form_state
* #param type $form_id
*/
function eventsor_views_query_alter(&$view, &$query) {
switch ($view->name) {
case 'Events':
_eventsor_composite_filter($query);
break;
}
}
/**
* Add to the where clause.
* #param type $query
*/
function _eventsor_composite_filter(&$query) {
// If we see "UPPER(node.title) LIKE UPPER('%%%s%%')" - then add and to it.
if (isset($query->where)) {
$where_count = 0;
foreach ($query->where as $where) {
$clause_count = 0;
if (isset($where['clauses'])) {
foreach ($where['clauses'] as $clause) {
$search_where_clause = "UPPER(node.title) LIKE UPPER('%%%s%%')";
// node_data_field_long_description.field_long_description_value
$desirable_where_clause = "UPPER(CONCAT_WS(' ', node.title, node_revisions.body)) LIKE UPPER('%%%s%%')";
if ($clause == $search_where_clause) {
// $query->add_where('or', 'revisions.body = %s'); - outside of what we are looking for
$query->where[$where_count]['clauses'][$clause_count] = $desirable_where_clause;
// Add the field to the view, just in case.
if (!isset($query->fields['node_revisions_body'])) {
$query->fields['node_revisions_body'] = array(
'field' => 'body',
'table' => 'node_revisions',
'alias' => 'node_revisions_body'
);
}
}
$clause_count++;
}
}
$where_count++;
}
}
}

Categories

Resources