0

I want to create a new content type " News " with a custom field Author which refers to the author-of-the-content(taxonomy reference or user reference).

This is my .module file:

<?php
/**
 * Implements hook_node_info()
 * Drupal calls this hook when it’s discovering which node types are available. You’ll
 * provide some metadata about your custom node.
 */
function news_node_info() {
    return array(
        'news' => array(
            'name' => t('News'),
            'base' => 'news',
            'description' => t('You can add  News here'),
            'has_title' => TRUE,
            'title_label' => t('News title')
         )
    );
}

/**
 * Implement hook_form()
 */
function news_form($node, $form_state) {
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('news Title'),
    '#default_value' => !empty($node->title) ? $node->title : '',
    '#required' => TRUE,
    '#weight' => -5
  );
  return $form;
  }

This is my .install file:

<?php
/**
 * Implements hook_install().
 */
function news_install() {
    node_types_rebuild();
    $types = node_type_get_types();|
      node_add_body_field($types['news']);
    add_custom_fields();
}

function _news_installed_fields() {
  $t = get_t();
  return array(
    'news_title' => array(
      'field_name' => 'news_title',
      'label' => $t('Title'),
      'type' => 'text',
      ),
    'news_image' => array(
      'field_name' => 'news_image',
      'label' => $t('Image'),
      'type' => 'image',
    ),
    'news_tags' => array(
      'field_name' => 'news_tags',
      'label' => $t('Add Tags'),
      'type' => 'taxonomy_term_reference',
      'settings' => array(
        'allowed_values' => array(
          array(
            'vocabulary' => 'news_tags_vocab',
            'parent' => 0,
            ),
          ),
        ),
      ),
    ),
  );
}

function _news_installed_instances() {
  $t = get_t();
  return array(
    'news_title' => array(
      'field_name' => 'news_title',
      'type' => 'text',
      'label' => $t('The heading of the news topic'),
      'widget' => array(
        'type' => 'text_textfield',
      ),
      'display' => array(
        'news_display_option' => array(
          'label' => $t('The heading of the news topic (display option)'),
          'type' => 'text',
          ),
        ),
      ),
      'news_image' => array(
        'field_name' => 'news_image',
        'type' => 'image',
        'label' => $t('Add Image'),
      ),
      'news_tags' => array(
        'field_name' => 'news_tags',
        'type' => 'taxonomy_term_reference',
        'label' => $t('Add Tags'),
        'widget' => array(
          'type' => 'taxonomy_autocomplete'
        ),
        'display' => array(
          'default' => array('type' => 'hidden'),
          'teaser' => array('type' => 'hidden')
        ),
      ),
    )
  );
}
function add_custom_fields() {
    foreach (_news_installed_fields() as $field) {
        field_create_field($field);
    }
    foreach (_news_installed_instances() as $fieldinstance) {
        $fieldinstance['entity_type'] = 'node';
        $fieldinstance['bundle'] = 'news';
        print_r($fieldinstance);
        field_create_instance($fieldinstance);
    }
}

/**
 * Implements hook_uninstall().
 */
function news_uninstall() {
  // Delete the taxonomy with its terms
  $vocab = taxonomy_vocabulary_machine_name_load('news_tags_vocab');
  taxonomy_vocabulary_delete($vocab->vid);

  // Gather all the example content that might have been created while this
  // module was enabled.
  $sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
  $result = db_query($sql, array(':type' => 'news'));
  $nids = array();
  foreach ($result as $row) {
    $nids[] = $row->nid;
  }

  // Delete all the nodes at once
  node_delete_multiple($nids);
  // Loop over each of the fields defined by this module and delete
  // all instances of the field, their data, and the field itself.
  foreach (array_keys(_news_installed_fields()) as $field) {
    field_delete_field($field);
  }
  // Loop over any remaining field instances attached to the news
  // content type (such as the body field) and delete them individually.
  $instances = field_info_instances('node', 'news');
  foreach ($instances as $instance_name => $instance) {
    field_delete_instance($instance);
  }
  // Delete our content type
  node_type_delete('news');
  // Purge all field infromation
  field_purge_batch(1000);
}
2
  • Request you to explain some more. Is above code working fine or is there any error. if any errors let us know so that we can help you out.
    – Ajay Reddy
    Commented Aug 8, 2016 at 13:03
  • It's working, I want to add another custom field like the image field and the tags field,a custom field instance that contains the author of the content. Commented Aug 8, 2016 at 13:18

1 Answer 1

0

The following code should get you started. Got this here. I suggest you check there first in the future.

$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!");
}

You can execute this code countless ways. I am not privy to the requirements of your project so its hard for me to make a recommendation. You could hook this into the update/install functions, or you could build it into a page hook in a module, or you could just bootstrap any new php file in the root directory with this:

require_once DRUPAL_ROOT . '/includes/bootstrap.inc'; drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

And you can also refer this.

2
  • in this part : $field = array( 'field_name' => $myField_name, 'type' => 'image', ) i want to know if there is a type called author that i can use instead of 'type' => 'image'. Commented Aug 8, 2016 at 13:39
  • I don't think so there is a field type called author. Refer this
    – Ajay Reddy
    Commented Aug 9, 2016 at 3:37

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.