Automatically Copying The Node Title To The Menu Title In Drupal 6
Note: This post is over two years old and so the information contained here might be out of date. If you do spot something please leave a comment and we will endeavour to correct.
13th April 2011 - 4 minutes read time
One new feature of Drupal 7 is that any title you give a node will be copied to the menu title field when you create a menu item. I wanted to replicate this functionality in Drupal 6 and so I created a function that did just that. I have used this function a few times in different projects so I'm posting it here.
This code uses the drupal_add_js() function to push a small amount of JavaScript code into the page when a node is added or edited. The hook hook_init() is used to add this content as it is run very early on in the Drupal boot cycle. I have tried putting this code into other hooks (like hook_preprocess_page()) but it isn't included, I would look into this further but this solution seems to work well.
The JavaScript added is a bit of jQuery code that looks at the title field of the node and will copy it to the menu title field if anything has been entered. It makes sure that nothing is already in the menu title field before doing the copying as it would otherwise overwrite any custom titles added. Not all nodes are menu items so the code is only run if the user clicks into the menu field, which replicates the Drupal 7 functionality.
/**
* Implementation of HOOK_init(). Used to add javascript to the node add and edit pages that copies the node title to the menu title field.
*/
function mymodule_init() {
if ((arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == 'edit') || (arg(0) == 'node' && arg(1) == 'add' && arg(2) != '')) {
drupal_add_js("
$(document).ready(function() {
$('#edit-menu-link-title').focus(function() {
if(this.value == '') {
this.value = $('#edit-title').val();
}
});
$('#edit-path').focus(function() {
if(this.v == '') {
this.value = $('#edit-title').val().toLowerCase().replace(' ', '_');
}
});
$('#edit-menu-link-title').keydown(function(event) {
if ($('#edit-menu-link-title').val().length < 80) {
$('#edit-menu-link-title').removeClass('error');
} else {
$('#edit-menu-link-title').addClass('error');
}
});
});
", 'inline');
}
}
This code also does a small amount of validation to encourage users from adding massive menu titles by adding an "error" class to the field if they add a long title. This is currently set to 80 characters, which seems like a good limit.
To use this function just add it to one of your own custom modules or even create a module for it.
XML is a useful format for configuration, data storage, and transmitting data from one system to another. As a human readable format that can be easily read by machines it quickly gained favor in lots of different systems as a mechanism for data storage.
Pi day was a few weeks ago, but I came across this simple approximation of pi recently and decided to put together an example in PHP since it seemed pretty simple.
This approximation of pi centers around a real world example, but we can simulate this using some code.
A parabolic curve is a type of curve where every point is an equal distance from a focal point. There a number of different way to generate this sort of curve using math, but one of the simplest is to use straight lines to create the illusion of the curve.
Comments
You could possibly do it via a hook_form_alter on the form and include the drupal_add_js() call in a form #after_build callback.
See http://api.drupal.org/api/drupal/developer--topics--forms_api_reference…
Submitted by budda on Thu, 11/03/2011 - 23:40
PermalinkAdd new comment