3

Hello I created the following form iterating for each node:

...
$render['my_list'][] = drupal_get_form('shop_form_item_'.$node->nid, $node->nid);
...

$render will output the form on the page. The forms will be shop-form-item-20, shop-form-item-21 etc...

function shop_forms($form_id, $args) {
  $forms = array();
      if(strpos($form_id, 'shop_form_item_') === 0) {
        $forms[$form_id] = array('callback' => 'shop_form',);
      }
  return $forms;
}

function shop_form($form, &$form_state, $nid) {
  $form = array();

    $form['my-item-name'] = array(
    '#type' => 'hidden',
    '#title' => t('Name'),
    '#value' => isset($node->title) ? $node->title : '',

  );


  return $form;
}

Now I need to add a class: <form method="post" action="" class="cart"> How do I insert it?

Thanks

4

You need to add it to the #attributes key of the $form array:

function shop_form($form, &$form_state, $nid) {
  $form['#attributes']['class'][] = 'cart'; // Classes are added to an array and internally converted to a single string by Drupal later on

  $form['my-item-name'] = array(
    '#type' => 'hidden',
    '#title' => t('Name'),
    '#value' => isset($node->title) ? $node->title : '',
  );


  return $form;
}

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.