Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
After creating a text field, you can define the default value for the text format. The selected value is ignored on save.
Comment | File | Size | Author |
---|---|---|---|
#23 | savednot.png | 10.85 KB | clude |
#23 | savedright.png | 11.72 KB | clude |
Comments
Comment #1
droplet CreditAttribution: droplet commented"The selected value is ignored on save" ??
how to reproduce this
Comment #2
sharpbites CreditAttribution: sharpbites commentedHere 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".
Comment #3
droplet CreditAttribution: droplet commentedbug confirmed.
Comment #4
yched CreditAttribution: yched commentedA '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.
Comment #5
sharpbites CreditAttribution: sharpbites commentedThanks 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?
Comment #6
yched CreditAttribution: yched commented@sharpbites: I guess it could be changed in a contrib module, but probably not through the 'default field value' UI.
Comment #7
francismacomber CreditAttribution: francismacomber commentedYou can create a simple module using hook_form_alter() to set the default text format per field in the following way:
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.
Comment #8
Sol Roth CreditAttribution: Sol Roth commentedIs 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?
Comment #9
yched CreditAttribution: yched commented"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)
Comment #10
Starbuck CreditAttribution: Starbuck commentedI 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.
Comment #11
mightyiam CreditAttribution: mightyiam commentedI'd like this also.
Comment #12
seandunaway CreditAttribution: seandunaway commentedRan into the same issue, +1 and subscribe.
Comment #13
naplesweb CreditAttribution: naplesweb commentedA 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"
Comment #14
jkaine CreditAttribution: jkaine commentednaplesweb,
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:
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.
Comment #15
dpouliot CreditAttribution: dpouliot commentedI'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.
Comment #16
droplet CreditAttribution: droplet commentedComment #17
jastraat CreditAttribution: jastraat commentedIf 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.
Comment #18
emagray CreditAttribution: emagray commented#17 = Perfect solution!!!!
Comment #19
jkaine CreditAttribution: jkaine commentedemagray 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
Comment #20
jastraat CreditAttribution: jastraat commentedThis wouldn't provide a UI, but if there's a particular field on which you want to set a default format, you just need:
Comment #21
jkaine CreditAttribution: jkaine commentedJastraat,
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.
Comment #22
jastraat CreditAttribution: jastraat commentedAh - good to know :)
Comment #23
clude CreditAttribution: clude commentedHello,
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.....
Comment #24
hedley CreditAttribution: hedley commentedThis seems to be a related issue, or a duplicate? http://drupal.org/node/1278886
Comment #25
mcfilms CreditAttribution: mcfilms commentedThis 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.
Comment #26
Paulmicha CreditAttribution: Paulmicha commented@see #1278886: Default text formats are not saved properly without accompanying values
Duplicate ?
Comment #27
sun#784672: Allow text field to enforce a specific text format
Comment #28
devad CreditAttribution: devad commentedFor 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().
In my case:
Comment #29
aangel CreditAttribution: aangel as a volunteer commenteddevad's solution didn't work for me but this did:
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.