1

I am using Views Database Connector to display safe data from external database in Drupal 8 within views. Some of the fields include simple html like a list which views encodes in html entities. I am using {{ fields.myfield.content }} in views-view-fields--myview.html.twig. I have searched for other solutions about using the raw value on the site but none of them seems to work.

Is there a simple solution without getting involved in preprocess functions?

0

I am not sure if a module exists for this yet, but to do this, you would need to create a field handler for Views that does not escape HTML.

Here is an example I made a year ago to achieve the same thing you are trying to do.

For the field handler, I implemented this hook to change what handled the data:

/**
 * Implements hook_views_data_alter().
 *
 * We need to look through and change the field handler for our stored item to
 * our custom handler so HTML is unescaped.
 *
 * @param array $data
 */
function search_api_view_modes_views_data_alter(array &$data) {
  $indices = \Drupal::entityTypeManager()->getStorage('search_api_index')->loadMultiple();
  $views_data_keys = array_keys($data);

  if ($indices) {
    foreach (array_keys($indices) as $index) {
      $views_index_name = 'search_api_index_' . $index;

      if (!in_array($views_index_name, $views_data_keys)) {
        continue;
      }

      foreach (array_keys($data[$views_index_name]) as $item) {
        if (preg_match('/^view_mode_(.*)/', $item)) {
          $data[$views_index_name][$item]['field']['id'] = 'search_api_view_mode';
          $data[$views_index_name][$item]['field']['fallback_handler'] = 'search_api_view_modes';
        }
      }
    }
  }
}

Anytime one of my fields was seen in a View, the field handler was set to search_api_view_mode, defined below:

/**
 * Provides a default handler for rendered entity view modes in Search API Views.
 *
 * @ingroup views_field_handlers
 *
 * @ViewsField("search_api_view_mode")
 */
class SearchApiViewMode extends FieldPluginBase implements MultiItemsFieldHandlerInterface {

  use SearchApiFieldTrait;

  /**
   * Overrides render_item to return our markup unescaped.
   * @param $count
   * @param $item
   * @return string
   */
  public function render_item($count, $item) {
    return check_markup($item['value'], 'full_html');
  }

}

check_markup is probably not advised anymore, over a Markup object, but nonetheless this is what I had to do to not have the HTML escaped. I am not sure you can reverse this at the twig level.

5
  • Thank you for your solution Kevin, but is there no way of using the equivalent of html_entity_decode or raw within twig? I did come across the linktwig Field Value module.
    – weben
    Apr 4 '17 at 15:16
  • I believe the problem is with Views, and not general field level rendering.
    – Kevin
    Apr 4 '17 at 15:26
  • Problem as in, where you need to handle it. Not a bug.
    – Kevin
    Apr 4 '17 at 15:51
  • Hi Kevin , sorry to ask, but I am more of a Drupal site builder than a developer, so where would I use this code, in the theme overide or create a custom module? Thanks
    – weben
    Apr 6 '17 at 14:28
  • You would have to make a custom module. hook_views_data_alter can only be called at the module layer, and your module would contain a plugin at src/Plugin/views/field/(class name).php.
    – Kevin
    Apr 6 '17 at 14:31
0

Probably not a best practice, but I was able to get a field from an external database that contained HTML to print using the markup, instead of printing out the tags by doing the following in twig (in Drupal 8.5.3). I used the raw field contents and wrapped that in autoescape false.

In my case I put it in a views-view-fields-MY_VIEW_NAME.html.twig template in my theme using the following:

{% autoescape false %}{{ field.raw }}{% endautoescape %}

I'm imagining that you could use something like Twig Tweak and Rewrite Results of MY_FIELD directly in Views to do this, also:

{% autoescape false %}{{ MY_FIELD|raw }}{% endautoescape %}

I know it's a bit late, but hopefully it'll help other Googlers like me.

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.