You are here

public function BlazyEntity::getFieldTextOrLink in Blazy 8.2

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

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

File

src/BlazyEntity.php, line 232

Class

BlazyEntity
Provides common entity utilities to work with field details.

Namespace

Drupal\blazy

Code

public function getFieldTextOrLink($entity, $field_name, $settings, $multiple = TRUE) {
  if ($entity
    ->hasField($field_name)) {
    $langcode = $settings['langcode'];
    if ($text = $this
      ->getFieldValue($entity, $field_name, $langcode)) {
      if (!empty($text[0]['value']) && !isset($text[0]['uri'])) {

        // Prevents HTML-filter-enabled text from having bad markups (h2 > p),
        // except for a few reasonable tags acceptable within H2 tag.
        $text = $this
          ->getFieldString($entity, $field_name, $langcode, FALSE);
      }
      elseif (isset($text[0]['uri']) && !empty($text[0]['title'])) {
        $text = $this
          ->getFieldRenderable($entity, $field_name, $settings['view_mode'], $multiple);
      }

      // 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 [];
}