You are here

class App in Convert Media Tags to Markup 2.x

Same name and namespace in other branches
  1. 8 src/ConvertMediaTagsToMarkup/App.php \Drupal\convert_media_tags_to_markup\ConvertMediaTagsToMarkup\App

Represents the Closest Zip Code API.

Hierarchy

Expanded class hierarchy of App

1 file declares its use of App
ConvertLegacyMediaTagsToMarkup.php in src/Plugin/Filter/ConvertLegacyMediaTagsToMarkup.php

File

src/ConvertMediaTagsToMarkup/App.php, line 11

Namespace

Drupal\convert_media_tags_to_markup\ConvertMediaTagsToMarkup
View source
class App {
  const MEDIA_WYSIWYG_TOKEN_REGEX = '/\\[\\[\\{.*?"type":"media".+?\\}\\]\\]/s';
  use Singleton;
  use CommonUtilities;

  /**
   * Filter text.
   *
   * The is the heart of this module: change media tags code to an actual
   * image tag. This can be done by the filter plugin or by the DbReplacer
   * object.
   *
   * @param string $text
   *   The text to filter.
   *
   * @return string
   *   The filtered text.
   *
   * @throws Exception
   */
  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;
  }

  /**
   * 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/media_wysiwyg.filter.inc?h=7.x-3.x.
   *
   * @param string $match
   *   Takes a match of tag code.
   * @param bool $wysiwyg
   *   Set to TRUE if called from within the WYSIWYG text area editor.
   *
   * @return string
   *   The HTML markup representation of the tag, or an empty string on failure.
   */
  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 '';
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
App::filterText public function Filter text.
App::MEDIA_WYSIWYG_TOKEN_REGEX constant
App::tokenToMarkup public function Replace callback to convert a media file tag into HTML markup.
CommonUtilities::decodeEntities public function Mockable wrapper around decodeEntities().
CommonUtilities::drupalJsonDecode public function Mockable wrapper around Json::decode().
CommonUtilities::fileLoad public function Mockable wrapper around File::load(). Also throws an exception.
CommonUtilities::getAllEntities protected function Get all entities of a specific type and bundle.
CommonUtilities::watchdog public function Log a string to the watchdog.
CommonUtilities::watchdogError public function Log an error to the watchdog.
CommonUtilities::watchdogThrowable public function Log a \Throwable to the watchdog.
Singleton::$instance private static property Interal instance variable used with the instance() method.
Singleton::instance public static function Implements the Singleton design pattern.