After creating a text field, you can define the default value for the text format. The selected value is ignored on save.

CommentFileSizeAuthor
#23 savednot.png10.85 KBclude
#23 savedright.png11.72 KBclude
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

droplet’s picture

"The selected value is ignored on save" ??

how to reproduce this

sharpbites’s picture

Here are the steps to reproduce it:
Create a field of type "Text" for a content type.
In the Edit tab, in Text processing select "Filtered text (user selects text format)".
Save settings.
Edit the field.
In the Edit tab, in "Default Value" change text format to "Full html" (default text format is "Filtered html").
Save settings.
Edit again and you'll see the text format is again "Filtered html", instead of the expected "Full html".

droplet’s picture

Title: Default Text format cannot be changed » Default Text format cannot be saved when Default value is empty

bug confirmed.

yched’s picture

Title: Default Text format cannot be saved when Default value is empty » Specify a default format for text fields

A 'formatted text" value is considered empty when the text itself is empty (the format selector *never* returns an empty format, so we need to rely on the text to detect empty values).
That's the logic in text_field_is_empty().

This rule applies for regular input in node edit forms, as well as for the 'default value' input on the field config form.

I'm tempted to mark 'by design'. A default 'formatted text' value cannot be just a default input format. I do see the use case, though, and agree that the current UI might be misleading.

sharpbites’s picture

Thanks for your reply.
I used this feature in D6 to set the default input format for the field in combination with better_formats to limit the ability for the editor to change the input format. I guess this is not possible anymore?

When you say 'A default 'formatted text' value cannot be just a default input format.' is there any technical reason or could that behavior somehow be changed in a custom module?

yched’s picture

@sharpbites: I guess it could be changed in a contrib module, but probably not through the 'default field value' UI.

francismacomber’s picture

You can create a simple module using hook_form_alter() to set the default text format per field in the following way:

function yourmodule_form_alter(&$form, &$form_state, $form_id) {  
  if (isset($form['#node']) && $form_id == $form['#node']->type .'_node_form') {
    $form['body']['und']['0']['#format'] = "filtered_html";
    $form['field_custom_php_code']['und']['0']['#format'] = "php_code";
  }
}

This checks that the form is a node edit form and sets all "body" fields to use "filtered_html" and all "field_custom_php_code" fields to use the "php_code" text format by default.

Sol Roth’s picture

Is creating a custom module still the best approach for this? There is a d7 version of better formats, but it does not do what the d6 version did as in specify default formats per field type. I need this feature in a d7 build and I'm going to use a simple custom module, but I'm wondering if there has been any work or contributed modules that address this issue?

yched’s picture

Version: 7.0 » 8.x-dev
Category: bug » feature

"Specify a default format for text fields" would be a D8 feature request, not a D7 bug, BTW.
Also, this topic is more complex than just pushing a given default value for the format selector in forms, since there not all roles are able to use all input formats.
This has typically been handled in dedicated contrib projects in D6 (like http://drupal.org/project/better_formats)

Starbuck’s picture

I came to this post hoping to find a way in D7 to set the default text format to Full HTML for users authorized and preferring that method. As the site admin, I want all Basic Page and Book nodes, probably others, to default to Full HTML. It's become a real pain to change from default Filtered HTML to Full HTML for every single new node.

My understanding at this point in the discussion is that this feature may be a D8 enhancement request rather than something we can do with the core right now. Is that accurate? Is the module proposed by @francismacomber the only way to do this now?

Thanks.

mightyiam’s picture

Title: Specify a default format for text fields » Specify a default format for text fields (not long text)

I'd like this also.

seandunaway’s picture

Ran into the same issue, +1 and subscribe.

naplesweb’s picture

A fairly functional crutch for this is to enter *any content* into the field default content box - even something not too noteworthy like a period (a space does NOT work) and change the default text format to your preferred value of Full HTML. Having something in the default content box allows the default value of the format to be saved. I tried to then empty the field to save it again but alas, it reverts to it's default of "filtered HTML" again.

So for now, my content all starts with a "." until I populate the field - removing the "." is still easier than remembering to change the type to Full HTML on each article and eliminates risk of blowing away any tables or images or other edits I've added upon saving the page in case I forget to modify each page to "Full HTML"

jkaine’s picture

naplesweb,

I took your idea of the default value, and took it a step further and got it to work. I was lost until I read your post.

It's a slightly ugly hack, but it does the trick.

Step 1: Do as you did and add a "." to the default value (via manage fields for the body field). Make sure to set the text format to Full HTML (or other variant). Doing this will add a period to your body form field, but will also set the text format to the one you used when setting the default value.

[so far, same as you did]

Step 2: Write a short custom module that does a hook_form_FORMID_alter to reset the default value of the body field to empty. Here's the code I used:

function adminformsoverride_form_story_feature_node_form_alter(&$form, &$form_state) {
	if (substr($form['body']['und'][0]['#default_value'], 0, 8)  == '<p>.</p>') {
		$form['body']['und'][0]['#default_value'] = '';
	}
	
};

NOTE: I used the substr in order to bypass some weird carriage return values at the end of the default content, and the if is in place so that the replace only occurs for default content and not for content already entered.

You'll need to adjust the function name, and double check the form array, etc.

I hope this helps.

JK

PS: It would be great to have the ability to specify formats for text fields on a per-content type and per-role basis, but it's a bit beyond my skill set right now to contribute more than the occasional hack.

dpouliot’s picture

Version: 8.x-dev » 7.4

I'd love to see this feature too. When I saw the text format field on the manage fields screen, I expected this to be a configurable setting. “Surely they would not have put an editable field on an edit screen and yet not allowed it to be editable”, but alas they did.

I don't want to sound ungrateful or disrespectful. I am new to Drupal and I think it is absolutely incredible product. The fact is open source blows me away.

droplet’s picture

Version: 7.4 » 8.x-dev
jastraat’s picture

If you set the weight of the Full HTML format to lower than the filtered format, it will be the default everywhere for users who have permission to use it.

emagray’s picture

#17 = Perfect solution!!!!

jkaine’s picture

emagray and jastraat,

That's a great solution if you want all of your text fields to use the same format. If you need some text fields to use one format, and other fields to use a different format, the weight solution doesn't quite work.

Ideally, there would be a way to set a priority list of field formats on a per-field+content type basis. If you set them in a priority order, that would allow some failover if a role didn't have permission for a specific content type. I can achieve something along these lines via a custom hack module( see #14), but I'm not yet skilled enough to build a full module to do this via the admin interface.

Best,
JK

jastraat’s picture

This wouldn't provide a UI, but if there's a particular field on which you want to set a default format, you just need:

function MODULENAME_form_CONTENTTYPE_node_form_alter(&$form, &$form_state) {
  $form['field_example']['und'][0]['#format'] = "filtered_html";

  return $form;
}
jkaine’s picture

Jastraat,

I tried that a while back, but couldn't get it to work. But my skills and understanding of Drupal have improved since. I went back to my code and your solution works great. I hadn't been able to find the format settings previously, but now I've got them.

Only thing to note is that you don't need the return $form; line with node_form_alter-- that piece is now handled by the function.

Thanks for the help.

jastraat’s picture

Ah - good to know :)

clude’s picture

FileSize
11.72 KB
10.85 KB

Hello,

i have the same problem in Drupal 7.8. The default text format only saved, if one or more chars in the default text field.
The the screenshots.....

hedley’s picture

This seems to be a related issue, or a duplicate? http://drupal.org/node/1278886

mcfilms’s picture

Category: feature » bug

This presents itself as a bug to the end-user. As originally pointed out, having the ability to choose the "default text format" in the text field initially seems like a feature. But when you set it and it doesn't remember your choice, it is a bug. Additionally, the Configuration » Content authoring » Text formats is so disconnected from the content configuration, I don't think a casual user will make that leap. And even if they do, that area is for global settings.

Paulmicha’s picture

sun’s picture

devad’s picture

Issue summary: View changes

For my usecase #7 was too general, and #20 too specific.
I needed it for all node forms (all content types).
So I did it with hook_form_BASE_FORM_ID_alter().

/*
 * Implementation of hook_form_BASE_FORM_ID_alter()
 */
function MODULENAME_form_BASE_FORM_ID_alter(&$form, &$form_state, $form_id) {  
  if (isset($form['FIELD_NAME']) && !isset($form['FIELD_NAME']['und']['0']['#format'])) {
    $form['FIELD_NAME']['und']['0']['#format'] = "TEXT_FORMAT";
  }
}

In my case:

function mymodule_form_node_form_alter(&$form, &$form_state, $form_id) {  
  if (isset($form['body']) && !isset($form['body']['und']['0']['#format'])) {
    $form['body']['und']['0']['#format'] = "filtered_html";
  }
}
aangel’s picture

devad's solution didn't work for me but this did:

function vc_extra_form_node_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['body']) && !isset($form['body']['und']['0']['#format'])) {
    $form['body']['widget'][0]['#format'] = "full_html";
  }
}

This really should be happening when the node is instantiated, not in the user interface layer. That sort of OOPy technique might be doable in D8. Have to look into that.