2

This may be posted elsewhere but I was having trouble finding anything answering my question directly.

I have a content type called Ticket that I want to have different fields based on what page a user is on when they push the button to create new ticket.

I am doing this because I would like a grand overview of all tickets entered, but there is different information needed from the user based on what queue they are entering a ticket for.

If there is a better way to do this I am open to suggestions.

Thanks in advance

1
  • Like if you go to/node/add/ticket?hello hello gets filled for some field??? or do you mean show different fields than if they went to /node/add/ticket?world ??? PS: Which Drupal version are you using, 7 or 8?
    – No Sssweat
    Commented Jul 13, 2016 at 4:35

1 Answer 1

2

For passing values to forms:

There is a Drupal Module called Prepopulate which allows you to prefill fields on node forms using $_REQUEST variables.

It is quite simple to use and merely requires you to rewrite the links to contain the field values you want to pass along. If you want to fill field_your_fieldon your ticket node, the link would need to look like this:

http://www.example.com/node/add/ticket?edit[field_your_field]=desired field value

That said, this wont hide any fields on the form, merely provide default values for them. If you want tickets with different fields, you could always use multiple node types.


If you want to actually hide fields based on the url, you could reuse the node form on custom pages and manipulate their fields there as you need.

You will need menu entries for your different node displays to distinguish which node form you want to call.

function ticket_module_menu() {
  $items['ticket/add/tech-ticket'] = array(
    'page callback' => 'drupal_get_form',
    'page arguments' => array('ticket_module_tech_ticket'),
    'access callback' => 'create ticket content', //or put a custom permission
  );

  $items['ticket/add/support-ticket'] = array(
    'page callback' => 'drupal_get_form',
    'page arguments' => array('ticket_module_support_ticket'),
    'access callback' => 'create ticket content', //or put a custom permission
  );

  return $items;
}

And then create a form for each entry:

function ticket_module_tech_ticket() {
  module_load_include('inc', 'node', 'node.pages');

  //get node add form for node type ticket
  $form = node_add('ticket');

  //Hide fields you do not want to display
  $form['field_your_field1']['#access'] = 0;
  $form['field_your_field1']['und'][0]['value']['#disabled'] = TRUE;

  return $form;
}

function ticket_module_support_ticket() {
  module_load_include('inc', 'node', 'node.pages');

  //get node add form for node type ticket
  $form = node_add('ticket');

  //Hide fields you do not want to display
  $form['field_your_field2']['#access'] = 0;
  $form['field_your_field2']['und'][0]['value']['#disabled'] = TRUE;

  return $form;
}
2
  • I will give that a try, What I will try doing is make a field group with conditional rules that show different fields based on what "Queue" is chosen. Thanks for the nudge in the right direction Commented Jul 12, 2016 at 19:15
  • You should be able to manipulate field groups with the approach above as well, as they merely change the setup of the render array. I forgot to mention that you could also combine both forms into one, using a wildcard in the url $items['ticket/add/%'] rather than distinct paths, but if you have many variations and do a lot of manipulations multiple functions are easier to maintain.
    – Zuzuesque
    Commented Jul 13, 2016 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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