1

I am using a module that defines a content type and view. When I install my module, the view is saved before the content type. So, my view states that fields of that content type are not available.

Is there any way to save content type first and then view?

.install

function _productcustomtype_installed_fields() {
    $t = get_t();
    return array(
        'example_date' => array(
            'field_name' => 'example_date',
            'label' => $t('example Date'),
            'type' => 'date'
        ),
        'example_slot' => array(
            'field_name' => 'example_slot',
            'label' => $t('example Slot '),
            'type' => 'list_text'
        )
    );
}


function _productcustomtype_installed_instances() {
    $t = get_t();
    return array(
        'example_date' => array(
            'field_name' => 'example_date',
            'type' => 'text',
            'label' => $t('example Date'),
            'widget' => array(
                'type' => 'date_popup'
            ),
            'date_format' => 'Y-m-d',
            'display' => array(
                'default' => array(
                    'label' => $t('example Date'),
                    'type' => 'date_popup'
                )
            )
        ),
        'example_slot' => array(
            'field_name' => 'example_slot',
            'type' => 'text',
            'label' => $t('example Slot'),
            'widget' => array(
                'type' => 'list_text'
            ),
            'display' => array(
                'default' => array(
                    'label' => $t('example Slot'),
                    'type' => 'list_text'
                )
            )
        )
    );
}

function add_custom_fields() {
    foreach (_productcustomtype_installed_fields() as $field) {
        field_create_field($field);
    }
    foreach (_productcustomtype_installed_instances() as $fieldinstance) {
        $fieldinstance['entity_type'] = 'node';
        $fieldinstance['bundle'] = 'example_calendar';
        print_r($fieldinstance);
        field_create_instance($fieldinstance);
    }
}

function example_install() {
    node_types_rebuild();
    $types = node_type_get_types();
    add_custom_fields();
}

.module file

/**
 * 
 * @return type
 * Implements hook_node_info
 */
function example_node_info() {
  return array(
    'example_calendar' => array(
      'name' => t('example Calendar'),
      'base' => 'example',
      'description' => t('A example content type'),
      'has_title' => TRUE,
      'locked' => FALSE,
    ),
  );
}

/**
 * 
 * @param type $node
 * @param type $form_state
 * @return type
 * Implements hook_form
 */
function example_form($node, $form_state) {
  $type = node_type_get_type($node);
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($type->title_label),
    '#default_value' => !empty($node->title) ? $node->title : '',
    '#required' => TRUE, '#weight' => -5
  );

  return $form;
}


/**
 * 
 * @return type
 * Implements hook_views_api
 */
function example_views_api() {
  return array(
    'api' => 3.0,
  );
}

/**
 * 
 * @return type
 * Implements hook_views_default_views
 */
function example_views_default_views() {
//node View code goes here
}
  • Plz chk with code... hope that will help you.. – Abin Jul 20 '15 at 6:16
0

Change your install function to

$name = 'your content type Machine name';

// Define the node type.
$jobs = array(
'type' => $name,
'name' => $t('your content type name'),
'base' => 'node_content',
'title_label' => $t(' Title'),
'description' => $t('Description.'),
'custom' => TRUE,
);

// Set other node defaults not declared above.
$content_type = node_type_set_defaults($jobs);

Remove node info hook from .module file and start your function name in .module file with your module name.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.