You are here

class RenderThis in Twig Render This 8

Twig Render This filter.

Hierarchy

  • class \Drupal\twig_render_this\TwigExtension\RenderThis extends \Drupal\twig_render_this\TwigExtension\Twig_Extension

Expanded class hierarchy of RenderThis

1 string reference to 'RenderThis'
twig_render_this.services.yml in ./twig_render_this.services.yml
twig_render_this.services.yml
1 service uses RenderThis
twig_render_this.twig_extension in ./twig_render_this.services.yml
Drupal\twig_render_this\TwigExtension\RenderThis

File

src/TwigExtension/RenderThis.php, line 12

Namespace

Drupal\twig_render_this\TwigExtension
View source
class RenderThis extends \Twig_Extension {

  /**
   * {@inheritdoc}
   */
  public function getName() {
    return 'twig_render_this.twig_extension';
  }

  /**
   * {@inheritdoc}
   */
  public function getFilters() {
    return [
      new \Twig_SimpleFilter('renderThis', [
        $this,
        'renderThisFilter',
      ]),
    ];
  }

  /**
   * Returns the rendered array for a single entity field.
   *
   * @param object $content
   *   Entity or Field object.
   * @param string $view_mode
   *   Name of the display mode.
   *
   * @return null|array
   *   A rendered array for the field or NULL if the value does not exist.
   */
  public static function renderThisFilter($content, $view_mode = 'default') {
    if ($content instanceof EntityInterface) {
      $view_builder = \Drupal::entityTypeManager()
        ->getViewBuilder($content
        ->getEntityTypeId());
      return $view_builder
        ->view($content, $view_mode);
    }
    elseif ($content instanceof FieldItemInterface || $content instanceof FieldItemListInterface || method_exists($content, 'view')) {
      return $content
        ->view($view_mode);
    }
    else {
      return t('Twig Render This: Unsupported content.');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RenderThis::getFilters public function
RenderThis::getName public function
RenderThis::renderThisFilter public static function Returns the rendered array for a single entity field.