0

I'm using textbox replace in a view for an ICAL and I need to get the special characters to render. Right now calendars like Google Calendar just shows the encoded characters instead of the decoded characters. I want ' instead of this ' .

right now it's not doing anything. I've tried multiple ways to pull this off using |render or using replace | but nothing works. It still just shows up with the encoded characters. example below

{% set special_1 = ''' %}
{% set result_1 = "'" %}
{% set special_2 = '&' %}
{% set result_2 = '&' %}

{{ title_1|replace({ (special_1): result_1, (special_2): result_2 }) }} - {{ field_sport|replace({ (special_1): result_1, (special_2): result_2 }) }}
1

You can write try custom Twig_Extention

In yourmodule create service

yourmodule.service.yml

services:
  custom_replace:
    class: Drupal\yourmodule\TwigExtension\Custom_Replace
    arguments: ['@renderer']
    tags:
      - { name: twig.extension }

src/TwigExtension/Custom_Replace.php

<?php

namespace Drupal\yourmodule\TwigExtension;

/**
 * Class Custom_Replace.
 */
class Custom_Replace extends \Twig_Extension {


  /**
   * {@inheritdoc}
   */
  public function getName() {
    return 'custom_replace';
  }

  public function getFunctions() {
    return [
      new \Twig_SimpleFunction(
        'custom_replace',
        [$this, 'custom_replace'],
        ['is_safe' => ['html']]
      ),
    ];
  }



  /**
   * {@inheritdoc}
   * @param $search can a array [`word1`,`work2`] or string `word`
   */
  public function custom_replace($string, $search, $replace = '') {
    return str_ireplace($search, $replace, $string);
  }
}

In template yourpage.html.twig

{{ custom_replace('word','',string) }}
  • I think this would work to get the view to use replace() extension but I've discovered for my problem even if I do replace and put & for example the view will still print the special characters &amp; It's more of a core drupal issue here. – Tyler Aug 29 '18 at 15:07

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.