You are here

public function ViewsIfEmpty::render in Views If Empty 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/ViewsIfEmpty.php, line 133
Contains \Drupal\views_ifempty\Plugin\views\field\ViewsIfEmpty.

Class

ViewsIfEmpty
Field handler to output an alternate field when a field is empty.

Namespace

Drupal\views_ifempty\Plugin\views\field

Code

public function render(ResultRow $values) {
  $emptyfield = $this->options['emptyfield'];
  $outputfield = $this->options['outputfield'];

  // Double-check that the field has been configured properly.
  if (!empty($emptyfield) && !empty($outputfield) && $emptyfield != $outputfield) {

    // Get all the available fields.
    $fields = $this->view->display_handler
      ->getHandlers('field');
    if (isset($fields[$emptyfield]) && isset($fields[$outputfield])) {

      // Is emptyfield empty? If so, output outputfield.
      if (empty($fields[$emptyfield]->last_render)) {

        // If we've selected to reverse the behavior, output nothing.
        if ($this->options['reverse']) {
          $this->last_render = '';
        }
        else {
          $this->last_render = $fields[$outputfield]->last_render;
        }
      }
      else {

        // If we've selected to reverse the behavior, output $outputfield.
        if ($this->options['reverse']) {
          $this->last_render = $fields[$outputfield]->last_render;
        }
        else {
          $this->last_render = $fields[$emptyfield]->last_render;
        }
      }
    }
  }
  return $this->last_render;
}