1

I'm porting a Drupal 6 module to Drupal 7. In Drupal 5 & Drupal 6, there was a Workflow section in the Content Types area and the additional fields went there. This is gone in Drupal 7. I tried to add a custom section, but it's not working at all.

Here is my code:

/**
* Implements hook_form_alter() - adds fields to Content Type forms.
*/
function phpfreechat_form_alter(&$form, &$form_state, $form_id) {
  global $user;
  $type = (isset($form['type']) && isset($form['type']['#value'])) ? $form['type']['#value'] : NULL;
  $node = isset($form['#node']) ? $form['#node'] : NULL;

  if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
    // Content-type edit form
    // Define field set in additional settings group
    $form['phpfreechat'] = array(
      '#type' => 'fieldset',
      '#title' => t('phpFreeChat settings'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => 10,
      '#group' => 'additional_settings',
    );
    $form['phpfreechat']['phpfreechat_nodeapi'] = array(
      '#type' => 'radios',
      '#title' => t('Show chat room'),
      '#default_value' => variable_get('phpfreechat_nodeapi_' . $form['#node_type']->type, 'never'),
      '#options' => array(
        'never' => t('Never'),
        'always' => t('Always'),
        'pernode' => t('Per Node'),
      ),
      '#description' => t('None: Nodes of this content type can never have a chat room.
' . 'Always: Nodes of this content type will always have a chat room.
' . 'Per Node: Nodes of this content type can choose to have a chat room if desired.'), ); $form['phpfreechat']['phpfreechat_nodeapi_custom'] = array( '#type' => 'checkbox', '#title' => t('Allow node to set title & channels'), '#return_value' => 1, '#default_value' => variable_get('phpfreechat_nodeapi_custom_' . $form['#node_type']->type, ''), '#description' => t('If this is checked then node editors will be able to set the chat ' . 'title and select what channels to join.'), ); $form['phpfreechat']['phpfreechat_nodeapi_title'] = array( '#type' => 'textfield', '#title' => t('Default Chat Channel Title'), '#default_value' => variable_get('phpfreechat_nodeapi_title_' . $form['#node_type']->type, ''), '#size' => 70, '#maxlength' => 128, '#description' => t('This is the default channel title for this content type. ' . 'Leaving this blank will use the global or node chat ' . 'title, or the node title if these are also blank.'), ); $form['phpfreechat']['phpfreechat_nodeapi_channels'] = array( '#type' => 'textfield', '#title' => t('Default Chat Channels'), '#default_value' => variable_get('phpfreechat_nodeapi_channels_' . $form['#node_type']->type, ''), '#size' => 70, '#maxlength' => 128, '#description' => t('Enter the default channel or channels to be joined for this content type. Separate multiple channels by commas (e.g. Channel1, Channel2). If you leave this blank, it will default to the global node channel(s), or the node title if this is also blank.'), ); } elseif (isset($form['type'])) { // Node edit form // Define field set in additional settings group if (phpfreechat_nodedata($type) && user_access($permission_create_chatboxes)) { $form['phpfreechat'] = array( '#type' => 'fieldset', '#title' => t('phpFreeChat Settings'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => 10, '#group' => 'additional_settings', ); if (variable_get('phpfreechat_nodeapi_' . $form['type']['#value'], 'never') == 'pernode') { $form['phpfreechat']['phpfreechat_enabled'] = array( '#type' => 'checkbox', '#title' => t('Show Chat Room'), '#return_value' => 1, '#default_value' => ($node->phpfreechat_enabled == 1 ? TRUE : FALSE), '#description' => 'If this is selected a chat room will be displayed below the content', ); } if (variable_get('phpfreechat_nodeapi_custom_' . $form['type']['#value'], '') == TRUE) { $form['phpfreechat']['phpfreechat_title'] = array( '#type' => 'textfield', '#title' => t('Chat Channel Title'), '#default_value' => $node->phpfreechat_title, '#size' => 70, '#maxlength' => 128, '#description' => t('This is the chat title. You can leave this blank to use the default.'), ); $form['phpfreechat']['phpfreechat_channels'] = array( '#type' => 'textfield', '#title' => t('Chat Channel'), '#default_value' => $node->phpfreechat_channels, '#size' => 70, '#maxlength' => 128, '#description' => t('Enter the channel or channels to be joined for this chat. Separate multiple channels with commas (e.g. Channel1, Channel2). You can leave this blank to use the default.'), ); } } } }

I posted it on the Drupal forum, but they're too arrogant to answer questions over there. They ignore everything I post.

https://www.drupal.org/node/2431113#comment-9648927

2
  • To post code on this site, follow the instructions in the text editor: put a tab or four spaces in front of every line. Most code editing software will do this for you easily.
    – Darvanen
    Commented Feb 22, 2015 at 1:22
  • To post code on this site, you may also wrap the code block in HTML (<pre>). Commented Feb 22, 2015 at 6:27

2 Answers 2

0

Looking at the code you posted:

if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {

I don't think there's a $form['identity'] defined in node_type_form, so your form fields would not be added at all. If you're checking for a valid content type, you can look for $form['#node_type'].

1
  • Thank you, yes! That was it. I changed the if-statement to remove the second clause and it worked. Commented Feb 28, 2015 at 1:45
0

Thanks very much. That turned out to be it. I found the answer in this response to another question:

https://drupal.stackexchange.com/a/5891/44501

I changed the "if" statement to:

if ($form_id == 'node_type_form')

and it worked. :)

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.