You are here

public function ViewsConditionalField::render in Views Conditional 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/ViewsConditionalField.php, line 249

Class

ViewsConditionalField
Field handler to flag the node type.

Namespace

Drupal\views_conditional\Plugin\views\field

Code

public function render(ResultRow $values) {
  $if = $this->options['if'];
  $condition = $this->options['condition'];
  $equalto = $this->options['equalto'];
  $then = $this->options['then'];
  $or = $this->options['or'] ?: '';

  // Translate text to be displayed with a context specific to this module,
  // view and display.
  $translation_context = "views_conditional:view:{$this->view->id()}";

  // Translate prior to replacements, otherwise the dynamic replacement
  // content results in endless translations:
  if ($this->options['then_translate']) {
    $then = $this
      ->t($then, [
      'context' => $translation_context,
    ]);
  }
  if ($this->options['or_translate']) {
    $or = $this
      ->t($or, [
      'context' => $translation_context,
    ]);
  }

  // Gather field information.
  $fields = $this->view->display_handler
    ->getHandlers('field');
  $labels = $this->view->display_handler
    ->getFieldLabels();

  // Search through field information for possible replacement variables.
  foreach ($labels as $key => $var) {

    // If we find a replacement variable, replace it.
    if (strpos($equalto, "{{ {$key} }}") !== FALSE) {
      $field = $this
        ->cleanVar($fields[$key]);
      $equalto = str_replace("{{ {$key} }}", $field, $equalto);
    }
    if (strpos($then, "{{ {$key} }}") !== FALSE) {
      $field = $this
        ->cleanVar($fields[$key]);
      $then = str_replace("{{ {$key} }}", $field, $then);
    }
    if (strpos($or, "{{ {$key} }}") !== FALSE) {
      $field = $this
        ->cleanVar($fields[$key]);
      $or = str_replace("{{ {$key} }}", $field, $or);
    }
  }

  // If we find a date stamp replacement, replace that.
  if (strpos($equalto, 'DATE_STAMP') !== FALSE) {
    $equalto = str_replace('DATE_STAMP', $this->dateFormatter
      ->format($this->dateTime
      ->getRequestTime()), $equalto);
  }
  if (strpos($then, 'DATE_STAMP') !== FALSE) {
    $then = str_replace('DATE_STAMP', $this->dateFormatter
      ->format($this->dateTime
      ->getRequestTime()), $then);
  }
  if (strpos($or, 'DATE_STAMP') !== FALSE) {
    $or = str_replace('DATE_STAMP', $this->dateFormatter
      ->format($this->dateTime
      ->getRequestTime()), $or);
  }

  // If we find a unix date stamp replacement, replace that.
  if (strpos($equalto, 'DATE_UNIX') !== FALSE) {
    $equalto = str_replace('DATE_UNIX', $this->dateTime
      ->getRequestTime(), $equalto);
  }
  if (strpos($then, 'DATE_UNIX') !== FALSE) {
    $then = str_replace('DATE_UNIX', $this->dateTime
      ->getRequestTime(), $then);
  }
  if (strpos($or, 'DATE_UNIX') !== FALSE) {
    $or = str_replace('DATE_UNIX', $this->dateTime
      ->getRequestTime(), $or);
  }

  // Strip tags on the "if" field. Otherwise it appears to output as
  // <div class="xxx">Field data</div>...
  // ...which of course makes it difficult to compare.
  $r = isset($fields["{$if}"]->last_render) ? trim(strip_tags($fields["{$if}"]->last_render, '<img><video><iframe><audio>')) : NULL;

  // Run conditions.
  switch ($condition) {

    // Equal to.
    case 'eq':
      if ($r == $equalto) {
        return $this
          ->markup($then);
      }
      else {
        return $this
          ->markup($or);
      }
      break;

    // Not equal to.
    case 'neq':
      if ($r !== $equalto) {
        return $this
          ->markup($then);
      }
      else {
        return $this
          ->markup($or);
      }
      break;

    // Greater than.
    case 'gt':
      if ($r > $equalto) {
        return $this
          ->markup($then);
      }
      else {
        return $this
          ->markup($or);
      }
      break;

    // Greater than or equals.
    case 'gte':
      if ($r >= $equalto) {
        return $then;
      }
      else {
        return $this
          ->markup($or);
      }
      break;

    // Less than.
    case 'lt':
      if ($r < $equalto) {
        return $this
          ->markup($then);
      }
      else {
        return $this
          ->markup($or);
      }
      break;

    // Less than or equals.
    case 'lte':
      if ($r <= $equalto) {
        return $then;
      }
      else {
        return $this
          ->markup($or);
      }
      break;

    // Empty.
    case 'em':
      if (empty($r)) {
        return $this
          ->markup($then);
      }
      else {
        return $this
          ->markup($or);
      }
      break;

    // Not empty.
    case 'nem':
      if (!empty($r)) {
        return $this
          ->markup($then);
      }
      else {
        return $this
          ->markup($or);
      }
      break;

    // Contains.
    case 'cn':
      if (mb_stripos($r, $equalto) !== FALSE) {
        return $this
          ->markup($then);
      }
      else {
        return $this
          ->markup($or);
      }
      break;

    // Does NOT contain.
    case 'ncn':
      if (mb_stripos($r, $equalto) === FALSE) {
        return $this
          ->markup($then);
      }
      else {
        return $this
          ->markup($or);
      }
      break;

    // Length Equal to.
    case 'leq':
      if (mb_strlen($r) == $equalto) {
        return $this
          ->markup($then);
      }
      else {
        return $this
          ->markup($or);
      }
      break;

    // Length Not equal to.
    case 'lneq':
      if (mb_strlen($r) !== $equalto) {
        return $this
          ->markup($then);
      }
      else {
        return $this
          ->markup($or);
      }
      break;

    // Length Greater than.
    case 'lgt':
      if (mb_strlen($r) > $equalto) {
        return $this
          ->markup($then);
      }
      else {
        return $this
          ->markup($or);
      }
      break;

    // Length Less than.
    case 'llt':
      if (mb_strlen($r) < $equalto) {
        return $this
          ->markup($then);
      }
      else {
        return $this
          ->markup($or);
      }
      break;
  }
}