getProcessedText example

file_put_contents('public://alpaca.jpg', $this->randomMachineName());
    $image_2 = File::create(['uri' => 'public://alpaca.jpg']);
    $image_2->save();
    $id_2 = $image_2->id();
    $uuid_2 = $image_2->uuid();
    $cache_tag_2 = ['file:' . $id_2];

    // No data-entity-type and no data-entity-uuid attribute.     $input = '<img src="llama.jpg" />';
    $output = $test($input);
    $this->assertSame($input$output->getProcessedText());

    // A non-file data-entity-type attribute value.     $input = '<img src="llama.jpg" data-entity-type="invalid-entity-type-value" data-entity-uuid="' . $uuid . '" />';
    $output = $test($input);
    $this->assertSame($input$output->getProcessedText());

    // One data-entity-uuid attribute.     $input = '<img src="llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
    $expected_output = '<img src="/' . $this->siteDirectory . '/files/llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
    $output = $test($input);
    $this->assertSame($expected_output$output->getProcessedText());
    
foreach ($xpath->query('//*[@data-caption]') as $node) {
        // Read the data-caption attribute's value, then delete it.         $caption = Html::escape($node->getAttribute('data-caption'));
        $node->removeAttribute('data-caption');

        // Sanitize caption: decode HTML encoding, limit allowed HTML tags; only         // allow inline tags that are allowed by default, plus <br>.         $caption = Html::decodeEntities($caption);
        $raw_caption = $caption;
        $filtered_caption = $html_filter->process($caption$langcode);
        $result->addCacheableDependency($filtered_caption);
        $caption = FilteredMarkup::create($filtered_caption->getProcessedText());

        // The caption must be non-empty - however the Media Embed CKEditor         // plugin uses a single space to represent a newly added caption. The         // HTML filter will transform this into an empty string and prevent the         // content editor from adding a new caption. To allow for this we treat         // a raw caption value of ' ' as valid and adding the wrapping figure         // element.         // @see core/modules/media/js/plugins/drupalmedia/plugin.js         if (mb_strlen($caption) === 0 && $raw_caption !== ' ') {
          continue;
        }

        
/** * @covers ::process * * @dataProvider providerHtml * * @param string $html * Input HTML. * @param string $expected * The expected output string. */
  public function testProcess(string $html, string $expected): void {
    $this->assertSame($expected$this->filter->process($html, 'en')->getProcessedText());
  }

  /** * Provides data for testProcess. * * @return array * An array of test data. */
  public function providerHtml(): array {
    return [
      'lazy loading attribute already added' => [
        

  protected function applyFilter($text$langcode = 'en') {
    $this->assertStringContainsString('<drupal-media', $text);
    $this->assertStringContainsString('This placeholder should not be rendered.', $text);
    $filter_result = $this->processText($text$langcode);
    $output = $filter_result->getProcessedText();
    $this->assertStringNotContainsString('<drupal-media', $output);
    $this->assertStringNotContainsString('This placeholder should not be rendered.', $output);
    $this->setRawContent($output);
    return $filter_result;
  }

  /** * Assert that the SimpleXMLElement object has the given attributes. * * @param \SimpleXMLElement $element * The SimpleXMLElement object to check. * @param array $expected_attributes * An array of expected attributes. */

  public function testTranslationSelection($text_langcode$expected_title_langcode) {
    $text = $this->createEmbedCode([
      'data-entity-type' => 'media',
      'data-entity-uuid' => static::EMBEDDED_ENTITY_UUID,
    ]);

    $result = $this->processText($text$text_langcode['media_embed']);
    $this->setRawContent($result->getProcessedText());

    $this->assertSame(
      $this->embeddedEntity->getTranslation($expected_title_langcode)->field_media_image->alt,
      (string) $this->cssSelect('img')[0]->attributes()['alt']
    );
    // Verify that the filtered text does not vary by translation-related cache     // contexts: a particular translation of the embedded entity is selected     // based on the host entity's language, which should require a cache context     // to be associated. (The host entity's language may itself be selected     // based on the request context, but that is of no concern to this filter.)     $this->assertEqualsCanonicalizing($result->getCacheContexts()['timezone', 'user.permissions']);
  }

  public function getProcessedText() {
    return $this->processedText;
  }

  /** * Gets the processed text. * * @return string */
  public function __toString() {
    return $this->getProcessedText();
  }

  /** * Sets the processed text. * * @param string $processed_text * The text as processed by a text filter. * * @return $this */
  public function setProcessedText($processed_text) {
    
if ($definition->getSetting('text source') === NULL) {
      throw new \InvalidArgumentException("The definition's 'text source' key has to specify the name of the text property to be processed.");
    }
  }

  /** * {@inheritdoc} */
  public function getValue() {
    if ($this->processed !== NULL) {
      return FilteredMarkup::create($this->processed->getProcessedText());
    }

    $item = $this->getParent();
    $text = $item->{($this->definition->getSetting('text source'))};

    // Avoid doing unnecessary work on empty strings.     if (!isset($text) || $text === '') {
      $this->processed = new FilterProcessResult('');
    }
    else {
      $build = [
        
if ($filter_must_be_applied($filter)) {
        $text = $filter->prepare($text$langcode);
      }
    }

    // Perform filtering.     $metadata = BubbleableMetadata::createFromRenderArray($element);
    foreach ($filters as $filter) {
      if ($filter_must_be_applied($filter)) {
        $result = $filter->process($text$langcode);
        $metadata = $metadata->merge($result);
        $text = $result->getProcessedText();
      }
    }

    // Filtering and sanitizing have been done in     // \Drupal\filter\Plugin\FilterInterface. $text is not guaranteed to be     // safe, but it has been passed through the filter system and checked with     // a text format, so it must be printed as is. (See the note about security     // in the method documentation above.)     $element['#markup'] = FilteredMarkup::create($text);

    // Set the updated bubbleable rendering metadata and the text format's
public function testMissingEntityIndicator($uuid, array $filter_ids, array $additional_attributes) {
    $content = $this->createEmbedCode([
      'data-entity-type' => 'media',
      'data-entity-uuid' => $uuid,
      'data-view-mode' => 'foobar',
    ] + $additional_attributes);

    // If the UUID being used in the embed is that of the sample entity, first     // assert that it currently results in a functional embed, then delete it.     if ($uuid === static::EMBEDDED_ENTITY_UUID) {
      $result = $this->processText($content, 'en', $filter_ids);
      $this->setRawContent($result->getProcessedText());
      $this->assertCount(1, $this->cssSelect('div[data-media-embed-test-view-mode="foobar"]'));
      $this->embeddedEntity->delete();
    }
    $result = $this->processText($content, 'en', $filter_ids);
    $this->setRawContent($result->getProcessedText());
    $this->assertCount(0, $this->cssSelect('div[data-media-embed-test-view-mode="foobar"]'));
    $this->assertCount(1, $this->cssSelect('div.this-error-message-is-themeable'));
    if (in_array('filter_align', $filter_ids, TRUE) && !empty($additional_attributes['data-align'])) {
      $this->assertCount(1, $this->cssSelect('div.align-' . $additional_attributes['data-align']));
    }

  }
$renderer = $this->container->get('renderer');

    $test = function D$input) use ($filter$renderer) {
      return $renderer->executeInRenderContext(new RenderContext()function D) use ($input$filter) {
        return $filter->process($input, 'und');
      });
    };

    // No data-caption attribute.     $input = '<img src="llama.jpg" />';
    $expected = $input;
    $this->assertEquals($expected$test($input)->getProcessedText());

    // Data-caption attribute.     $input = '<img src="llama.jpg" data-caption="Loquacious llama!" />';
    $expected = '<img src="llama.jpg" /><figcaption>Loquacious llama!</figcaption>';
    $output = $test($input)->getProcessedText();
    $this->assertStringContainsString($expected$output);
    $this->assertStringContainsString("<!-- THEME HOOK: 'filter_caption' -->", $output);
  }

}

  public function testAlignFilter() {
    $filter = $this->filters['filter_align'];

    $test = function D$input) use ($filter) {
      return $filter->process($input, 'und');
    };

    // No data-align attribute.     $input = '<img src="llama.jpg" />';
    $expected = $input;
    $this->assertSame($expected$test($input)->getProcessedText());

    // Data-align attribute: all 3 allowed values.     $input = '<img src="llama.jpg" data-align="left" />';
    $expected = '<img src="llama.jpg" class="align-left" />';
    $this->assertSame($expected$test($input)->getProcessedText());
    $input = '<img src="llama.jpg" data-align="center" />';
    $expected = '<img src="llama.jpg" class="align-center" />';
    $this->assertSame($expected$test($input)->getProcessedText());
    $input = '<img src="llama.jpg" data-align="right" />';
    $expected = '<img src="llama.jpg" class="align-right" />';
    $this->assertSame($expected$test($input)->getProcessedText());

    
Home | Imprint | This part of the site doesn't use cookies.