1

I am working in drupal 7, creating content types in a custom module with the code below

/**
 * Implements hook_install().
 */

//Create 'award' content type

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

    // Step 1 - Define the custom content type

    $content_type = array(  
        'type'          => 'awards_entry_3',
        'name'          => $t('Award 3'),
        'description'   => $t('Enter a record into the awards 3 page'),
        'title_label'   => $t('Award title'),
        'base'          => 'node_content',
        'custom'        => 1,
    );

    $node_type = node_type_set_defaults($content_type);

    node_type_save($node_type);

    // Step 2 - Create new fields

    $fields = array(    
        // Author’s name field
        'awards_entry_3_author_name' => array(      
            'field_name'    => 'awards_entry_3_author_name',
            'type'          => 'text',
            'cardinality'   => 1,
        ),
        // Description of award field
        'awards_entry_3_description' => array(      
            'field_name'    => 'awards_entry_3_description',
            'type'          => 'text_long',
            'cardinality'   => 1,
        ),
    );

    //Add fields to database
    foreach( $fields as $field ) {
        field_create_field($field);
    }

    // Step 3 - Attach fields to content type

    $instances = array( 
        // Author’s name field
        'awards_entry_3_author_name' => array(      
            'field_name'   => 'awards_entry_3_author_name',
            'label'        => $t('Author Name'),
            'required'     => TRUE,
            'widget'       => array('type' => 'text_textfield'),
        ),
        // Description field
        'awards_entry_3_description' => array(      
            'field_name'   => 'awards_entry_3_description',
            'label'        => $t('Description'),
            'required'     => TRUE,
            'widget'       => array('type' => 'text_textarea'),
        ),
    );

    foreach( $instances as $instance ) { // Loop through our instances
        $instance['entity_type'] = 'node';
        // Attach the instance to our content type
        $instance['bundle'] = 'awards_entry_3'; 
        field_create_instance($instance);
    }

    //Set content types' machine name as variable
    $c_path = $content_type['type'];

    //Variable to be tageted with variable_set
    $path = 'pathauto_node_' . $c_path . '_pattern';

    //Set path of new content type
    variable_set($path, 'award/[node:title]'); 

}

How can I turn 'comments' off and 'revision's on in code?

  • Please fix your code sample along the Drupal coding standards. – leymannx Jun 26 '17 at 15:37
  • Could you be more specific? – Tom Bellew Jun 27 '17 at 8:26
  • Indentation, spacing, documentation. What does & adding the fields with exactly stand for? In what hook are you firing your code? What you provided now is barely readable. – leymannx Jun 27 '17 at 8:33
  • Ok thanks for the tips pal. Hopefully it's more readable now. – Tom Bellew Jun 27 '17 at 9:21

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.