1

I'm developing a module that create a several new content types:

function create_new_content_type($type, $name){
    $t = get_t();

    $new_node = array(
        'type' => $type,
        'name' => $name,
        'base' => 'node_content',
        'custom' => TRUE,
        //'description' => 'This is an example node type with a few fields.',
    );
    $content_type = node_type_set_defaults($new_node);
    node_add_body_field($content_type);

    // Check if we create content type or update.
    $status = node_type_save($content_type);

    // Replacement rule for the messages.
    $t_args = array('%name' => $content_type->name);

    if ($status == SAVED_NEW) { // create case
        watchdog('node', 'Added content type %name.', $t_args, WATCHDOG_NOTICE, l($t('view'), 'admin/structure/types'));
        return true;
    }

    return false;
}

after that creates a new fields for that type (for example):

function createTextField($name, $label, $bundle, $required=FALSE){
    // Create the field base.
    $field = array(
        'field_name' => $name,
        'type' => 'text',
    );
    field_create_field($field);

    // Create the field instance on the bundle.
    $instance = array(
        'field_name' => $name,
        'entity_type' => 'node',
        'label' => $label,
        'bundle' => $bundle,
        // If you don't set the "required" property then the field wont be required by default.
        'required' => $required,
        'widget' => array(
            'type' => 'textfield',
        ),
    );
    field_create_instance($instance);

}

Lets assume I have already existed field with name field_my_custom how can I attach it to my new created content type? (if I understand right in the method above I only create new fields).

3 Answers 3

1

You would need to call again the field_create_instance() function passing a new array.

 $instance = array(
    'field_name' => 'field_my_custom', // ** This already exists, or check with field_info_field()
    'entity_type' => 'node',
    'label' => 'a label', // change this of course
    'bundle' => $new_created_content_type, // this too 
    'widget' => array(), // if you're not sure about your widget settings, take a glimpse at an existing field instance using debug(field_info_instance(...));
);
field_create_instance($instance);

More links to read and better understand the above functions:

https://api.drupal.org/api/drupal/modules%21field%21field.info.inc/function/field_info_field/7 https://api.drupal.org/api/drupal/modules%21field%21field.info.inc/function/field_info_instance/7 https://api.drupal.org/api/drupal/modules%21field%21field.crud.inc/function/field_create_instance/7

Also, take a look at the Examples for Developers module, node example and field example.

1

No creating a new instance means you are attaching that field to a content type. Here $bundle is the machine name of the content type

0
<?php

function new_menu(){
    $items = array();

 $items['field_create'] = array(
        'page callback' => 'field_create',
        'type' => MENU_CALLBACK,
        'access callback' => TRUE,
      );

return $items;

}



function field_create() {



/*print_r(field_info_field($myField_name));
exit;*/

$contenttypes = node_type_get_types();


foreach($contenttypes as $type)
{

     $myField_name = "new_".$type->type."_value9";

    if(!field_info_field($myField_name)) 
    {


    /*print_r($type);
    exit;*/ 



    $field = array(
        'field_name'    => $myField_name,
        'type'          => 'image',
    );
    field_create_field($field);

    $field_instance = array(
        'field_name'    => $myField_name,
        'entity_type'   => 'node',
        'bundle'        => 'Basic Page',
        'label'         => t('Select an image'),
        'description'   => t(''),
        'widget'        => array(
            'type'      => 'image_image',
            'weight'    => 10,
        ),
        'formatter'     => array(
            'label'     => t('label'),
            'format'    => 'image'
        ),
        'settings'      => array(
            'file_directory'        => 'gallery', 
            'max_filesize'          => '4M',
            'preview_image_style'   => 'thumbnail',
            'title_field'           => TRUE,
            'alt_field'             => FALSE,
        )
    );
    field_create_instance($field_instance);

}
 }
 drupal_set_message("Field created successfully!");

}
1
  • please add some description to your answer. Commented Sep 15, 2014 at 5:07

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.