In the php code I am printing date one field like

<div class="my-class">
<?php
print $row->field_name;
?>
</div>

if I add wrapper div to this field then it will print as is.
Output:
<div class="my-class"> Field Value </div>

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

gajanannehul created an issue. See original summary.

gajanannehul’s picture

Issue summary: View changes
nikunjkotecha’s picture

Is <?php opening twice really used and working?

gajanannehul’s picture

Issue summary: View changes

Its by mistake <?php open twice Nikunj. I have updated issue.

sajiniantony’s picture

I am also facing the same issue in views .please advice

navalogan’s picture

Hi all,
I have the same issue! Has anyone got a solution for this?
Thanks!

navalogan’s picture

Any news for this issue?
thx

Leagnus’s picture

Confirm the bug.
let's solve it this way:

/**
 * Implements hook_preprocess_views_view_field()
 */
function yourmodulename_preprocess_views_view_field(&$vars){
    if(isset($vars['field']->realField) && $vars['field']->realField == 'php'){
        $vars['var_is_safe'] = 1;
    }
}

in views-view-field.html.twig:

{% if var_is_safe %}
    {{ output|raw -}}
{% else %}
    {{ output -}}    
{% endif %} 
alanpeart’s picture

Nice solution Leagnus! thanks.

guignonv’s picture

Thanks for the trick Leagnus! I created a custom module for the hook and a custom theme for the twig template and it worked well!

rahul patidar’s picture

Very helpful #8. Very many thanks Leagnus!

noopal’s picture

Is it solved or not?

Im using 8 and when I use html tags they get printed instead of rendered as html.

Can you advise how I can turn html from raw text to rendered html?

Thanks

rahul patidar’s picture

You can set debug: FALSE in your service.yml file, but this will prevent the debugging for the site. So if you want to remove this without setting debug: FALSE, you have to create a twig extension in you module for removing html tags and comments. These are the steps for creating twig extension in my_module module:

  1. Create my_module.services.yml file and place the following code:
    services:
      my_module.remove_html_comments:
        arguments: ['@renderer']
        class: Drupal\my_module\TwigExtension\RemoveHtmlComments
        tags:
          - { name: twig.extension }
    
  2. Now create RemoveHtmlComments.php file in my_module/src/TwigExtension directory and place the below code in the file:
    namespace Drupal\my_module\TwigExtension;
     
    class RemoveHtmlComments extends \Twig_Extension {    
     
      /**
       * Generates a list of all Twig filters that this extension defines.
       */
      public function getFilters() {
        return [
          new \Twig_SimpleFilter('remove_html_comments', array($this, 'removeHtmlComment')),
        ];
      }
     
      /**
       * Gets a unique identifier for this Twig extension.
       */
      public function getName() {
        return 'my_module.remove_html_comments';
      }
     
      /**
       * Replaces all numbers from the string.
       */
      public static function removeHtmlComment($string, $num='') {
        $output = preg_replace('/<!--(.|\s)*?-->/', '', $string);
        if( $num != ''){
          $output .= '<br>' . strip_tags($num);
        }
        return $output;
      }
     
    }
    
  3. Your Twig extention is ready to be used in the twig files. Use this like
    {{ your_variable | remove_html_comments | raw }}
  4. Replace my_module with your module name and your_variable with the variable for which you want to remove the html comments and tags.
johnv’s picture

Priority: Major » Normal

As a maintainer, I am not sure if this needs a fix in the module itself. All solutions are fixes in custom code.
Is a fix in the module itsef needed/possible? IF so, how?
I lowered the priority, since the error only appears when additional custom code is used.
[EDIT] Sorry, wrong queue, I am not a maintainer fo this module.

gerson.analista’s picture

Status: Active » Needs review
FileSize
825 bytes

I have the same problem.
Researched how the native field "Custom text" of the Views module works (this field allows return of HTML).

"Custom text" field code:

https://api.drupal.org/api/drupal/core!modules!views!src!Plugin!views!fi...

See how the field value is returned in the render method:

/**
 * {@inheritdoc}
 */
  public function render(ResultRow $values) {

    // Return the text, so the code never thinks the value is empty.
    return ViewsRenderPipelineMarkup::create(Xss::filterAdmin($this->options['alter']['text']));
  }

I made a patch to apply the same type of return in the render function of Views PHP module.
I think it worked, could you help me test it?

boby_ui’s picture

confirm #15 is working with 8.8.5 version

alternativo’s picture

Hi guys,
Using D8.8.5 from code in custom module, new node of 'alpha' type is created, filling a formatted long text field 'description' with '...my text...'
Created a view of node type alpha, with HTML format list of that field 'description'
When i see the view, the text is formatted as plain...so '

....my text...

'
After I open a node, if I click on EDIT, doing no modify, and SAVE, the view shows the right HTML format.
Tried to use ViewsRenderPipelineMarkup::create(Xss::filterAdmin($description)), and only and tag disappear, but no HTML rendering.
also tried function Html::escape($description) but still same problem, all tags rendered as plain text.

Tried also with template raw rendering, no changes.

Any ideas how to solve?
thanks