You are here

public function BlazyEntity::getFieldTextOrLink in Blazy 7

Same name and namespace in other branches
  1. 8.2 src/BlazyEntity.php \Drupal\blazy\BlazyEntity::getFieldTextOrLink()

Returns the text or link value of the fields: link, or text.

File

src/BlazyEntity.php, line 142

Class

BlazyEntity
Implements BlazyFormatterInterface.

Namespace

Drupal\blazy

Code

public function getFieldTextOrLink($entity, $field_name, $settings) {
  if ($text = $this
    ->getFieldValue($entity, $field_name, $settings)) {

    // The $text may be just a plain string when using Entity.
    if (is_array($text)) {

      // If a link fetch the themeable output since the array is useless.
      if (isset($text[0]['url']) && !empty($text[0]['title'])) {
        $text = $this
          ->getFieldRenderable($entity, $field_name, $settings, TRUE);
      }
      elseif ($output = $this
        ->getFieldString($entity, $field_name, $settings, FALSE)) {
        $text = $output;
      }
    }

    // Prevents HTML-filter-enabled text from having bad markups
    // (h2 > p), save for few reasonable tags acceptable within H2 tag.
    return is_string($text) ? [
      '#markup' => strip_tags($text, '<a><strong><em><span><small>'),
    ] : $text;
  }
  return [];
}