You are here

public function ContentBrowserPreview::render in Content Browser 8

Renders the field.

Parameters

\Drupal\views\ResultRow $values: The values retrieved from a single row of a view's query result.

Return value

string|\Drupal\Component\Render\MarkupInterface The rendered output. If the output is safe it will be wrapped in an object that implements MarkupInterface. If it is empty or unsafe it will be a string.

Overrides FieldPluginBase::render

File

src/Plugin/views/field/ContentBrowserPreview.php, line 72

Class

ContentBrowserPreview
Defines a custom field that renders a preview of a Content Entity type, and allows for changing field settings with exposed input.

Namespace

Drupal\content_browser\Plugin\views\field

Code

public function render(ResultRow $values) {
  $build = [];
  $entity = $this
    ->getEntity($values);

  // If the related Entity is not a Content Entity, return early.
  if (!$entity instanceof ContentEntityInterface) {
    return $build;
  }
  $exposed_view_mode = isset($this->view->exposed_data['view_mode']) ? $this->view->exposed_data['view_mode'] : FALSE;
  $view_mode_options = $this->entityDisplayRepository
    ->getViewModeOptions($this
    ->getEntityType());
  if ($this->options['exposed_view_mode'] && $exposed_view_mode && isset($view_mode_options[$exposed_view_mode])) {
    $view_mode = $exposed_view_mode;
  }
  else {
    $view_mode = isset($this->options['view_mode']) ? $this->options['view_mode'] : 'teaser';
  }
  $entity_view = $this->entityTypeManager
    ->getViewBuilder($entity
    ->getEntityTypeId())
    ->view($entity, $view_mode);
  return $entity_view;
}