App.php in Convert Media Tags to Markup 8
File
src/ConvertMediaTagsToMarkup/App.php
View source
<?php
namespace Drupal\convert_media_tags_to_markup\ConvertMediaTagsToMarkup;
use Drupal\convert_media_tags_to_markup\traits\Singleton;
use Drupal\convert_media_tags_to_markup\traits\CommonUtilities;
class App {
const MEDIA_WYSIWYG_TOKEN_REGEX = '/\\[\\[\\{.*?"type":"media".+?\\}\\]\\]/s';
use Singleton;
use CommonUtilities;
public function filterText(string $text) : string {
$rendered_text = $text;
$count = 1;
preg_match_all(self::MEDIA_WYSIWYG_TOKEN_REGEX, $text, $matches);
if (!empty($matches[0])) {
foreach ($matches[0] as $match) {
$replacement = $this
->tokenToMarkup([
$match,
], FALSE);
$rendered_text = str_replace($match, $replacement, $rendered_text, $count);
}
}
return $rendered_text;
}
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 '';
}
}
}
Classes
Name |
Description |
App |
Represents the Closest Zip Code API. |