2

how can i alter/inject a form in a specific content type. I'm developing a module (product list/shopping cart) to insert a form after the body of a specific content type but right now it's showing up in all the existing content types.

The module was not initially developed by me so i'm having a bit of trouble picking up from where it was left…

Here is the part i think is relevant:

function ftf_product_form_node_form_alter(&$form, $form_state) {


  $products = db_select('ftf_product')->fields(NULL, array('pid', 'nid', 'name', 'font_ref', 'description', 'app', 'web', 'desktop',  'avangate_desktop', 'avangate_web', 'avangate_app'))->condition('nid', $form['nid']['#value'], '=');
  $result = $products->execute();
  foreach ($result as $record) {
    $defaults[] = $record;
  }

  if(empty($form_state['num_products'])) {
    $results = $products->countQuery()->execute()->fetchField();
    $form_state['num_products'] = $results;

    if($form_state['num_products']  == 0) {
      $form_state['num_products'] = 1;
    }
  }

  $form['products'] = array(
    '#type' => 'fieldset',
    '#title' => t('<strong>Typeface products</strong>'),
    '#collapsible' => TRUE,
    '#collapsed' => (($defaults[$form_state['num_products']-1]->name && !$_SESSION["collapse_typeface_product"])?TRUE:FALSE), 
    '#weight' => 5,
    '#tree' => TRUE,
    '#attributes' => array('class' => array('container-inline')),
    '#prefix' => '<div id="participants"><p>&nbsp;</p>',
    '#suffix' => '</div><p>&nbsp;</p>',
    '#description' => t('<p style="color:grey">Create, edit and delete products here. To add or edit Typeface Price Factors please visit the <a href="/admin/config/content/ftf_pricetable">admin section</a>.</br></br></p>'), 
  );
1
function ftf_product_form_node_form_alter(&$form, $form_state) {

  if($form['#node']->type == 'YOUR_NODE_TYPE'){
    $products = db_select('ftf_product')->fields(NULL, array('pid', 'nid', 'name', 'font_ref', 'description', 'app', 'web', 'desktop',  'avangate_desktop', 'avangate_web', 'avangate_app'))->condition('nid', $form['nid']['#value'], '=');
    $result = $products->execute();
    foreach ($result as $record) {
      $defaults[] = $record;
    }

    if(empty($form_state['num_products'])) {
      $results = $products->countQuery()->execute()->fetchField();
      $form_state['num_products'] = $results;

      if($form_state['num_products']  == 0) {
        $form_state['num_products'] = 1;
      }
    }

    $form['products'] = array(
      '#type' => 'fieldset',
      '#title' => t('<strong>Typeface products</strong>'),
      '#collapsible' => TRUE,
      '#collapsed' => (($defaults[$form_state['num_products']-1]->name && !$_SESSION["collapse_typeface_product"])?TRUE:FALSE), 
      '#weight' => 5,
      '#tree' => TRUE,
      '#attributes' => array('class' => array('container-inline')),
      '#prefix' => '<div id="participants"><p>&nbsp;</p>',
      '#suffix' => '</div><p>&nbsp;</p>',
      '#description' => t('<p style="color:grey">Create, edit and delete products here. To add or edit Typeface Price Factors please visit the <a href="/admin/config/content/ftf_pricetable">admin section</a>.</br></br></p>'), 
    );
  }
}
| improve this answer | |
0

Since you want to alter only one specific form, hook_form_FORM_ID_alter is ideal. Replace FORM_ID with the form_id of your content type (should be something like machine_name_node_form - print $form_id). That hook's implementation has the advantage of not being invoked by all forms, but only by the specified form. This is important in terms of performance.

Hope it helps!

| improve this answer | |

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.