3

I'm trying to strip the HTML tags from a JSON response I generated using Services Views, but I'm not having any luck so far. I have a content type with a Field Collections field in it that is outputting HTML tags no matter what I do. I've tried the following:

  • Check Strip HTML tags in the field settings in the view
  • Set customize field HTML to 'None'
  • Set Customize field and label wrapper HTML to 'None'
  • All combinations of the above
  • Making sure to turn of all 'Provide default field wrapper elements' options

Am I missing anything? My output still looks like this in my json:

"Animals": [
            "<div class=\"entity entity-field-collection-item field-collection-item-field-product-animals clearfix\">\n  <div class=\"content\">\n    <div class=\"field field-name-field-product-animal field-type-taxonomy-term-reference field-label-above\"><div class=\"field-label\">Animal:&nbsp;</div><div class=\"field-items\"><div class=\"field-item even\">Big</div></div></div><div class=\"field field-name-field-product-withdrawal-time field-type-text field-label-above\"><div class=\"field-label\">Withdrawal time:&nbsp;</div><div class=\"field-items\"><div class=\"field-item even\">25 dg</div></div></div>  </div>\n</div>\n",
            "<div class=\"entity entity-field-collection-item field-collection-item-field-product-animals clearfix\">\n  <div class=\"content\">\n    <div class=\"field field-name-field-product-animal field-type-taxonomy-term-reference field-label-above\"><div class=\"field-label\">Animal:&nbsp;</div><div class=\"field-items\"><div class=\"field-item even\">Rund vlees</div></div></div><div class=\"field field-name-field-product-withdrawal-time field-type-text field-label-above\"><div class=\"field-label\">Withdrawal time:&nbsp;</div><div class=\"field-items\"><div class=\"field-item even\">21 dg</div></div></div>  </div>\n</div>\n",
            "<div class=\"entity entity-field-collection-item field-collection-item-field-product-animals clearfix\">\n  <div class=\"content\">\n    <div class=\"field field-name-field-product-animal field-type-taxonomy-term-reference field-label-above\"><div class=\"field-label\">Animal:&nbsp;</div><div class=\"field-items\"><div class=\"field-item even\">Pluimvee ei</div></div></div><div class=\"field field-name-field-product-withdrawal-time field-type-text field-label-above\"><div class=\"field-label\">Withdrawal time:&nbsp;</div><div class=\"field-items\"><div class=\"field-item even\">6 dg</div></div></div>  </div>\n</div>\n"

Any help on this is greatly appreciated.

0

You have to tell use what you have done so far.

To strip tags use strip_tags php code. This function takes two parameters:

string strip_tags ( string $str , [string $allowable_tags ] )

The first argument is the string and the optional second parameter is to specify tags which should not be stripped. For example

<?php
    echo strip_tags("Hello <b><i>world!</i></b>","<b>");
?>

Will output this

Hello world!

Not

Hello world!


I guess you have to use views_datasource module to generate JSON. I have already used this module and no default HTML tag were inserted by the View.

Did you try preprocess functions to strip the tags from the view?

  • I was under the impression that I did say what I have done so far in my initial post. However since then I have tried editing the Views module and replaced the strip_tags implementation based on this link but I am still getting the same result. – Sander van't Veer Jun 30 '15 at 12:24
  • You had to put the link in your question. Where did you put strip_tags code? – M a m a D Jun 30 '15 at 12:27
  • In views/handler/views_field_handler.inc, line 1157: if (!empty($alter['strip_tags'])) { //$value = strip_tags($value, $alter['preserve_tags']); $value = strip_tags(html_entity_decode($value), $alter['preserve_tags']); } The link described the issue coming from the fact that Drupal was sending encoded tags, which didn't get picked up. Sadly though, this doesn't seem to fix the issue on my end. – Sander van't Veer Jun 30 '15 at 12:36
  • I updated the answer – M a m a D Jun 30 '15 at 12:44
0

You can override the output using hook_preprocess_HOOK and expose it into the variable, for example:

/**
 * Implements hook_preprocess_HOOK().
 */
function MYMODULE_preprocess_views_view_field(&$vars) {
  $view = $vars['view'];
  switch ($view->name) {
    case 'my_view':
      // @see: views-view-field--my-view--page--field-foo.tpl.php
      $node = entity_metadata_wrapper('node', $vars['row']->nid);
      $vars['value']  = '';

      foreach ($node->field_foo_collection->getIterator() as $item) {
        foreach ($item->field_multivalued as $value) {
          $vars['value'] .= '[ ' . $value . ' ], ';
        }
      }
    break;
  }
}

Then print the $value in your template file (e.g. views-view-field--my-view--page--field-foo.tpl.php), e.g.

<?= $value ?>

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.