You are here

function discussthis_field_widget_form in Discuss This! 7.2

Implements hook_field_widget_form().

Define the field widget configuration form.

File

./discussthis.fields.inc, line 246
DiscussThis field declaration using Field Types API.

Code

function discussthis_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $value = isset($items[$delta]['rgb']) ? $items[$delta]['rgb'] : '';
  $widget = $element;
  $widget['#delta'] = $delta;
  switch ($instance['widget']['type']) {
    case 'field_discussthis_widget':

      // If the user has no rights to override the Discuss This! settings: return.
      if (!user_access('override discuss this forums')) {
        return;
      }

      // We know it is a node, attempt loading info about it
      // WARNING: $nid can be NULL or 0 when creating a new node.
      $nid = $form['nid']['#value'];
      $type = $form['type']['#value'];

      // Only modify the form if this node type is discussthis-enabled.
      // Note that a type may be turned off, but if the node already
      // had a discussthis post attached to it, then it is still editable
      $forum_tid = _discussthis_get_forum($nid, $type);
      if (!$forum_tid) {
        return;
      }
      $widget += array(
        '#type' => 'fieldset',
        '#title' => t('DiscussThis!'),
        '#element_validate' => array(
          'field_discussthis_form_validate',
        ),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#tree' => TRUE,
        '#delta' => $delta,
        '#group' => 'additional_settings',
      );
      $name = '';

      // Retrieve the topic nid attached with this node.
      // NOTE: the topic is 0 if no comments as yet been done.
      $topic_nid = _discussthis_get_topic($nid);
      if ($topic_nid) {

        // there is a topic linked, is the node still available?
        $topic = node_load($topic_nid);
        if ($topic) {

          // TODO: also get the name of the forum to display to the user
          $name = '<strong>' . t('Current topic #@nid: @title', array(
            '@title' => $topic->title,
            '@nid' => $topic_nid,
          )) . '</strong><br /><br />';
        }
        else {
          $sql = "DELETE FROM {discussthis} WHERE nid = %d";
          db_delete('discussthis')
            ->condition('nid', $nid)
            ->execute();
          unset($topic_nid);
        }
      }

      // This case is when no topic exists for the current node: ie no yet comment.
      if (!$topic_nid) {

        // Let users override the default module of new discussion
        // (this is not shown unless there is a discussion).
        // Retrieve the , or "No Discuss This!"
        $discussthis_forums = _discussthis_build_forums_selection_list();
        $discussthis_forums[0] = 'No Discuss This!';
        $widget['discussthis_forum'] = array(
          '#type' => 'select',
          '#title' => t('Forum for new discussion'),
          '#required' => TRUE,
          '#description' => t('Select the forum where the first discussion about this node will be created. Or select "No Discuss This!" to prevent discussions on this node. Note: if a topic is attached to this node, then this parameter will have no effect.'),
          '#options' => $discussthis_forums,
          '#default_value' => $forum_tid,
        );
      }

      // let the user assign a forum topic to this node
      // this can be done at the time a node is being created
      // any forum is acceptable (although we could have flags to know whether that's the case...)
      $widget['discussthis_topic'] = array(
        '#type' => 'textfield',
        '#title' => t('Identifier of the forum topic to attach to this node'),
        '#description' => $name . t('Override default topic associated with this node. Leave empty for default topic. Enter a topic name and use autocompletion to associate this node with an existing topic.'),
        '#default_value' => $topic_nid ? $topic_nid : '',
        '#autocomplete_path' => 'discussthis/autocomplete',
        '#maxlength' => 255,
      );
      break;
  }
  $element['discussthis'] = $widget;
  return $element;
}