I'm porting a Drupal 6 module to Drupal 7, and everything is working except one critical piece, and I can't figure it out. The module is phpFreeChat, and one of the things it needs to do is add some options to all the different Content Types available that let you choose whether to allow a chatbox on that type of node. The code worked in Drupal 5 and Drupal 6, but Drupal 7 is very different. There is no longer a "Workflow" section in the Content Type administration area. This is the Drupal 6 code that worked. I have been trying for hours, and I cannot find a Drupal 7 equivalent. Can someone help me?

/**
 * Implementation of hook_form_alter() - alterations before a form is rendered.
 */
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
    $form['workflow']['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.<br />' . 'Always: Nodes of this content type will always have a chat room.<br />' . 'Per Node: Nodes of this content type can choose to have a chat room if desired.'),
    );
    $form['workflow']['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['workflow']['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['workflow']['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. <em>Channel1, Channel2</em>) . Leaving this ' . 'blank will use the global or node channel(s), or the node title if these are also blank.'),
    );
  }
  elseif (isset($form['type'])) {
    // Node edit form
    if (phpfreechat_nodedata($type) && user_access('create nodes with chatrooms')) {
      $form['phpfreechat'] = array(
        '#type' => 'fieldset',
        '#title' => t('phpFreeChat 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 by commas (e.g. <em>Channel1, Channel2</em>) . ' . 'You can leave this blank to use the default.'),
        );
      }
    }
  }
}

Comments

permutations’s picture

From what I read on another Web site, this should work in Drupal 7. I guess I just have a bug in my code somewhere. This is what I have so far (Drupal 7 version). It doesn't work at all - nothing appears in the form.

/**
 * 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.<br />' . 'Always: Nodes of this content type will always have a chat room.<br />' . '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. <em>Channel1, Channel2</em>). 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. <em>Channel1, Channel2</em>). You can leave this blank to use the default.'),
        );
      }
    }
  }
}
permutations’s picture

I found part of the problem (and at least I know the general direction of the problem).

This line:

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

didn't work, but this line did:

if ($form_id == 'node_type_form') {

So the way different types of nodes are identified is different in Drupal 7. I'm able to identify the Content Type edit form now, but I'm still not identifying the Node edit form. That's changed in some way, too. Anyone know how?

Do people actually come to these forums? I almost never get a response when I post. Drupal is very difficult. There is no backward compatibility (with pride) and the documentation is voluminous and obscure. And no one ever responds in the forums. Or maybe people only respond to their friends?

permutations’s picture

I just realized that the other bug has been there from the very beginning. I just tested it on Drupal 5 and Drupal 6, and the bug is on both those platforms, too. I took over this module when it was a Drupal 4 module, and it was so full of bugs it was unusable. I squashed most of them, but I missed this one. I got the additional form fields working on the Content Types page (that was critical), but I never got them to appear on the Node Edit form, and I didn't notice that I didn't. I'd like to fix it now, but I don't even know where to start since I never had working code.

Can anyone point me in the right direction?

Jaypan’s picture

I'm not clear on what the problem is. Your last post seems to indicate that it's not what you had been describing in the first posts.

What is/isn't happening, and what do you want to happen?