2

I have an entity reference of widget type = autocomplete. It's a collection of users where you can add multiple users.

Now I would like to disable the ordering of the users. So you can't order the users and the text Show row heights isn't show. Here you can see how it is now:

enter image description here

I tried to follow this tutorial but I'm stuck at the last step, he can't find the element['#field_name'] = 'myfield'.

This is what I have so far from the tutorial: (I'm using a custom module named other)

function other_theme_registry_alter(&$theme_registry) {
    if (isset($theme_registry['field_multiple_value_form'])) {
        $theme_registry['field_multiple_value_form']['type'] = 'module';
        $theme_registry['field_multiple_value_form']['theme path'] = drupal_get_path('module', 'other');
        $theme_registry['field_multiple_value_form']['function'] = 'other_theme_field_multiple_value_form';
    } 
}


/**
 * Theme function override for multiple-value form widgets.
 *
 * @see theme_field_multiple_value_form()
 */
function other_theme_field_multiple_value_form($variables) {
  $element = $variables['element'];
  $output = '';

  // The first condition is the override.
  if (($element['#cardinality'] > 1 || $element['#cardinality'] == FIELD_CARDINALITY_UNLIMITED) && isset($element[0]['#nodrag'])) {
    die;
    $table_id = drupal_html_id($element['#field_name'] . '_values');
    $required = !empty($element['#required']) ? theme('form_required_marker', $variables) : '';

    $header = array(
      array(
        'data' => '<label>' . t('!title !required', array('!title' => $element['#title'], '!required' => $required)) . "</label>",
        'class' => array('field-label'),
      ),
    );
    $rows = array();

    // Sort items according to weight
    $items = array();
    foreach (element_children($element) as $key) {
      if ($key === 'add_more') {
        $add_more_button = &$element[$key];
      }
      else {
        $items[] = &$element[$key];
      }
    }
    usort($items, '_field_sort_items_value_helper');

    // Add the items as table rows.
    foreach ($items as $key => $item) {
      // We don't want the weight to render.
      unset($item['_weight']);
      $cells = array(
        drupal_render($item),
      );
      $rows[] = array(
        'data' => $cells,
      );
    }

    $output = '<div class="form-item">';
    $output .= theme('table', array(
      'header' => $header,
      'rows' => $rows,
      'attributes' => array(
        'id' => $table_id,
        'class' => array('field-multiple-table'),
      ),
    ));
    $output .= $element['#description'] ? '<div class="description">' . $element['#description'] . '</div>' : '';
    $output .= '<div class="clearfix">' . drupal_render($add_more_button) . '</div>';
    $output .= '</div>';
  }
  elseif ($element['#cardinality'] > 1 || $element['#cardinality'] == FIELD_CARDINALITY_UNLIMITED) {
    $table_id = drupal_html_id($element['#field_name'] . '_values');
    $order_class = $element['#field_name'] . '-delta-order';
    $required = !empty($element['#required']) ? theme('form_required_marker', $variables) : '';

    $header = array(
      array(
        'data' => '<label>' . t('!title !required', array('!title' => $element['#title'], '!required' => $required)) . "</label>",
        'colspan' => 2,
        'class' => array('field-label'),
      ),
      t('Order'),
    );
    $rows = array();

    // Sort items according to '_weight' (needed when the form comes back after
    // preview or failed validation).
    $items = array();
    foreach (element_children($element) as $key) {
      if ($key === 'add_more') {
        $add_more_button = &$element[$key];
      }
      else {
        $items[] = &$element[$key];
      }
    }
    usort($items, '_field_sort_items_value_helper');

    // Add the items as table rows.
    foreach ($items as $key => $item) {
      $item['_weight']['#attributes']['class'] = array($order_class);
      $delta_element = drupal_render($item['_weight']);
      $cells = array(
        array(
          'data' => '',
          'class' => array('field-multiple-drag'),
        ),
        drupal_render($item),
        array(
          'data' => $delta_element,
          'class' => array('delta-order'),
        ),
      );
      $rows[] = array(
        'data' => $cells,
        'class' => array('draggable'),
      );
    }

    $output = '<div class="form-item">';
    $output .= theme('table', array(
      'header' => $header,
      'rows' => $rows,
      'attributes' => array(
        'id' => $table_id,
        'class' => array('field-multiple-table'),
      ),
    ));
    $output .= $element['#description'] ? '<div class="description">' . $element['#description'] . '</div>' : '';
    $output .= '<div class="clearfix">' . drupal_render($add_more_button) . '</div>';
    $output .= '</div>';

    drupal_add_tabledrag($table_id, 'order', 'sibling', $order_class);
  }
  else {
    foreach (element_children($element) as $key) {
      $output .= drupal_render($element[$key]);
    }
  }

  return $output;
}


function other_field_widget_form_alter(&$element, &$form_state, $context) {
  if (isset($element['#field_name'])) {
    var_dump($element['#field_name']);
    switch ($element['#field_name']) {
      case 'field_co_authors': die;
        $element['#nodrag'] = TRUE;
      default:
        break;
    }
  }
}

As you can see I have a switch but the text is never equal to field_co_authors. The two fields that are an entity reference and can have multiple values don't show... .

Am I doing something wrong or is there another way to do this?

2
+50

$element['#field_name'] doesn't seem to be set for entityreference fields, this worked for me:

function other_field_widget_form_alter(&$element, &$form_state, $context) {
  if ($context['instance']['field_name'] == 'field_co_authors') {
    $element['#nodrag'] = TRUE;
  }
}

This is how I've modified the theme funciton:

function other_field_multiple_value_form($variables) {

  $element = $variables['element'];
  $output = '';

  if ($element['#cardinality'] > 1 || $element['#cardinality'] == FIELD_CARDINALITY_UNLIMITED) {
    $table_id = drupal_html_id($element['#field_name'] . '_values');
    $order_class = $element['#field_name'] . '-delta-order';
    $required = !empty($element['#required']) ? theme('form_required_marker', $variables) : '';

    $header = array(
      array(
        'data' => '<label>' . t('!title !required', array('!title' => $element['#title'], '!required' => $required)) . "</label>",
        'colspan' => 2,
        'class' => array('field-label'),
      ),
    );
    $rows = array();

    if (!isset($element[0]['#nodrag'])) {
      $header[] = t('Order');
    }

    // Sort items according to '_weight' (needed when the form comes back after
    // preview or failed validation)
    $items = array();
    foreach (element_children($element) as $key) {
      if ($key === 'add_more') {
        $add_more_button = &$element[$key];
      }
      else {
        $items[] = &$element[$key];
      }
    }
    usort($items, '_field_sort_items_value_helper');

    // Add the items as table rows.
    foreach ($items as $key => $item) {
      $item['_weight']['#attributes']['class'] = array($order_class);
      $delta_element = drupal_render($item['_weight']);
      $cells = array(
        array('data' => '', 'class' => array('field-multiple-drag')),
        drupal_render($item),
      );
      if (!isset($element[0]['#nodrag'])) {
        $cells[] = array('data' => $delta_element, 'class' => array('delta-order'));
      }
      $rows[] = array(
        'data' => $cells,
        'class' => array('draggable'),
      );
    }

    $output = '<div class="form-item">';
    $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => $table_id, 'class' => array('field-multiple-table'))));
    $output .= $element['#description'] ? '<div class="description">' . $element['#description'] . '</div>' : '';
    $output .= '<div class="clearfix">' . drupal_render($add_more_button) . '</div>';
    $output .= '</div>';

    if (!isset($element[0]['#nodrag'])) {
      drupal_add_tabledrag($table_id, 'order', 'sibling', $order_class);
    }
  }
  else {
    foreach (element_children($element) as $key) {
      $output .= drupal_render($element[$key]);
    }
  }

  return $output;
}

UPDATE:

To change the "Add another item" text, I think you'll need to implement a form alter. Assuming the node type is page, this is how to do it:

function other_form_page_node_form_alter(&$form, &$form_state, $form_id) {
  $language = $form['field_co_authors']['#language'];
  $form['field_co_authors'][$language]['add_more']['#value'] = t('Add another user');
}

or you can use the general hook_form_alter if you need this in multiple content types.

There is some discussion on drupal.org about adding the appropriate alter function to drupal core here: Add hook alter to override field_multiple_value_form()

2
  • Thanks, that works. But the problem is that I set the button (add another item) to "Add another user" but he overwrites it with "Add another item"..
    – nielsv
    Sep 15 '14 at 12:17
  • @nielsv: I'm afraid you can only do it with a hook form alter: I rather update to original answer...
    – Peter
    Sep 15 '14 at 12:40
0

You could hide the drag handle and the "show row weights" text using css. Also, you could use hook_form_alter() where you can add your own validation function to reset the weight field to 0 (just in case someone changes the value using firebug / inspector)

0

I suggest you you can do it by jquery, simple and quick. add js that load only in this page by drupal_add_js

then worte

  $('.field-multiple-drag').remove();

in your coustom module in hook_form_alter

if($form_id=='your_form_name'){
drupal_add_js(drupal_get_path('module', 'yourmodule') . '/removesortable.js');
}

then in js file

//removesortable.js
(function($) {
    Drupal.behaviors.yourmodule = {
        attach: function(context) {
        $('.field-multiple-drag').remove();
    }
  }
})(jQuery);

and enjoy :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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