Hi,

I'm trying to expand the range of html tags that can be used in the 'Help Text' for fields. I’d like this to be applied to all fields.

I've been trying to use a D7 example and updating for D8/9, but I'm running into problems. This has led me to a lot of API stuff, that I'm not fully sure I'm understanding.

Here's what I've got:

function builder2020_field_widget_form_alter(&$element,&$form_state,&$context){
  if(isset($element[0])){
    $element[0]['#description'] = Xss::filter($context['instance']['description'], builder2020_field_filter_xss_allowed_tags());
  }
}

// Provide a more permissive set of tags to be used with filter_xss()
function builder2020_field_filter_xss_allowed_tags() {
  // Merge the new set of allowed tags with the less permissive defaults
  $new_tags = array('table', 'thead', 'tbody', 'tfoot', 'tr', 'th', 'td', 'div');
  return array_merge(allowedTags, $new_tags);
}

I think my problem is with the line:

return array_merge(allowedTags, $new_tags);

and 'allowedTags' not being an array.

Can anyone point me in the right direction?

Comments

slewazimuth’s picture

If you understand what allowed HTML tags does and how it works you'll have zero problems. However, unfortunately this never seems to be the case. Drupal 9 is Drupal 8 with the deprecated code removed. Drupal 7 and 6 use procedural code architecture. Drupal 8 and 9 use OOP architecture. Drupal 8 was built on top of Symfony which has excellent documentation and is OOP based.

gmak’s picture

Thanks. I guess I’m not as clear about this as I thought. I understand your point, but it doesn’t really help me get any closer to a solution. Can you help me understand what I need to do to my code to get it working?

wombatbuddy’s picture

One possible solution: 

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldFilteredMarkup;

/**
 * Implements hook_field_widget_form_alter().
 *
 * Allow additional tags for the field description.
 */
function YOUR_MODULE_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
  $new_allowed_tags = [
    'table',
    'tr',
    'td',
  ];
  // Merge new allowed tags with the default allowed tags.
  $allowed_tags = array_merge(FieldFilteredMarkup::allowedTags(), $new_allowed_tags);
  $unfiltered_description = $context['items']->getFieldDefinition()->getDescription();
  // PHP 7.4.0 enables to pass allowed tags as array to the strip_tags().
  $element['value']['#description'] = strip_tags($unfiltered_description, $allowed_tags);
}

/**
 * Implements hook_form_FORM_ID_alter() for field_config_edit_form.
 *
 * Add new allowed tags to the description of the "Help text" field.
 */
function YOUR_MODULE_form_field_config_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $allowed_tags = FieldFilteredMarkup::displayAllowedTags();
  // Add our new allowed tags to display in the description.
  $allowed_tags .= ' <table> <tr> <td>';

  $description = t('Instructions to present to the user below this field on the editing form.');
  $description .= '<br />';
  $description .= t('Allowed HTML tags: tags: @tags', ['@tags' => $allowed_tags]);
  $description .= '<br />';
  $description .= t('This field supports tokens.');

  $form['description']['#description'] = $description;
}