2

I am creating a new content type and I want to add a Tags field. I created a vocabulary and a taxonomy_term_reference, field but this field is giving this notice every time I try to add content and click save:

Notice: Undefined index: tid in taxonomy_field_is_empty() (line 1562 of /var/www/html/drupal/modules/taxonomy/taxonomy.module).

This is the code I am using.

news.install

function news_install() {
  node_types_rebuild();
  $types = node_type_get_types();
  // Add the body field to the node type
  node_add_body_field($types['news']);

  // Load the instance definition for our content type's body
  $body_instance = field_info_instance('node', 'body', 'news');
  // Configure the body field
  $body_instance['type'] = 'text_summary_or_trimmed';
  // Save our changes to the body field instance.
  field_update_instance($body_instance);
  // Create all the fields we are adding to our content type.
  foreach (_news_installed_fields() as $field) {
    field_create_field($field);
  }


  // Create all the instances for our fields.
  foreach (_news_installed_instances() as $instance) {
    $instance['entity_type'] = 'node';
    $instance['bundle'] = 'news';
    $instance['required'] = True;
    field_create_instance($instance);
  }

  // Create vocabulary for tags
  $news_tags_vocab = (object) array(
    'name' => 'Tags vocabulary',
    'description' => 'This vocabulary is used for the tags field',
    'machine_name' => 'news_tags_vocab',
    );

  taxonomy_vocabulary_save($news_tags_vocab);
}

function _news_installed_fields() {
  $t = get_t();
  return array(
    'news_title' => array(
      'field_name' => 'news_title',
      'label' => $t('Title'),
      'type' => 'text',
      ),
    'news_image' => array(
      'field_name' => 'news_image',
      'label' => $t('Image'),
      'type' => 'image',
    ),
    'news_tags' => array(
      'field_name' => 'news_tags',
      'label' => $t('Add Tags'),
      'type' => 'taxonomy_term_reference',
      'settings' => array(
        'allowed_values' => array(
          array(
            'vocabulary' => 'news_tags_vocab',
            'parent' => 0,
            ),
          ),
        ),
      ),
    ),
  );
}

/**
 * Return a structured array defining the field instances associated with this content type.
 */
function _news_installed_instances() {
  $t = get_t();
  return array(
    'news_title' => array(
      'field_name' => 'news_title',
      'type' => 'text',
      'label' => $t('The heading of the news topic'),
      'widget' => array(
        'type' => 'text_textfield',
      ),
      'display' => array(
        'news_display_option' => array(
          'label' => $t('The heading of the news topic (display option)'),
          'type' => 'text',
          ),
        ),
      ),
      'news_image' => array(
        'field_name' => 'news_image',
        'type' => 'image',
        'label' => $t('Add Image'),
      ),
      'news_tags' => array(
        'field_name' => 'news_tags',
        'type' => 'taxonomy_term_reference',
        'label' => $t('Add Tags'),
        'widget' => array(
          'type' => 'taxonomy_autocomplete'
        ),
        'display' => array(
          'default' => array('type' => 'hidden'),
          'teaser' => array('type' => 'hidden')
        ),
      ),
    )
  );
}

/**
 * Implements hook_uninstall().
 */
function news_uninstall() {
  // Delete the taxonomy with its terms
  $vocab = taxonomy_vocabulary_machine_name_load('news_tags_vocab');
  taxonomy_vocabulary_delete($vocab->vid);

  // Gather all the example content that might have been created while this
  // module was enabled.
  $sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
  $result = db_query($sql, array(':type' => 'news'));
  $nids = array();
  foreach ($result as $row) {
    $nids[] = $row->nid;
  }

  // Delete all the nodes at once
  node_delete_multiple($nids);
  // Loop over each of the fields defined by this module and delete
  // all instances of the field, their data, and the field itself.
  foreach (array_keys(_news_installed_fields()) as $field) {
    field_delete_field($field);
  }
  // Loop over any remaining field instances attached to the news
  // content type (such as the body field) and delete them individually.
  $instances = field_info_instances('node', 'news');
  foreach ($instances as $instance_name => $instance) {
    field_delete_instance($instance);
  }
  // Delete our content type
  node_type_delete('news');
  // Purge all field infromation
  field_purge_batch(1000);
}

What's wrong in my code, and how can I fix it?

1

The solution for this problem is to implement Hook_form() So, the following code must be added into news.module file.

function news_form($node, $form_state){
  return node_content_form($node, $form_state);
}

Tricky but easy :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.