protected function JuiceboxFieldFormatter::getFieldText in Juicebox HTML5 Responsive Image Galleries 8.2
Same name and namespace in other branches
- 8.3 src/Plugin/Field/FieldFormatter/JuiceboxFieldFormatter.php \Drupal\juicebox\Plugin\Field\FieldFormatter\JuiceboxFieldFormatter::getFieldText()
 
Utility to get sanitized text directly from a field item.
This method will attempt to extract text, in a format safe for display, from the data contained within a file item. We have to generate a raw string of text here, as opposed to a render array, beacuse this output must be valid for use in both HTML and XML.
Parameters
\Drupal\Core\Field\FieldItemInterface $item: A field item implementing FieldItemInterface.
string $source: The source property that contains the text we want to extract. This property may be part of the item metadata or a property on a referenced entity.
Return value
string Safe text for output or an empty string if no text can be extracted.
1 call to JuiceboxFieldFormatter::getFieldText()
- JuiceboxFieldFormatter::buildGallery in src/
Plugin/ Field/ FieldFormatter/ JuiceboxFieldFormatter.php  - Utility to build a Juicebox gallery based on field formatter data.
 
File
- src/
Plugin/ Field/ FieldFormatter/ JuiceboxFieldFormatter.php, line 409  
Class
- JuiceboxFieldFormatter
 - Plugin implementation of the 'juicebox' formatter.
 
Namespace
Drupal\juicebox\Plugin\Field\FieldFormatterCode
protected function getFieldText(FieldItemInterface $item, $source) {
  // If the text source is the filename we need to get the data from the
  // item's related file entity.
  if ($source == 'filename' && isset($item->entity)) {
    $entity = $item->entity;
    $entity_properties = $item->entity
      ->toArray();
    if (isset($entity_properties[$source])) {
      // A processed_text render array will utilize text filters on rendering.
      $text_to_build = [
        '#type' => 'processed_text',
        '#text' => $item->entity
          ->get($source)->value,
      ];
      return $this->renderer
        ->render($text_to_build);
    }
  }
  // Otherwise we are dealing with an item value (such as image alt or title
  // text). For some reason alt and title values are not always set as
  // properties on items, so we can't use $item->get(). However, calling the
  // variable directly triggers __get(), which works for BOTH properties and
  // plain values.
  if (isset($item->{$source}) && is_string($item->{$source})) {
    // A processed_text render array will utilize text filters on rendering.
    $text_to_build = [
      '#type' => 'processed_text',
      '#text' => $item->{$source},
    ];
    return $this->renderer
      ->render($text_to_build);
  }
  // @todo: Add support for fieldable file entities and/or media entities.
  return '';
}