1

I have been trying to created a content type programmatically in Drupal 7. I notice that I should use the hook_node_info to generate the content type with the title and to add the body I should use the node_add_body_field. I have been follow the examples modules

So this is what I got as my code ...
non_recruiter.module

<?php

/**
 * Implements hook_node_info().
 */
function non_recruiter_node_info() {
  // We define the node type as an associative array.
  return array(
      'job_template_2' => array(
          'name' => t('Job Template 2'),
          'base' => 'job_template_2',
          'description' => t('A content type for posting your jobs.'),
          'title_label' => t('Job Template 2'),
          'locked' => TRUE,
      ),
  );
}

/**
 * Implements hook_node_type_insert().
 */
function non_recruiter_node_type_insert($content_type) {
  if ($content_type->type == 'job_template_2') {
      $body_instance = node_add_body_field($content_type, t('Job Description'));
        $body_instance['display']['non_recruiter_list'] = array(
        'label' => 'hidden',
        '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 (_non_recruiter_installed_fields() as $field) {
      field_create_field($field);
    }

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

/**
 * Implements hook_form().
 */
function non_recruiter_form($node, $form_state) {
  return node_content_form($node, $form_state);
}


function _non_recruiter_installed_fields() {
  return array(
      'non_recruiter_quantity' => array(
          'field_name'  => 'node_example_quantity',
          'cardinality' => 1,
          'type'        => 'text',
      ),
  );
}


function _non_recruiter_installed_instances() {
  return array(
      'non_recruiter_quantity' => array(
          'field_name'  => 'non_recruiter_quantity',
          'label' => t('Quantity required'),
          'type'        => 'text',
          'widget'      => array(
              'type'    => 'text_textfield',
          ),
          'display' => array(
              'non_recruiter_list' => array(
                  'label' => 'hidden',
                  'type' => 'hidden',
              ),
          ),
      ),
  );
}

So I took what they did and modify the code base on what I want.

My Goal is to have a content type that have the following fields...

  • Job Title (title)
  • Job Description (body)
  • Quantity (text)

But I'm doing something wrong because the fields are empty. If I try to add new content using this content type this is what I got ... Empty Content type

An empty content type, without any fields.

Here is some extra information of how my Manage Fields & Display looks like..

Manage Fieldsenter image description here

Manage Display

enter image description here

So, can anyone tell me why my fields are not showing up in my content type ?

  • 1
    Any chance you've installed the module, then added hook_node_type_insert() afterwards? If so it won't be run. Might be worth uninstalling and re-installing the module to check – Clive Oct 22 '14 at 17:19
  • I have uninstalling and re-installing the module few times already. And just in case I did it again, but this did not fix my problem. I still have the same problem. – itsdarrylnorris Oct 22 '14 at 17:24
  • Core modules (like Blog) seem to do it in hook_install() rather than the insert hook - you could try using the same method as api.drupal.org/api/drupal/modules%21blog%21blog.install/…. It may be a case of adding node_types_rebuild(); to the top of the insert hook instead. Though that doesn't seem right thinking about it – Clive Oct 22 '14 at 17:28
0

enter image description here

/**
 * Implements hook_node_info().
 */
function non_recruiter_node_info() {
  // We define the node type as an associative array.
  return array(
      'job_template_2' => array(
          'name' => t('Job Template 2'),
          'base' => 'job_template_2',
          'description' => t('A content type for posting your jobs.'),
          'title_label' => t('Job Template 2'),
          'locked' => TRUE,
      ),
  );
}

/**
 * Implements hook_node_type_insert().
 */
function non_recruiter_node_type_insert($content_type) {
  if ($content_type->type == 'job_template_2') {
      $body_instance = node_add_body_field($content_type, t('Job Description'));
        $body_instance['display']['non_recruiter_list'] = array(
        'label' => 'hidden',
        '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 (_non_recruiter_installed_fields() as $field) {
  field_create_field($field);
}

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

/**
 * Implements hook_form().
 */
function job_template_2_form($node, $form_state) {
  return node_content_form($node, $form_state);
}


function _non_recruiter_installed_fields() {
  return array(
      'non_recruiter_quantity' => array(
          'field_name'  => 'node_example_quantity',
          'cardinality' => 1,
          'type'        => 'text',
      ),
  );
}


function _non_recruiter_installed_instances() {
  return array(
      'non_recruiter_quantity' => array(
          'field_name'  => 'node_example_quantity',
          'label' => t('Quantity required'),
          'type'        => 'text',
          'widget'      => array(
              'type'    => 'text_textfield',
          ),
          'display' => array(
              'non_recruiter_list' => array(
                  'label' => 'hidden',
                  'type' => 'hidden',
              ),
          ),
      ),
  );
}
  • There is unmatched value for key 'field_name' => 'non_recruiter_quantity' in the function _non_recruiter_installed_fields() and _non_recruiter_installed_instances() do it similar as you wanna also replace bundle name $instance['bundle'] = 'non_recruiter'; to $instance['bundle'] = 'job_template_2'; improvement:if you want to see your node type in UI under(your/domain/admin/structure/types) replace non_recruiter_form to job_template_2_form (as you are using 'base' => 'job_template_2') I have tested. problem resolved. – Manoj Paul Dec 16 '17 at 14:37

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.