2

I have a filefield multi-value CCK field and I want to be able to set the number of values that can be added based on some condition when the user is editing the existing node. Is there a way to handle this through a form_alter? I do not want to set the number of values to "unlimited" for this case. enter image description here

0

Use template.php and override the form functions and apply your logic there before rendering the form. Example override function

/* Overiding the question creation form */

function yourthemename_question_node_form($form) {
//  dsm($form);
  // Modify title text
  unset($form['title']['#title']);
  // Set a default value for the title field
    // Set a default value for the title field
  $title_value =   $form['title']['#value'];
  if($title_value ==''){
  $form['title']['#value'] = t('Enter Title Here'); 
}
  else { }
  $form['title']['#attributes'] = array(
       'onblur' => "if (this.value == '') {this.value = '".$form['title']['#value']."';} this.style.color = '#333333';",
         'onfocus' => "if (this.value == '".$form['title']['#value']."') {this.value = '';} this.style.color = '#333333';" );
  $title = drupal_render($form['title']);
  $body = drupal_render($form['body_field']['body']);
  $form['body_field']['format']['#collapsed'] = FALSE;
  $form['body_field']['format']['#collapsible'] = FALSE;
  $form['body_field']['format']['#title'] = NULL;
  $body_filter = drupal_render($form['body_field']['format']);
  $tags = drupal_render($form['taxonomy']['tags']);
  $thematic_areas = drupal_render($form['taxonomy']['47']);
  $buttons = drupal_render($form['buttons']);
  $remaining_form = drupal_render($form);
    return '<table><tbody style="border:none;"><tr><td style="width:73%;"><div id="form-title">'.$title.'</div>'.'<div id="form-body">'.$body.'<div id="body-format">'.$body_filter.'</div></div><span style="display:none";>'.$remaining_form.'</span>'.'</td><td style="vertical-align:top;">'.$thematic_areas.$tags.'<div id="publish-post">'.$buttons.'</div>'."</td></tr></tbody></table>"; 
}
  • That kinda helps...any idea on how to set the number of multiple values allowed without having to remove or hide the existing fields? – user2014 Oct 17 '11 at 18:27
0

You can use something like the following, this always shows 10 fields but can easily be adapted.

function MYMODULE_form_alter(&$form, $form_values, $form_id) {
  if (isset($form['#node']) && isset($form['type']['#value']) && $form_id == $form['type']['#value'] .'_node_form') {
    $field_name = 'MYFIELD';
    $startid = 0;
    foreach ($form[$field_name] as $k => $v) {
      if (isset($form[$field_name][$k]['#field_name'])) {
        $startid = $k > $startid ? $k : $startid;
      }
    }
    if ($startid <= 9) {
      $form[$field_name][$startid]['_weight']['#delta'] = 10;
      for ($i = $startid; $i <= 9; $i++) {
        $form[$field_name][$i] = $form[$field_name][$startid];
        $form[$field_name][$i]['#weight'] = $i;
        $form[$field_name][$i]['_weight']['#default_value'] = $i;
      }
    }
  }     
}

Your Answer

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