function FieldPluginBase::renderText

Same name in other branches
  1. 8.9.x core/modules/views/src/Plugin/views/field/FieldPluginBase.php \Drupal\views\Plugin\views\field\FieldPluginBase::renderText()
  2. 10 core/modules/views/src/Plugin/views/field/FieldPluginBase.php \Drupal\views\Plugin\views\field\FieldPluginBase::renderText()
  3. 11.x core/modules/views/src/Plugin/views/field/FieldPluginBase.php \Drupal\views\Plugin\views\field\FieldPluginBase::renderText()

Overrides FieldHandlerInterface::renderText

1 call to FieldPluginBase::renderText()
FieldPluginBase::advancedRender in core/modules/views/src/Plugin/views/field/FieldPluginBase.php
Renders a field using advanced settings.

File

core/modules/views/src/Plugin/views/field/FieldPluginBase.php, line 1239

Class

FieldPluginBase
Base class for views fields.

Namespace

Drupal\views\Plugin\views\field

Code

public function renderText($alter) {
    // We need to preserve the safeness of the value regardless of the
    // alterations made by this method. Any alterations or replacements made
    // within this method need to ensure that at the minimum the result is
    // XSS admin filtered. See self::renderAltered() as an example that does.
    $value_is_safe = $this->last_render instanceof MarkupInterface;
    // Cast to a string so that empty checks and string functions work as
    // expected.
    $value = (string) $this->last_render;
    if (!empty($alter['alter_text']) && $alter['text'] !== '') {
        $tokens = $this->getRenderTokens($alter);
        $value = $this->renderAltered($alter, $tokens);
        // $alter['text'] is entered through the views admin UI and will be safe
        // because the output of $this->renderAltered() is run through
        // Xss::filterAdmin().
        // @see \Drupal\views\Plugin\views\PluginBase::viewsTokenReplace()
        // @see \Drupal\Component\Utility\Xss::filterAdmin()
        $value_is_safe = TRUE;
    }
    if (!empty($this->options['alter']['trim_whitespace'])) {
        $value = trim($value);
    }
    // Check if there should be no further rewrite for empty values.
    $no_rewrite_for_empty = $this->options['hide_alter_empty'] && $this->isValueEmpty($this->original_value, $this->options['empty_zero']);
    // Check whether the value is empty and return nothing, so the field isn't rendered.
    // First check whether the field should be hidden if the value(hide_alter_empty = TRUE) /the rewrite is empty (hide_alter_empty = FALSE).
    // For numeric values you can specify whether "0"/0 should be empty.
    if (($this->options['hide_empty'] && empty($value) || $alter['phase'] != static::RENDER_TEXT_PHASE_EMPTY && $no_rewrite_for_empty) && $this->isValueEmpty($value, $this->options['empty_zero'], FALSE)) {
        return '';
    }
    // Only in empty phase.
    if ($alter['phase'] == static::RENDER_TEXT_PHASE_EMPTY && $no_rewrite_for_empty) {
        // If we got here then $alter contains the value of "No results text"
        // and so there is nothing left to do.
        return ViewsRenderPipelineMarkup::create($value);
    }
    if (!empty($alter['strip_tags'])) {
        $value = strip_tags($value, $alter['preserve_tags']);
    }
    $more_link = '';
    if (!empty($alter['trim']) && !empty($alter['max_length'])) {
        $length = strlen($value);
        $value = $this->renderTrimText($alter, $value);
        if ($this->options['alter']['more_link'] && strlen($value) < $length) {
            $tokens = $this->getRenderTokens($alter);
            $more_link_text = $this->options['alter']['more_link_text'] ? $this->options['alter']['more_link_text'] : $this->t('more');
            $more_link_text = strtr(Xss::filterAdmin($more_link_text), $tokens);
            $more_link_path = $this->options['alter']['more_link_path'];
            $more_link_path = strip_tags(Html::decodeEntities($this->viewsTokenReplace($more_link_path, $tokens)));
            // Make sure that paths which were run through URL generation work as
            // well.
            $base_path = base_path();
            // Checks whether the path starts with the base_path.
            if (strpos($more_link_path, $base_path) === 0) {
                $more_link_path = mb_substr($more_link_path, mb_strlen($base_path));
            }
            // @todo Views should expect and store a leading /. See
            //   https://www.drupal.org/node/2423913.
            $options = [
                'attributes' => [
                    'class' => [
                        'views-more-link',
                    ],
                ],
            ];
            if (UrlHelper::isExternal($more_link_path)) {
                $more_link_url = CoreUrl::fromUri($more_link_path, $options);
            }
            else {
                $more_link_url = CoreUrl::fromUserInput('/' . $more_link_path, $options);
            }
            $more_link = ' ' . $this->linkGenerator()
                ->generate($more_link_text, $more_link_url);
        }
    }
    if (!empty($alter['nl2br'])) {
        $value = nl2br($value);
    }
    if ($value_is_safe) {
        $value = ViewsRenderPipelineMarkup::create($value);
    }
    $this->last_render_text = $value;
    if (!empty($alter['make_link']) && (!empty($alter['path']) || !empty($alter['url']))) {
        if (!isset($tokens)) {
            $tokens = $this->getRenderTokens($alter);
        }
        $value = $this->renderAsLink($alter, $value, $tokens);
    }
    // Preserve whether or not the string is safe. Since $more_link comes from
    // \Drupal::l(), it is safe to append. Check if the value is an instance of
    // \Drupal\Component\Render\MarkupInterface here because renderAsLink()
    // can return both safe and unsafe values.
    if ($value instanceof MarkupInterface) {
        return ViewsRenderPipelineMarkup::create($value . $more_link);
    }
    else {
        // If the string is not already marked safe, it is still OK to return it
        // because it will be sanitized by Twig.
        return $value . $more_link;
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.