0

I have a module that's creating a custom node type. I'm attaching a textarea to this node type. Currently it works, but the text formatting defaults to "Plain Text."

I would like to specify during installation that the field should be set to "Filtered Text," specifically, "Filtered HTML." This seems like something that should be possible when defining the instance of the field, but it's not working.

Here's the instance definition as it stands now:

<?php
// ...more fields above
    'release_notes' => array(
      'field_name' => 'release_notes',
      'type' => 'text_long',
      'label'  => $t('Notes'),
      'description'  => $t('Release notes (e.g. liner notes)'),
      'widget' => array('type' => 'text_textarea'),
      'settings' => array('text_processing ' => '1'), // Filtered HTML...?
      'weight' => 9,
    ),
// ...more fields below
?>

Does anyone have any ideas?

Thanks.

2 Answers 2

3

Set the 'format' => 'filtered_html' to allow filtered html

<?php
    // ...more fields above
        'release_notes' => array(
          'field_name' => 'release_notes',
          'type' => 'text_long',
          'label'  => $t('Notes'),
          'description'  => $t('Release notes (e.g. liner notes)'),
          'widget' => array('type' => 'text_textarea'),
          'settings' => array('text_processing ' => '1'), // Filtered HTML...?
          'format' => 'filtered_html' // <<--- Set this value to allow filtered HTML
          'weight' => 9,
        ),
    // ...more fields below
    ?>
1
  • Actually, there is a slight mistake in this. The keys are not 'type', 'format' etc... They're actually '#type', '#format' and so on...
    – shivams
    Commented Oct 28, 2014 at 15:07
3

I had thought that setting text_processing in the settings array would automatically set the filter, but all that does is allow anything other than plaintext.

In fact, you need to set this, and you need to specify the format value for the instance as a whole. The key to allow filtered HTML is filter_html.

So, here is the new hotness:

<?php
// ...more fields above
    'release_notes' => array(
      'field_name' => 'release_notes',
      'type' => 'text_long',
      'label'  => $t('Notes'),
      'description'  => $t('Release notes (e.g. liner notes)'),
      'widget' => array('type' => 'text_textarea'),
      'settings' => array('text_processing' => 1), // Allow text processing
      'format' => 'filter_html', // <<--- Set this value to allow filtered HTML
      'weight' => 9,
    ),
// ...more fields below
?>

You can get a list of the default filters in filter.module's implementation of hook_filter_info(): https://api.drupal.org/api/drupal/modules%21filter%21filter.module/funct...

Hope this helps someone, it took me forever to figure this out.

If you try to programmatically save a node with a custom field, and that field has HTML in it, then you need to do this. Otherwise the HTML won't be stripped - it will be converted to its entity counterparts and displayed to the user.

1
  • Actually, there is a slight mistake in this. The keys are not 'type', 'format' etc... They're actually '#type', '#format' and so on...
    – shivams
    Commented Oct 28, 2014 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.