1

I have a custom text format that i use in the Wysiwyg profile with ckeditor. Using Drupal 7. I want to restrict that text format use to a certain node/edit URL.

What would be the best option to achieve that either Programmatically or using custom modules?

Any advise is appreciated!

1

You can't achieve that in config, since text formats are configured per field instance, but not as granular as for to get to the node level.

You'll need too implement a hook_form_alter(), and then check the url to find out if you want to allow the format or not. Something like this:

function mymodule_form_alter($form, &$form_state, $form_id) {
  if ($form_id == 'node_type_form_to_alter' && (current_path() == 'node/%nid/edit')) {
    // Enforce desired text_format here, by altering $form array.
  }
}

That's what I would do for that case. You can take the opposite approach, and implement the same form_alter, but instead of enforcing it on a particular URL, just making sure that it's not available unless the url is the one you want to target.

Hope it helps.

EDIT: To enforce the format, you need to find the field you want to alter, inside the $form array. Wysiwyg fields use the 'text_format' when declaring the field through Form API (example here).

So, you have to find the desired field, and change the '#format' property (as in the example), to the machine name of the text format you want to use. Also, you'll see a 'format' propery in the element array. You might want to change that too if you want avoid certain formats from appearing in the dropdown widget.

I recommend you use xdebug to see the structure of the $form variable inside the hook_form_alter() implementation. Alternatively, enable the devel module, and stick a "dpm($form);" in that function, to render the structure on screen when visiting the node form.

0

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.