카테고리 보관물: drupal

drupal

사용자 정의 drupal 양식에 분류 용어 참조 필드를 추가하는 방법 MENU_CALLBACK,

메뉴 항목은 drupal_get_form콜백 함수로 정의 되고 콜백 함수에서 양식을 반환합니다. taxonomy_term_reference이 양식 에 필드를 추가 하려면 어떻게 해야합니까?

$items['files/add'] = array(
      'title' => 'Add file',
      'description' => 'Allows users to add files',
      'type' => MENU_CALLBACK,
      'page callback' => 'drupal_get_form',
      'page arguments' => array('mymodule_add_file'),
      'access callback' => TRUE,
    );
function mymodule_add_file($form, &$form_state) {
    drupal_set_title("Add file");
    $form['mymodule_form'] = array(
      '#type' => 'fieldset',
      '#tree' => TRUE,
      '#collapsable' => FALSE,
      '#title' => 'Adding file to locker room',
    );

    $form['mymodule_form']['file'] = array(
      '#type' => 'managed_file',
      '#title' => 'Upload file',
    );

    $form['mymodule_form']['tag'] = array(
      '#type' => 'taxonomy_term_reference',
      '#title' => 'Tags',
    );

    return $form;
}

에 taxonomy_term_reference 필드를 추가하는 방법을 잘 모르겠습니다 $form['mymodule_form']['tag']. 이 필드는 어휘에서 자동 완성되고 입력 된 용어를 찾을 수 없을 때 추가되는 새 용어가있는 텍스트 필드가되기를 원합니다.



답변

Drupal 7의 경우 코드는 다음과 같습니다 field_tags. 위젯 유형이 자동 완성 인 노드의 분류 필드가 있습니다.

<?php
   $node=node_load($nid);
    $tags = array();
    foreach ($node->field_tags['und'] as $item) {
      $tags[$item['tid']] = isset($item['taxonomy_term']) ?  $item['taxonomy_term'] : taxonomy_term_load($item['tid']);
    }
    $form['tags'] = array(
      '#type' => 'textfield',
      '#default_value' => taxonomy_implode_tags($tags),
      '#title' => 'Add Tags',
      '#autocomplete_path' => 'taxonomy/autocomplete/field_tags',
      '#maxlength' => 1024,
      '#element_validate' => array('taxonomy_autocomplete_validate')
    );
?>


답변

어휘 ID를 포함해야합니다. 어휘 ID를 하드 코딩 할 수 있어야합니다.

$form['mymodule_form']['tag'][$vocabulary->vid] = array(
  '#type' => 'textfield',
  '#default_value' => $typed_string,
  '#maxlength' => 100,
  '#autocomplete_path' => 'taxonomy/autocomplete/'. $vocabulary->vid,
  '#required' => $vocabulary->required,
  '#title' => $vocabulary->name,
  '#description' => t('Some description ...").')

);

또는 vocab id 5의 경우

$form['mymodule_form']['tag']['5'] = array(
  '#type' => 'textfield',
  '#default_value' => $typed_string,
  '#maxlength' => 100,
  '#autocomplete_path' => 'taxonomy/autocomplete/5',
  '#required' => $vocabulary->required,
  '#title' => $vocabulary->name,
  '#description' => t('Some description ...").')
);

테스트되지는 않았지만 작동합니다. 그렇지 않으면 여기에 실마리가 있습니다 : http://drupal.org/node/854216


답변

나는 이것을 사용했고 자동 분류 콜백이 작동했지만 지정된 분류 체계가 아닙니다. 대신 모든 어휘에서 결과를 반환했습니다.

  $element['test'] = array(
      '#type' => 'textfield',
      '#default_value' => isset($items[$delta]['test']) ? $items[$delta]['test'] : NULL,
      '#maxlength' => 100,
      '#autocomplete_path' => 'taxonomy/autocomplete/37',
   );

왜 그것이 정직해야하는지 잘 모르겠습니다.


답변

@tecjam Drupal 7의 경우 거의 다 왔습니다. vocab id 대신 필드 이름 만 사용하면됩니다.

이처럼 :

 $element['test'] = array(
 '#type' => 'textfield',
  '#default_value' => isset($items[$delta]['test']) ? $items[$delta]['test'] : NULL,
  '#maxlength' => 100,
  '#autocomplete_path' => 'taxonomy/autocomplete/field_name',
);

field_name을 필드 이름으로 바꾸십시오.


답변