You are here

public function BlazyStylePluginTrait::getFieldString in Blazy 8

Returns the string values for the expected Title, ET label, List, Term.

@todo re-check this, or if any consistent way to retrieve string values.

File

src/Dejavu/BlazyStylePluginTrait.php, line 281

Class

BlazyStylePluginTrait
A Trait common for optional views style plugins.

Namespace

Drupal\blazy\Dejavu

Code

public function getFieldString($row, $field_name, $index) {
  $values = [];
  $renderer = $this->blazyManager
    ->getRenderer();

  // Content title/List/Text, either as link or plain text.
  if ($value = $this
    ->getFieldValue($index, $field_name)) {
    $value = is_array($value) ? array_filter($value) : $value;

    // Entity reference label where the above $value can be term ID.
    if ($markup = $this
      ->getField($index, $field_name)) {
      $value = is_object($markup) ? trim(strip_tags($markup
        ->__toString())) : $value;
    }

    // Tags has comma separated value, although can be changed, just too much.
    if (is_string($value) && strpos($value, ',') !== FALSE) {
      $tags = explode(',', $value);
      $rendered_tags = [];
      foreach ($tags as $tag) {
        $rendered_tags[] = Html::cleanCssIdentifier(mb_strtolower(trim($tag)));
      }
      $values[$index] = implode(' ', $rendered_tags);
    }
    else {
      $value = is_string($value) ? $value : (isset($value[0]['value']) && !empty($value[0]['value']) ? $value[0]['value'] : '');
      $values[$index] = empty($value) ? '' : Html::cleanCssIdentifier(mb_strtolower($value));
    }
  }

  // Term reference/ET, either as link or plain text.
  if (empty($values)) {
    if ($renderable = $this
      ->getFieldRenderable($row, $index, $field_name, TRUE)) {
      $value = [];
      foreach ($renderable as $key => $render) {
        $class = isset($render['rendered']['#title']) ? $render['rendered']['#title'] : $renderer
          ->render($render['rendered']);
        $class = trim(strip_tags($class));
        $value[$key] = Html::cleanCssIdentifier(mb_strtolower($class));
      }
      $values[$index] = empty($value) ? '' : implode(' ', $value);
    }
  }
  return $values;
}