2

I'm new to Drupal and I'm looking for a way to add a new field to an already installed content-type in Drupal 7. Please note that some content is already present in the database. Also, I need to do this programmatically and not via a GUI.

I was thinking to use hook_update function to add the new field. This is my install file:

function jobpost_install() {
    $t = get_t();

    $node_job = array(
        'type' => 'job',
        'name' => $t('Job'),
        'base' => 'node_content',
        'description' => $t('Your job post'),
        'title_label' => $t('Job Title'),
        'custom' => TRUE
    );

    $content_type = node_type_set_defaults($node_job);
    node_add_body_field($content_type, $t('synopsis'));
    node_type_save($content_type);
    $body_instance = field_info_instance('node', 'body', 'job');
    $body_instance['display']['job_node_list'] = array(
        'label' => 'hidden',
        'type' => 'text_summary_or_trimed'
    );
    field_update_instance($body_instance);

    foreach(jobpost_node_shared_installed_fields() as $field) {
        field_create_field($field);
    }

    foreach(jobpost_node_shared_installed_instances() as $instance) {
        $instance['entity_type'] = 'node';
        $instance['bundle'] = $node_job['type'];
        field_create_instance($instance);
    }
}

function jobpost_node_shared_installed_fields() {
    $t = get_t();
    return array(
        'job_subtitle' => array(
            'field_name' => 'job_subtitle',
            'cardinality' => 1,
            'type' => 'text',
            'settings' => array(
                'max_length' => 1024
            )
        ),
        'job_company' => array(
            'field_name' => 'job_company',
            'cardinality' => 1,
            'type' => 'text',
            'settings' => array(
                'max_length' => 1024
                    )
        )
    );
}

function jobpost_node_shared_installed_instances() {
    $t = get_t();
    return array(
        'job_subtitle' => array(
            'field_name' => 'job_subtitle',
            'label' => $t('Subtitle'),
            'type' => 'text',
            'widget' => array(
                'type' => 'text_textfield',
                'weight' => 1.0,
            ),
            'display' => array(
                'default' => array(
                    'label' => 'hidden',
                    'type' => 'hidden',
                ),
                'job_node_list' => array(
                    'label' => 'hidden',
                    'type' => 'hidden',
                ),
            ),
        ),
        'job_company' => array(
            'field_name' => 'job_company',
            'label' => $t('Subtitle'),
            'type' => 'text',
            'widget' => array(
                'type' => 'text_textfield',
                'weight' => 1.0,
            ),
            'display' => array(
                'default' => array(
                    'label' => 'hidden',
                    'type' => 'hidden',
                ),
                'job_node_list' => array(
                    'label' => 'hidden',
                    'type' => 'hidden',
                ),
            ),
        ),

    );
}

function jobpost_update_7100() {
    foreach(jobpost_node_shared_installed_fields() as $field) {
        $field_name = field_info_field($field['field_name']);
        if(!$field_name) {
            field_create_field($field);
            watchdog('jobpost', 'Field %field_name has been successfully added', array('%field_name' => $field['field_name']));
        }
        else {
            watchdog('jobpost', 'Field %field_name already exists', array('%field_name' => $field['field_name']));
        }
    }

    foreach(jobpost_node_shared_installed_instances() as $instance) {
        $instance_name = field_info_field($instance['field_name']);
        if(!$instance_name) {
            $instance['entity_type'] = 'node';
            $instance['bundle'] = $node_job['type'];
            field_create_instance($instance);
            watchdog('jobpost', 'Instance %instance_name has been successfully added', array('%instance_name' => $field['instance_name']));
        }
        else {
            watchdog('jobpost', 'Instance %instance_name has been successfully added', array('%instance_name' => $field['instance_name']));
        }
    }
}

I installed the module with the only job_subtitle field then I tried to add the second field Job_company calling the update function. The two tables field_data_job_company and field_revision_job_company were created but I cannot see the new field in the list of fields to share in the administration panel.

enter image description here

Do you have an idea or basic example of how to deal with this?

Thanks.

  • Sorry for the question, I realized that it was a cache problem. Reading the drupal book I find this: "If you ever modify code in the menu hook, you’ll need to clear the menu cache. You can do this by truncating the cache_menu table or by clicking the “Rebuild menus” link that the Drupal development module (devel.module) provides or by using the “Clear cached data” button by visiting the Configuration page and clicking the Performance link." – Katie Oct 28 '13 at 9:28

Your Answer

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

Browse other questions tagged or ask your own question.