在drupal7中动态的为某个内容类型添加字段

 $myField_name = "my_new_field_name";
    if(!field_info_field($myField_name)) // check if the field already exists.
    {
        $field = array(
            'field_name'    => $myField_name,
            'type'          => 'image',
        );
        field_create_field($field);

        $field_instance = array(
            'field_name'    => $myField_name,
            'entity_type'   => 'node',
            'bundle'        => 'CONTENT_TYPE_NAME',
            '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'        => 'photos', // save inside "public://photos"
                'max_filesize'          => '4M',
                'preview_image_style'   => 'thumbnail',
                'title_field'           => TRUE,
                'alt_field'             => FALSE,
            )
        );
        field_create_instance($field_instance);
        drupal_set_message("Field created successfully!");
    }


原文地址:https://www.cnblogs.com/keanuyaoo/p/3263039.html