0

I am building a custom content type programmatically within my module. Everything appears as I thought it would on the "Add Content" page, but whenever I create a new piece of content or edit the created content the data from the field is not being saved. When I go back into the content nothing is there.

What am I missing? Is there a better approach? I wanted to keep the scope of this module inside of the related "Add Content" page because it needs to be able to follow the Drupal Workbench workflow.

On .install:

function header_maker_enable(){
  $contentType = "brand_header";
  $readableType = "Brand Header";
  $type = node_type_load($contentType);

  //Verify that the content type has not already been created once for this module
  if(!$type){
    $type = array(
      'type' => $contentType,
      'name' => t($readableType),
      'base' => 'node_content',
      'description' => t("Test"),
      'custom' => 1,
      'modified' => 1,
      'locked' => 0,
    );

   $type = node_type_set_defaults($type);

   //Creating Custom Test Field
   $field = array(
    'field_name' => 'brand_header_config',
    'type' => 'text',
    'entity_types' => array('node'),
  );
  $field = field_create_field($field);
  $instance = array(
    'field_name' => 'brand_header_config',
    'entity_type' => 'node',
    'bundle' => 'brand_header',
    'label' => t('Test Field'),
    'widget' => array('type' => 'text_textfield'),
  );
  $instance = field_create_instance($instance);

  node_type_save($type);
}

}

Here is my .module file. Something a bit more tricky is going on here with some JS. Basically I am rendering a UI to create some JSON. That JSON is then being added to the input field that was created on install in real time.

function header_maker_form_brand_header_node_form_alter(&$form, $form_state, $form_id){
  if ($form_id == 'brand_header_node_form') {
  $form['header_maker_ui'] = array(
    '#prefix' => '<div class="header-builder">',
    '#suffix' => '</div>', 
  );
  $form['header_maker_render'] = array(
    '#prefix' => '<div class="header-render">',
    '#suffix' => '</div>', 
  );

$order = array(
'header_maker_render',  
'brand_header_node_form',
'brand_header_config'
  );

  foreach ($order as $key => $field) {
// Set/Reset the field's 
// weight to the array key value
// from our order array. 
$form[$field]['#weight'] = $key;
  }
  }
  return $form;
  }

function header_maker_preprocess_page(&$vars, $hook){
  if(isset($vars['page']['content']['system_main']['#form_id'])){
$formid = $vars['page']['content']['system_main']['#form_id'];
if($formid == "brand_header_node_form"){
  drupal_add_css(drupal_get_path('module', 'header_maker'). '/css/brand-header-form.css', array('group' => CSS_THEME, 'every_page' => FALSE));
  drupal_add_js(drupal_get_path('module', 'header_maker') . '/js/header_maker.js');
}
}
}
  • Why are you adding a field through code instead of through the admin interface? That is an important detail to knowing how I can suggest something to you. The admin interface also creates the database table that stores the value for your field, which it looks like your code does not. – DeveloperWeeks Nov 5 '15 at 17:18
  • @DeveloperWeeks A content type and it's relevant fields are being added on module install to make the UX as simple as possible. I wanted to have the users be able to add the module to their site and have everything ready to go for content creation, since most of the people who will be working with it will not be actual developers. After looking in the DB I can confirm that this code does create the table in my DB. – jrounsav Nov 5 '15 at 17:28
  • Features module was made specifically for the feat you are attempting. – DeveloperWeeks Nov 5 '15 at 17:31
  • 1
    If the issue is not in the construction of the field instance, then you can try adding a custom validator or submit handler to your form and see if the field retains its value that far. Add a watchdog line in the node_save section of core to see what is being passed, etc. – DeveloperWeeks Nov 5 '15 at 17:33
  • I actually ended up dpm'ing the values through node_save. Thanks! – jrounsav Nov 5 '15 at 17:36
1

I figured out what my issue was. In order to force users to use the UI I set the input field as being disabled from my JS file.

 document.getElementById('edit-brand-header-config-und-0-value').disabled = true; 

Apparently setting it to disabled prevents it from being stored even though I could add a value to it programmatically through JS.

Removing "disabled = true"

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.