0

I have created a content type with some field API fields.

How can I create a edit form that allows to edit just the title, the node body, and those fields?

The normal node edit form contains extra form fields that are not necessary for my purpose (i.e. they are not for editing the parts of a node I listed).

  • Hello, and welcome to Drupal Answers. Do you mean accessing them programmatically? If it is so, from which hook/function do you need to access them? – kiamlaluno Jan 10 '13 at 11:30
  • @kiamlaluno ya programmatically ... i tried with drupal_get_form(), but its gives to much of unwanted things – Soni Kishan Jan 10 '13 at 11:33
  • @SoniKishan Use drupal_get_form with your content type id. – Sumit Madan Jan 10 '13 at 11:34
  • @SumitMadan if i use drupal_get_form() than i have to go through so many filters, what i want is just form fields. (which can have field type,default value, options, etc).. – Soni Kishan Jan 10 '13 at 11:42
  • check this I just created drupalwithphp.blogspot.in/2013/01/… ;) – Sumit Madan Jan 10 '13 at 11:47
1

I get you want to show a form where the users can enter the values for the fields implemented using the field API, the node body, and the title; other form fields should not be shown.

Using the following code, you can achieve what you want.

function mymodule_node_form($form, $form_state, $node) {
  global $user;

  if (!isset($form_state['node'])) {
    if (!isset($node->title)) {
      $node->title = NULL;
    }
    node_object_prepare($node);
    $form_state['node'] = $node;
  }
  else {
    $node = $form_state['node'];
  }

  $form['#node_edit_form'] = TRUE;

  $form['#attributes']['class'][] = 'node-form';
  if (!empty($node->type)) {
    $form['#attributes']['class'][] = 'node-' . $node->type . '-form';
  }

  foreach (array('nid', 'vid', 'uid', 'created', 'type', 'language') as $key) {
    $form[$key] = array(
      '#type' => 'value', 
      '#value' => isset($node->$key) ? $node->$key : NULL,
    );
  }

  $form['changed'] = array(
    '#type' => 'hidden', 
    '#default_value' => isset($node->changed) ? $node->changed : NULL,
  );

  $function = node_type_get_base($node) . '_form';
  if (function_exists($function) && ($extra = $function($node, $form_state))) {
    $form = array_merge_recursive($form, $extra);
  }
  if (isset($form['title']) && !isset($form['title']['#weight'])) {
    $form['title']['#weight'] = -5;
  }

  $form['#node'] = $node;

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit', 
    '#access' => variable_get('node_preview_' . $node->type, DRUPAL_OPTIONAL) != DRUPAL_REQUIRED || (!form_get_errors() && isset($form_state['node_preview'])), 
    '#value' => t('Save'), 
    '#weight' => 5, 
    '#submit' => array('node_form_submit'),
  );
  if (!empty($node->nid) && node_access('delete', $node)) {
    $form['actions']['delete'] = array(
      '#type' => 'submit', 
      '#value' => t('Delete'), 
      '#weight' => 15, 
      '#submit' => array('node_form_delete_submit'),
    );
  }

  // Add the validation handlers.

  field_attach_form('node', $node, $form, $form_state, entity_language('node', $node));
  return $form;
}

This code is taken from node_form(); it is a ripped off version that include only what necessary to allow to enter the title, the body content, and the values for any field implemented using the field API.
As you see, the code still need to define the form validation handler, and the form submission handler. You use the code of node_form_validate() as guideline for your validation handler.

The form builder can be used for a menu item as the following one.

function mymodule_menu() {
  $items['mymodule/%node/edit'] = array(
    'title' => 'Edit', 
    'page callback' => 'drupal_get_form', 
    'page arguments' => array('mymodule_node_form', 1), 
    'access arguments' => array('the permission users should have to see this menu item'), 
  );

  return $items;
}

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.