6

I have created a file field with multiple uploads allowed. Is there any way(except CSS) I can remove "Show row weights" link from file upload field? Or is there any way to remove weight drop down entirely from file field?

5
  • It is possible bot sure that the whole thing work. try hook_form_alter
    – niksmac
    Feb 18, 2013 at 8:33
  • Do you mean the extra top-right button with the four arrows so you can change the weight by integers? From a usability and cross-platform point-of-view, you should never remove that functionality, since it's the alternative for when you've got JS disabled or is not working as expected.
    – Ambidex
    Feb 19, 2013 at 9:45
  • My issue is that I want the extra top right button as it is but just want to remove the link. I have done is using locale module for now. Thanks for the input.
    – qasimzee
    Feb 21, 2013 at 9:36
  • I found a great article that explains how to do this in a way you can indicate to which fields apply it. enjoy it! commercialprogression.com/post/…
    – svelandiag
    Sep 13, 2014 at 7:18
  • Double up! All one has to do is pasting the code inside a .module, adding an .info and insert a field name within the last function. Put it on github here: github.com/leymannx/drupal-disable-weight-on-multi-value-fields
    – leymannx
    Mar 19, 2015 at 16:28

6 Answers 6

4

You need to disable/alter settings.tableDrag function.

<?php
/**
 * Implements hook_js_alter().
 */
function MODULENAME_js_alter(&$javascript) {
  unset($javascript['misc/tabledrag.js']);
}
?>
2
  • 2
    This removes the "Show row weights" link but when enabled it displays the Weight column. It's a good answer but a complete solution would include removal of the Weight column.
    – John81
    Jul 11, 2013 at 13:51
  • 2
    This will remove the ability to reorder the rows entirely, not just remove the link at the top of the page. Adding this code will break the ability to order items. USE AT YOUR OWN RISK.
    – jenlampton
    Sep 1, 2016 at 1:52
6

The first part of your question is regarding removal of link "Show row weights" (or "Hide row weights" if dragging functionality is disabled) in a form where table rows are draggable, so I'm going to answer that.

"Show row weights" string appears in Drupal.tableDrag.prototype.showColumns() method and "Hide row weights" appers in Drupal.tableDrag.prototype.hideColumns() method that is included in tabledrag.js file (see head in Drupal 7.x branch) so you need to override these methods in order to replace or remove these strings without losing row dragging functionality.

Place this code in a .js file that is loaded along with the form that uses tabledrag.js:

(function ($) {
  // Copy hideColumns() method
  var hideColumns = Drupal.tableDrag.prototype.hideColumns;
  Drupal.tableDrag.prototype.hideColumns = function() {
    // Call the original hideColumns() method
    hideColumns.call(this);
    // Remove the 'Show row weights' string
    $('.tabledrag-toggle-weight').text('');
  }

  // Copy showColumns() method
  var showColumns = Drupal.tableDrag.prototype.showColumns;
  Drupal.tableDrag.prototype.showColumns = function () {
    // Call the original showColumns() method
    showColumns.call(this);
    // Remove the 'Hide row weights' string
    $('.tabledrag-toggle-weight').text('');
  }
})(jQuery);
3
  • will it any effect on default misc/tabledrag.js ?
    – Fawwad
    May 7, 2015 at 6:31
  • @Fawwad, the only side effect of the provided code is that toggle element text is hidden. Default hideColumns() and showColumns() methods are still invoked.
    – maijs
    May 7, 2015 at 20:01
  • 1
    It works but it's much easier to simply add to your css: .tabledrag-toggle-weight-wrapper, .tabledrag-toggle-weight, .tableresponsive-toggle-columns, .tableresponsive-toggle { height: 0 !important; font-size: 0 !important; color: transparent !important; display: none !important; } Yes, it seems a bit overkill but, depending on various themes, some might not be enough.
    – Gábor
    May 30, 2018 at 22:03
3

Here is a solution for Drupal 8 using hook_preprocess_field_multiple_value_form()

First find the field you want to remove re-ordering ability from:

/**
 * Implements hook_preprocess_field_multiple_value_form().
 */
function MY_MODULE_preprocess_field_multiple_value_form(&$variables) {
  if($variables['element']['#field_name'] == 'MY_FIELD_NAME') {
    MY_MODULE_remove_table_reordering($variables['table']);
  }
}

And then call a helper function to actually remove the tables re-ordering functionality:

/**
 * Removes the ability for a table to be re-ordered.
 */
function MY_MODULE_remove_table_reordering(&$table) {
  unset($table['#tabledrag']);

  // Remove re-order header column
  foreach($table['#header'] as $header_i => $header) {
    if(is_object($header) && (string) $header == 'Order') {
      unset($table['#header'][$header_i]);
    }
  }

  // Loop table rows
  foreach($table['#rows'] as &$row) {

    // Remove draggable class from tr
    $i = array_search('draggable', $row['class']);
    if($i !== FALSE) {
      unset($row['class'][$i]);
    }

    // Loop columns in row
    foreach($row['data'] as $col_i => &$col) {

      if(empty($col['class'])) {
        continue;
      }

      // Remove td with drag handle
      $i = array_search('field-multiple-drag', $col['class']);
      if($i !== FALSE) {
        unset($row['data'][$col_i]);
      }

      // Remove td with re-ordering select
      $i = array_search('delta-order', $col['class']);
      if($i !== FALSE) {
        unset($row['data'][$col_i]);
      }
    }
  }

}
1

You can also try a theme-override, as in https://api.drupal.org/api/drupal/modules!field!field.form.inc/function/theme_field_multiple_value_form/7

Paste the function to your theme's template.php, rename 'theme_' in the function name to your theme's name and clear the cache.

Find the line saying

drupal_add_tabledrag($table_id, 'order', 'sibling', $order_class);

and comment it out.

1

I'm not sure about disabling for a single field, but I found myself wanting to disable the row dragging for an entire form.

monymirza suggested an answer to disable it entirely. However in D7, that approach is not reliable, because in some forms drupal_add_tabledrag() will be called after your hook_js_alter(). This is typical Drupal - everything is theoretically customizable, but in practice it's error prone and sensitive.

In the end I used the following hook_form_alter() to disable table drag for just one specific form:

<?php

/**                                                                                                                                
 * Implements hook_form_alter()                                                                                                    
 */                                                                                                                                
function sunflower_custom_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'field_ui_field_overview_form' && $form['#bundle'] == 'sunflower_habitat') {
    // The habitat form is wizardized based on row weights.  This is a sensitive thing, and drupal's row weight javascript is not at all sensitive, completely changing all values when it feels like it.  So we don't want that "feature" on this form.             

    // Disable it, if already enabled.                                                                                             
    $javascript = &drupal_static('drupal_add_js', array());
    unset($javascript['misc/tabledrag.js']);

    // Prevent it from being enabled later.                                                                                        
    $tabledrag_added = &drupal_static('drupal_add_tabledrag');
    $tabledrag_added = TRUE;
  }
}
?>
1

Adding on to moneymirza's answer to remove the weights and add a qualifyer, since we don't want to disable tabledrag.js from the entire site.

<?php
/**
 * Implements hook_js_alter().
 */
function MODULENAME_js_alter(&$javascript) {
  // Use a more specific page condition as desired.
  if(arg(0) == "node" && arg(2) == "edit") {
    unset($javascript['misc/tabledrag.js']);
    drupal_add_js(drupal_get_path('module', 'MODULENAME') . '/MODULENAME.js');
  }
}

Then make a MODULENAME.js file:

(function ($) {
  Drupal.behaviors.NoTabledragWeights = {
    attach: function (context, settings) {
      // Again, get more specific if desired.
      $('td:nth-child(2),th:nth-child(2)').hide();
    }
  };
}(jQuery));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.