Drupal 7模块递归提交

时间:2017-02-15 19:22:03

标签: module drupal-7

我正在构建一个允许用户选择联系人的Drupal模块。如果他们选择联系人并且联系人有孩子,那么他们可以选择子孩子,例如Stackoverflow> C ++>标记。虽然我在PHP中对此编程很好,但我很难在Drupal 7中使用此代码。欢迎任何帮助:-)。我似乎对Drupal处理表单提交的方式有疑问(例如,渲染表单呈现两次)。

SQL

CREATE TABLE `contacts` (
  `id` int(11) NOT NULL,
  `parent_id` int(11) DEFAULT NULL,
  `title` varchar(100) NOT NULL,
  `description` text,
  `link` varchar(250) NOT NULL,
  `tel` varchar(15) DEFAULT NULL,
  `tel_toll_free` varchar(15) DEFAULT NULL,
  `email_address` varchar(50) DEFAULT NULL,
  `mail_address` varchar(500) DEFAULT NULL,
  `reply_msg` text,
  `image` varchar(255) DEFAULT NULL,
  `relevant_links` text,
  `fax` varchar(15) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `contacts` (`id`, `parent_id`, `title`, `description`, `link`, `tel`, `tel_toll_free`, `email_address`, `mail_address`, `reply_msg`, `image`, `relevant_links`, `fax`) VALUES

 (1, NULL, 'title', 'description', 'link-1', 'tel', 'tel_toll_free', 'email_address', 'mail_address', 'reply_msg', 'image', 'relevant_links', 'fax'),
 (2, 1, 'title', 'description', 'link-2', 'tel', 'tel_toll_free', 'email_address', 'mail_address', 'reply_msg', 'image', 'relevant_links', 'fax'),
 (3, 2, 'title', 'description', 'link-3', 'tel', 'tel_toll_free', 'email_address', 'mail_address', 'reply_msg', 'image', 'relevant_links', 'fax'),
 (4, 3, 'title', 'description', 'link-4', 'tel', 'tel_toll_free', 'email_address', 'mail_address', 'reply_msg', 'image', 'relevant_links', 'fax'),
 (5, 4, 'title', 'description', 'link-5', 'tel', 'tel_toll_free', 'email_address', 'mail_address', 'reply_msg', 'image', 'relevant_links', 'fax'),
 (6, NULL, 'title', 'description', 'link-6', 'tel', 'tel_toll_free', 'email_address', 'mail_address', 'reply_msg', 'image', 'relevant_links', 'fax');

构建递归选择器

function contact_us_selector($array, $selected = NULL, $placeholder = false, $parent_id = 0){
  if(is_array($array) && count($array) > 0) {
    $select_box['#type'] = 'select';
    $select_box['#attributes'] = array('class'=> 'jumper', 'onchange' => 'this.form.submit();');

    if($placeholder){
      $select_box['#options']['null'] = t('Please Choose...');
      $select_box['#options_attributes']['null'] = array('selected' => 'selected', 'disabled' => 'disabled');
    }
    foreach($array as $row){
      $select_box['#options'][$row['link']] = $row['title'];
      if($row['link'] == $selected){
        $select_box['#options_attributes'][$row['link']] = array('selected' => 'selected');
      }
    }
    if($parent_id==NULL){
      $parent_id = 0;
    }
    return array('selector_'.$parent_id => $select_box);
  } else {
    return null;
  }
}

显示表格功能

function _contact_us_form($form_id, &$form_state, $conf) {
  error_reporting(-1);

  $form['#name'] = 'contact_selector';
  $form['#prefix'] = '<h1>Contact Us</h1><br/>';
  $form['#type'] = 'fieldset';

  $form['contact_selector']['#type'] = 'fieldset';
  $form['contact_selector']['#prefix'] = 'Who can we help you contact?';

  // find out selector's current count
  if(isset($form_state['values'])){
    $counter = 0;
    while ($form_state['values']['selector_'.($counter+1)]!=NULL){
      $counter++;
    }
    $contact['link'] = $form_state['values']['selector_'.$counter];

    // look up contact, if one is selected
    if(isset($contact['link'])){
      $results = db_query('SELECT `id`, `title`, `description`, `link`, `tel`, `tel_toll_free`, `email_address`, `mail_address`, `reply_msg`, `relevant_links`, `fax`
        FROM `contacts`
        WHERE `link` = :link
        LIMIT 1',
        array(':link' => $contact['link'])
      );
      if($results->rowCount()>0){
        foreach($results as $row){
          $contact['id'] = $row->id;
          $contact['title'] = $row->title;
          $contact['description'] = $row->description;
          $contact['link'] = $row->link;
          $contact['tel'] = $row->tel;
          $contact['tel_toll_free'] = $row->tel_toll_free;
          $contact['email_address'] = $row->email_address;
          $contact['mail_address'] = $row->mail_address;
          $contact['reply_msg'] = $row->reply_msg;
          $contact['relevant_links'] = $row->relevant_links;
          $contact['fax'] = $row->fax;
        }
      }
    }
  }
  // generate select boxes for previous items
  if(isset($contact['id'])){
    // look up parents of selected contact
    $results = db_query('SELECT `T2`.`id`, `T2`.`parent_id`, `T2`.`link`, `T2`.`title`
      FROM (SELECT @r AS _id, (SELECT @r := `parent_id` FROM `contacts` WHERE `id` = _id) AS `parent_id`, @l := @l +1 AS `lvl` FROM (SELECT @r := :id, @l :=0) vars, `contacts` m WHERE @r <>0) `T1`
      JOIN `contacts` `T2` ON T1._id = `T2`.`id`
      ORDER BY `T1`.`lvl` DESC
      LIMIT 10;',
      array(':id' => $contact['id'])
    );
    // generate list of parent child elements
    if($results->rowCount()>0){
      $crumb_lvl = 0;
      foreach($results as $row){
        if($row->parent_id == NULL){
          $results2 = db_select('contacts')
            ->fields('contacts', array('title','link'))
            ->isNull('parent_id')
            ->orderBy('title', 'ASC')
            ->execute();
        } else {
          $results2 = db_select('contacts')
            ->fields('contacts', array('title','link'))
            ->condition('parent_id',$contact['id'],'=')
            ->orderBy('title', 'ASC')
            ->execute();
        }
        if($results2->rowCount()>0){
          $array = array();
          foreach($results2 as $row2){
            $array[] = array('title' => str_repeat('-',$crumb_lvl).' '.$row2->title, 'link' => $row2->link);
          }
          $form += contact_us_selector($array,$row->id,false,$row->parent_id);
        }
        unset($results2);
        $crumb_lvl++;
      }
    }
  }
  // generate current selection
  if($contact['id'] == NULL){
    $results = db_select('contacts')
      ->fields('contacts', array('title','link'))
      ->isNull('parent_id')
      ->orderBy('title', 'ASC')
      ->execute();
  } else {
    $results = db_select('contacts')
      ->fields('contacts', array('title','link'))
      ->condition('parent_id',$contact['id'],'=')
      ->orderBy('title', 'ASC')
      ->execute();
  }
  if($results->rowCount()>0){
    $array = array();
    foreach($results as $row){
      $array[] = array('title' => $row->title, 'link' => $row->link);
    }
    $form += contact_us_selector($array,null,true);
  }

  return $form;
}

1 个答案:

答案 0 :(得分:0)

问题在于选择器传递。使用$ _GET值可以很好地工作。

function contact_us_selector($array, $selected = NULL, $parent_id = 0){
  if(is_array($array) && count($array) > 0) {
    $select_box['#type'] = 'select';
    $select_box['#attributes'] = array('class'=> 'jumper', 'onchange' => 'javascript:location.href = this.value;');
    $select_box['#default_value'] = "?find={$selected}";
    if($selected==NULL){
      $select_box['#options']['null'] = t('Please Choose...');
      $select_box['#options_attributes']['null'] = array('selected' => 'selected', 'disabled' => 'disabled');
    }
    foreach($array as $row){
      $select_box['#options']["?find={$row['link']}"] = $row['title'];
    }
    if($parent_id==NULL){
      $parent_id = 0;
    }
    echo '<pre>';
    print_r(array('selector_'.$parent_id => $select_box));
    echo '</pre>';
    return array('selector_'.$parent_id => $select_box);
  } else {
    return;
  }
}

渲染

function _contact_us_form($form_id, &$form_state, $conf) {
  error_reporting(-1);

  //$form_state['config'] = $conf;
  //$form_state['rebuild'] = TRUE;
  //$form['#submit'][] = 'contact_us_form_submit';

  $form['#name'] = 'contact_selector';
  $form['#prefix'] = '<h1>Contact us</h1><br/>';
  $form['#type'] = 'fieldset';

  $form['contact_selector']['#type'] = 'fieldset';
  $form['contact_selector']['#prefix'] = 'Who can we help you contact?';

  // find out selector's current count
  if(isset($_GET['find'])){
    $contact['link'] = $_GET['find'];

    // look up contact, if one is selected
    if(isset($contact['link'])){
      $results = db_query('SELECT `id`, `title`, `description`, `link`, `tel`, `tel_toll_free`, `email_address`, `mail_address`, `reply_msg`, `relevant_links`, `fax`
        FROM `contacts`
        WHERE `link` = :link
        LIMIT 1',
        array(':link' => $contact['link'])
      );
      if($results->rowCount()>0){
        foreach($results as $row){
          $contact['id'] = $row->id;
          $contact['title'] = $row->title;
          $contact['description'] = $row->description;
          $contact['link'] = $row->link;
          $contact['tel'] = $row->tel;
          $contact['tel_toll_free'] = $row->tel_toll_free;
          $contact['email_address'] = $row->email_address;
          $contact['mail_address'] = $row->mail_address;
          $contact['reply_msg'] = $row->reply_msg;
          $contact['relevant_links'] = $row->relevant_links;
          $contact['fax'] = $row->fax;
        }
      }
    }
  }

  // generate select boxes for previous items
  if(isset($contact['id'])){
    // look up parents of selected contact
    $results = db_query('SELECT `T2`.`id`, `T2`.`parent_id`, `T2`.`link`, `T2`.`title`
      FROM (SELECT @r AS _id, (SELECT @r := `parent_id` FROM `contacts` WHERE `id` = _id) AS `parent_id`, @l := @l +1 AS `lvl` FROM (SELECT @r := :id, @l :=0) vars, `contacts` m WHERE @r <>0) `T1`
      JOIN `contacts` `T2` ON T1._id = `T2`.`id`
      ORDER BY `T1`.`lvl` DESC
      LIMIT 10;',
      array(':id' => $contact['id'])
    );
    // generate list of parent child elements
    if($results->rowCount()>0){
      $crumb_lvl = 0;
      foreach($results as $row){
        if($row->parent_id==NULL){
          $results2 = db_select('contacts')
            ->fields('contacts',array('title','link'))
            ->isNull('parent_id')
            ->orderBy('title', 'ASC')
            ->execute();
        } else {
          $results2 = db_select('contacts')
            ->fields('contacts', array('title','link'))
            ->condition('parent_id',$row->parent_id,'=')
            ->orderBy('title', 'ASC')
            ->execute();
        }
        $array = array();
        foreach($results2 as $row2){
          $array[] = array('title' => str_repeat('-',$crumb_lvl).' '.$row2->title, 'link' => $row2->link);
        }
        $select_form_code = contact_us_selector($array,$row->link,$row->parent_id);
        if(isset($select_form_code)){
          $form += $select_form_code;
        }
        $crumb_lvl++;
      }
    }
  }
  // generate current selection
  if($contact['id'] == NULL){
    $results = db_select('contacts')
      ->fields('contacts', array('title','link'))
      ->isNull('parent_id')
      ->orderBy('title', 'ASC')
      ->execute();
  } else {
    $results = db_select('contacts')
      ->fields('contacts', array('title','link'))
      ->condition('parent_id',$contact['id'],'=')
      ->orderBy('title', 'ASC')
      ->execute();
  }
  if($results->rowCount()>0){
    $array = array();
    foreach($results as $row){
      $array[] = array('title' => $row->title, 'link' => $row->link);
    }
    $form += contact_us_selector($array,null,$contact['id']);
  }

  return $form;
}