You are here

public function App::tokenToMarkup in Convert Media Tags to Markup 8

Same name and namespace in other branches
  1. 2.x src/ConvertMediaTagsToMarkup/App.php \Drupal\convert_media_tags_to_markup\ConvertMediaTagsToMarkup\App::tokenToMarkup()

Replace callback to convert a media file tag into HTML markup.

This code is adapted from http://cgit.drupalcode.org/media/tree/modules/media_wysiwyg/includes/med....

Parameters

string $match: Takes a match of tag code.

bool $wysiwyg: Set to TRUE if called from within the WYSIWYG text area editor.

Return value

string The HTML markup representation of the tag, or an empty string on failure.

1 call to App::tokenToMarkup()
App::filterText in src/ConvertMediaTagsToMarkup/App.php
Filter text.

File

src/ConvertMediaTagsToMarkup/App.php, line 60

Class

App
Represents the Closest Zip Code API.

Namespace

Drupal\convert_media_tags_to_markup\ConvertMediaTagsToMarkup

Code

public function tokenToMarkup($match, $wysiwyg = FALSE) {
  try {
    $match = str_replace("[[", "", $match);
    $match = str_replace("]]", "", $match);
    $tag = $match[0];
    if (!is_string($tag)) {
      throw new \Exception('Unable to find matching tag');
    }
    $tag_info = $this
      ->drupalJsonDecode($tag);
    if (!isset($tag_info['fid'])) {
      throw new \Exception('No file Id');
    }
    $file = $this
      ->fileLoad($tag_info['fid']);
    $uri = $file
      ->getFileUri();
    $filepath = file_url_transform_relative(file_create_url($uri));
    $alt = empty($tag_info['attributes']['alt']) ? '' : $tag_info['attributes']['alt'];
    $title = $alt;
    $height = empty($tag_info['attributes']['height']) ? '' : 'height="' . $tag_info['attributes']['height'] . '"';
    $width = empty($tag_info['attributes']['width']) ? '' : 'width="' . $tag_info['attributes']['width'] . '"';
    $class = empty($tag_info['attributes']['class']) ? '' : $tag_info['attributes']['class'];
    $style = empty($tag_info['attributes']['style']) ? '' : $tag_info['attributes']['style'];
    $output = '
      <div class="media media-element-container media-default">
        <div id="file-' . $tag_info['fid'] . '" class="file file-image">
          <div class="content">
            <img style="' . $style . '" alt="' . $alt . '" title="' . $title . '" class="' . $class . '" src="' . $filepath . '" ' . $height . ' ' . $width . '>
          </div>
        </div>
      </div>';
    return $output;
  } catch (\Throwable $t) {
    $this
      ->watchdogThrowable($t);
    return '';
  }
}