summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBen Davis2012-11-21 22:51:43 (GMT)
committer Ben Davis2012-11-21 22:51:43 (GMT)
commit74b5be6810960fb60e10b78aa52c4af4de115975 (patch)
tree220ec65c911aed34b17a1a4bac966e255a8b0d13
parent5b88f4ee011f386635663ab9beb316d4f1889812 (diff)
Issue #1816116 by ScratchFury, 10oclock: Menu weight improvements.
-rw-r--r--weight.js16
-rw-r--r--weight.module72
2 files changed, 84 insertions, 4 deletions
diff --git a/weight.js b/weight.js
index fd39d92..a09ddd4 100644
--- a/weight.js
+++ b/weight.js
@@ -17,6 +17,22 @@ Drupal.behaviors.weight = {
$('fieldset#edit-weight', context).drupalSetSummary(function(context) {
return Drupal.t('Weight: !weight', {'!weight' : $('#edit-weight-weight option:selected').val()});
});
+
+ // Force range to 50 when menu weight is selected.
+ if ($('input:radio[name="weight_menu"]:checked').val()) {
+ $('#edit-weight-range-50').click();
+ $('input:radio[name="weight_range"]').attr('disabled', 'disabled');
+ }
+
+ $('input:radio[name="weight_menu"]').change(function() {
+ if ($(this).val() == 1) {
+ $('#edit-weight-range-50').click();
+ $('input:radio[name="weight_range"]').attr('disabled', 'disabled');
+ }
+ else {
+ $('input:radio[name="weight_range"]').removeAttr('disabled');
+ }
+ });
}
};
diff --git a/weight.module b/weight.module
index aef534f..e6667b6 100644
--- a/weight.module
+++ b/weight.module
@@ -311,7 +311,7 @@ function weight_form_views_form_alter(&$form, &$form_state, $form_id) {
function weight_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
if ($entity_type == 'node') {
$settings = _weight_get_settings($entity->type);
- if (isset($settings['enabled']) && $settings['enabled']) {
+ if (isset($settings['enabled']) && $settings['enabled'] && !$settings['menu_weight']) {
$form['weight'] = array(
'#type' => 'fieldset',
'#title' => t('Weight'),
@@ -360,6 +360,21 @@ function weight_node_update($node) {
}
/**
+ * Implements hook_menu_link_insert().
+ */
+function weight_menu_link_insert($link) {
+ _weight_set_weight_from_menu_link($link);
+
+}
+
+/**
+ * Implements hook_menu_link_update().
+ */
+function weight_menu_link_update($link) {
+ _weight_set_weight_from_menu_link($link);
+}
+
+/**
* Implements hook_views_api().
*/
function weight_views_api() {
@@ -427,10 +442,25 @@ function weight_apachesolr_query_prepare($query) {
* Set the weight of a node.
*/
function _weight_set_weight($node) {
+ // Default weight value: passed in weight value
+ $weight = $node->weight_weight;
+
+ // Get the settings for this node
+ $settings = _weight_get_settings($node->type);
+
+ // If menu_weight selected & exists, override menu weight
+ if ($settings['menu_weight']) {
+ // Get the menu weight for this node (if it exists).
+ $weight = db_select('menu_links', 'ml');
+ $weight->fields('ml', array('weight'))
+ ->condition('link_path', 'node/' . $node->nid);
+ $weight = $weight->execute()->fetchField();
+ }
+
if (module_exists('translation') && $node->nid != $node->tnid) {
$settings = _weight_get_settings($node->type);
if ($settings['sync_translations']) {
- $node->weight_weight = _weight_get_weight($node->tnid);
+ $weight = _weight_get_weight($node->tnid);
}
}
@@ -439,10 +469,10 @@ function _weight_set_weight($node) {
->fields(array(
'entity_id' => $node->nid,
'entity_type' => 'node',
- 'weight' => $node->weight_weight,
+ 'weight' => $weight,
))
->updateFields(array(
- 'weight' => $node->weight_weight,
+ 'weight' => $weight,
))
->execute();
}
@@ -473,6 +503,40 @@ function _weight_get_weight($node) {
}
/**
+ * Set the weight of a node from the menu link.
+ */
+function _weight_set_weight_from_menu_link($link) {
+ // Block updating by users without appropriate permissions
+ if (user_access('assign node weight')) {
+ $node = _weight_get_node_from_menu_link($link);
+ $weight_types = _weight_get_types();
+ if (is_object($node) && in_array($node->type, $weight_types)) {
+ $settings = _weight_get_settings($node->type);
+ // Only use menu weight if that setting has been checked
+ if ($settings['menu_weight']) {
+ _weight_set_weight($node);
+ }
+ }
+ }
+}
+
+/**
+ * Get node from menu item
+ */
+function _weight_get_node_from_menu_link($link) {
+ // Default to no node being available
+ $node = FALSE;
+
+ // Check that link exists and is a node
+ if (is_array($link) && strpos($link['link_path'], 'node/') == 0) {
+ $nid = drupal_substr($link['link_path'], 5);
+ $node = node_load($nid);
+ }
+
+ return $node;
+}
+
+/**
* Get weight settings for a content type.
*/
function _weight_get_settings($type = NULL) {