Comments

wjaspers’s picture

Adding patch for testing.

wjaspers’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, drupal-enforce-specific-text-formats-1419016-1.patch, failed testing.

wjaspers’s picture

Just leaving a note for myself:
In the test case 'text_processing' is a boolean.

Questions to the greater community:
A. Does the test case need to be updated?
B. Should the patch be updated to allow the field settings to use checkboxes instead of radio buttons? Then the field definition can allow multiple specific formats (assuming the end user has access to each of them).

wjaspers’s picture

StatusFileSize
new26.38 KB

Just for anyone who's interested to see that this is working...
I just need to resolve the testing and this patch should apply nicely.

wjaspers’s picture

Figured out how to make the field capable of defining multiple specific text formats, but at the moment I don't know how much this will affect other areas of core. Still trying to figure out the best course of action for #4.

wjaspers’s picture

Priority: Normal » Major
Status: Needs work » Needs review
StatusFileSize
new2.7 KB

I think I got it now. This patch will allow the formatter to define either "plain text", a single, or multiple formats.
EDITED: Updated to re-trigger test request.

Status: Needs review » Needs work
willvincent’s picture

TestTranslationTestCase is failing, that looks like the only issue

tstoeckler’s picture

In general this looks really good. Below is a code-review but those are mostly minor issues.

I have one more general problem with this. Instead of the Form API element trying to figure out, what Field API wants to allow as text formats, really it should be the other way around: Field API specifying upfront in text_field_widget_form() what formats it wants to allow.

The problem is that there is no facility for that in the #text_format Form API element. So I would suggest opening a new issue for that, something á la: "Introduce a #formats property for the #text_format element, to preselect/-reduce available formats"

Here's the detailed review:

+++ b/modules/field/modules/text/text.module
@@ -87,15 +87,40 @@ function text_field_settings_form($field, $instance, $has_data) {
+    // Check for disabled filter formats.
+    if (empty($format->status)) {
+      continue;
+    }
+    $formats[$format->format] = $format->name;

Minor, but I think if ($format->status) would be more concise. That's just my opinion, though.

+++ b/modules/field/modules/text/text.module
@@ -87,15 +87,40 @@ function text_field_settings_form($field, $instance, $has_data) {
-      t('Plain text'),
-      t('Filtered text (user selects text format)'),
+       t('Plain text'),
+       t('Filtered text (user selects input format)'),

These changes need to be reverted, the indentation was fine before.

+++ b/modules/field/modules/text/text.module
@@ -87,15 +87,40 @@ function text_field_settings_form($field, $instance, $has_data) {
+    '#default_value' => !empty($settings['text_formats']) ? $settings['text_formats'] : array(),

We can initialize the default value of these settings in text_field_info(). That way the check isn't needed.

+++ b/modules/field/modules/text/text.module
@@ -87,15 +87,40 @@ function text_field_settings_form($field, $instance, $has_data) {
+    '#states' => array(

Yay, I love more #states in core!!!

+++ b/modules/filter/filter.module
@@ -866,6 +866,21 @@ function filter_process_format($element) {
+    // If text_processing == 1, skip this step, since it will fall back to only formats the user has access to.

That comment is incorrect. The condition itself is OK, but it is not described correctly.

+++ b/modules/filter/filter.module
@@ -866,6 +866,21 @@ function filter_process_format($element) {
+    if (!empty($field_info['settings']['text_processing']) && isset($field_info['settings']['text_formats'])) {

Since text_processing is either 0 or 1, I think simply dropping the !empty() should be fine. That's just a matter of taste, though.

I didn't try it out (yet), but I think $field_info['settings']['text_formats'] will always be set (at least if we initialize the default as explained above), so a !empty() would be in order here, I think.

+++ b/modules/filter/filter.module
@@ -866,6 +866,21 @@ function filter_process_format($element) {
+      // Hide unavailable formats. (They'll be keyed as '<format_name>' => 0).

We don't use abbreviations in comments. Also I think "unavailable" might be misleading. Maybe we could just write:

Formats that have been disabled for this field instance, have a value of 0.<code> Or something like that...

<code>
+++ b/modules/filter/filter.module
@@ -866,6 +866,21 @@ function filter_process_format($element) {
+      // @TODO: How do we define a sensible default?
+      $element['#format'] = end($options);

Ideally the default would have been specified on the field instance setting form. I don't know, off the top of my hat, though, why that might not be the case.

wjaspers’s picture

Status: Needs work » Needs review
StatusFileSize
new2.5 KB

Trying another shot at this where I don't try to preset $element['#format'] if multiple values are defined.

tstoeckler’s picture

I opened #1423244: #allowed_formats property for #type 'text_format' to filter available formats.

I posted some example code there, and if it really is as easy as I claim over there, then we might want to re-merge the issues. Not sure, though.

wjaspers’s picture

@tstoeckler, Thanks for all your feedback!

+++ b/modules/field/modules/text/text.module
@@ -87,15 +87,40 @@ function text_field_settings_form($field, $instance, $has_data) {
+    // Check for disabled filter formats.
+    if (empty($format->status)) {
+      continue;
+    }
+    $formats[$format->format] = $format->name;
Minor, but I think if ($format->status) would be more concise. That's just my opinion, though.

I generally don't like to assume that a value that even if it is 1 or 0 could be interpreted as TRUE or FALSE. On the off-chance another module modifies or fouls up the $format->status property, and its NULL, the if() could fail or worse, dump errors on the end-user.

+++ b/modules/field/modules/text/text.module
@@ -87,15 +87,40 @@ function text_field_settings_form($field, $instance, $has_data) {
-      t('Plain text'),
-      t('Filtered text (user selects text format)'),
+       t('Plain text'),
+       t('Filtered text (user selects input format)'),

That was a side-effect of playing around too much and forgetting to revert some changes. I've since fixed that, but thanks for catching it.

+++ b/modules/field/modules/text/text.module
@@ -87,15 +87,40 @@ function text_field_settings_form($field, $instance, $has_data) {
+    '#default_value' => !empty($settings['text_formats']) ? $settings['text_formats'] : array(),
We can initialize the default value of these settings in text_field_info(). That way the check isn't needed.

Good call, I'll give that a try.

+++ b/modules/filter/filter.module
@@ -866,6 +866,21 @@ function filter_process_format($element) {
+    // If text_processing == 1, skip this step, since it will fall back to only formats the user has access to.

I'll cleanup my comments.

Status: Needs review » Needs work
wjaspers’s picture

Just for the sake of comment, it seems like something may be wrong with the TextTranslationTest.

I created a node with multiple longtext fields (with varying format settings), and translated it 3 times for fun. No watchdog errors, no PHP notices, and no fatals. Just to try and throw a wrench in the mix, I updated the field values here and there, but no bugs.
I've attached both the side-by-side screenshots of the original content (English [at left], up to Spanish [at right]).
I've also included a screenshot of the Devel information about a translated node. According to the tests as they failed, the value and format are both present.

wjaspers’s picture

Status: Needs work » Needs review
StatusFileSize
new3.49 KB

Following @tstoeckler's comments, I've updated my patch. I've also renamed the settings variable we'll use to allow this functionality so its a little more descriptive.

Status: Needs review » Needs work
wjaspers’s picture

Status: Needs work » Needs review
StatusFileSize
new4.11 KB

Here's another crack at it. Finally figured out how to handle the #default_value property for the format setting as well.

Status: Needs review » Needs work
tstoeckler’s picture

Status: Needs work » Postponed
+++ b/modules/field/modules/text/text.module
@@ -42,14 +48,21 @@ function text_field_info() {
+        'text_processing' => 1, ¶

@@ -96,6 +109,29 @@ function text_field_instance_settings_form($field, $instance) {
+  ¶

+++ b/modules/filter/filter.module
@@ -861,6 +861,28 @@ function filter_process_format($element) {
+      // Use the default format value from the field settings. ¶

Trailing whitespace.

+++ b/modules/filter/filter.module
@@ -861,6 +861,28 @@ function filter_process_format($element) {
+        // Filter the list of options to only formats the field has permitted and the user has access to.

This comment is incorrect. It's not about whether the user has access to this format or not. That is being handled by the code preceding the bit in the patch. This is about checking whether each of the formats that are allowed for the user, are in the allowed formats for the textfield. That said array_intersect() could be used here, which would make the code a bit leaner.

I still think this should be postponed on #1423244: #allowed_formats property for #type 'text_format' to filter available formats, so marking as such. Please revert if you disagree.

wjaspers’s picture

David_Rothstein’s picture

Status: Postponed » Closed (duplicate)

This is a duplicate of #784672: Allow text field to enforce a specific text format, which is the equivalent issue for Drupal 8. We should only have one issue open for this, and any patch needs to get committed to Drupal 8 first before being considered for possible Drupal 7 backport.

As far as I know, closing this will not prevent the testbot from testing any patches that are posted here (I could be wrong though)... If so, this issue could still be used as a place to get the testbot to test Drupal 7 versions of any patches, which was the original reason it was opened above.