Hi!

I'm trying to alter the content of a field when it is going to be edited. I figured out I had to use Forms Support, activated the rules for the specific forms (If I use a custom php code as the rule's action, it works when I want to edit the node)...
But there's one thing I can't figure: How to I save my changes? For exemple, I want to edit $form['#node']->body, to replace all occurences of a word by an other. My php code looks like that:

dsm($form['#node']->body);
$form['#node']->body = str_replace("word_1", "word_2", $form['#node']->body);
dsm($form['#node']->body);

When the second dsm is printed on screen the value is altered like I want it to be, but there's no change in the CKEditor field of my form.

I don't know if I made myself clear, if some things are a little blurry or hard to understand, please let me know and I'll try to make it clearer.

Comments

Elhu’s picture

Title: Forms Support: » Forms Support: How to alter content of a form?
mitchell’s picture

Title: Forms Support: How to alter content of a form? » Forms Support:
Status: Active » Postponed (maintainer needs more info)

Which event are you using to trigger the rule? I think you'll need 'content is going to be saved' or 'form is being submitted'. Also, try using the rules debug option.

Elhu’s picture

I use "Page Node Form is being built" event.

My goal is to alter what the user is going to see when he's editing the node. So I think that "Content is going to be saved" and "Form is being submitted" will trigger to late for that!

mitchell’s picture

Title: Forms Support: » Forms Support: How to alter content of a form?
Status: Postponed (maintainer needs more info) » Active

I kinda see what you want to achieve, but I don't think you're going about it in a workable fashion. Let me verify a couple of things:

So you have data already saved in the body field, and when the user goes to edit it, you want to alter the text that is displayed before they start editing it?

Your debug function outputs as expected, but the body field doesn't reflect that desired change in the node edit form?

Does it work when you turn off CKEditor?

Elhu’s picture

Yes, I already have data stored in my database, and I want it to be altered when the user goes to edit it, so the altering must be done before the content is loaded into the form (or after if it's possible to alter the form's content).
My debug function output as it should, but the field doesn't reflect those changes, even when I turn off CKEditor.

I'm wondering if that's because there is no "intelligent saving" on the "$form" variable on the triggered rule... I think I need to find a way to save the changes I've made.

Any idea anyone? With this method or any other, that doesn't matters as long as the job is done :P!

netbear’s picture

Try something like this:

$form['body_field']['body']['#default_value'] = str_replace("word_1", "word_2", $form['#node']->body);
mitchell’s picture

Title: Forms Support: How to alter content of a form? » Action: replace field text
Component: Forms Support » Rules Engine
Category: support » feature
Status: Active » Postponed

This may be useful to a wider audience. Another example that comes to mind is sanitizing curse words.

@Elhu: If you still have questions, please undo my changes here.

giorgio79’s picture

Status: Postponed » Closed (duplicate)

This may be a duplicate

Test the patch here for VBO:
#1125086: Search and Replace Action