0

I am developing a module and I would like to know how to add a field to a taxonomy term?

My section is named special and I want to add terms in this section

I have this code:

function add($form,&$form_state) {
    $form['name'] = array(
        '#type' => 'textfield',
        '#title' => t('name'),
        '#required' => TRUE,
    );

    $form['submit_button'] = array(
        '#type' => 'submit',
        '#value' => t('send'),
    );

    $form['#submit'][] = 'special_add_submit';

    return $form;
}

function special_add_submit ($form, &$form_state) {
    $instance = array(
        'field_name' => 'nombre',
        'entity_type' => 'taxonomy_term',
        // This is the vocabulary
        'bundle' => 'special',
        'label' => t('Relates to option'),
        'description' => t('This is the description'),
        'required' => true,
        'widget' => array(
        'type' => 'text_textfield',
        'settings'=> array(
            'max_width'=>80)
        ),
        );
    field_create_instance($instance);
}

How to do that?

enter image description here

i create a field named name when i save that field i want to add the value to taxonomy term named special

3
  • What is the specific issue you are running into? Usually you create the field instance when enabling a module. Yours should be created when the user submits a form, or should it only save the value? Please update the question, explain what you want to achieve and what does and does not work. Dec 26, 2016 at 22:44
  • @skycomputer2 If I understood correctly you need to add some fields to a taxonomy field? If so, consider to follow the default way as it is provided by Drupal and don't try to implement it from scratch
    – mchar
    Dec 27, 2016 at 9:43
  • This question makes no sense. You want to create a term programmatically - not add a field to a term. Or is it vice versus? It's not clear.
    – Kevin
    Dec 28, 2016 at 20:19

1 Answer 1

0

here is how i can do it

  $newterm = new stdClass();
  $newterm->name = 'fashion'; // The name of the new term
  $newterm->vid = 2; // The ID of the parent vocabulary
  taxonomy_term_save($newterm); 

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.