You are here

class InlineResponsiveImage in Inline responsive images 7

Same name and namespace in other branches
  1. 8 src/Plugin/Filter/InlineResponsiveImage.php \Drupal\inline_responsive_image\Plugin\Filter\InlineResponsiveImage

Plugin annotation


@Filter(
  id = "inline_responsive_image",
  title = @Translation("Inline responsive images"),
  description = @Translation("Filter for inline responsive images"),
  type = Drupal\filter\Plugin\FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
)

Hierarchy

  • class \Drupal\inline_responsive_image\Plugin\Filter\InlineResponsiveImage extends \Drupal\filter\Plugin\FilterBase

Expanded class hierarchy of InlineResponsiveImage

File

src/Plugin/Filter/InlineResponsiveImage.php, line 19

Namespace

Drupal\inline_responsive_image\Plugin\Filter
View source
class InlineResponsiveImage extends FilterBase {
  public function process($text, $langcode) {
    $result = new FilterProcessResult($text);
    if (stristr($text, 'data-style') !== FALSE) {
      $dom = Html::load($text);
      $xpath = new \DOMXPath($dom);
      foreach ($xpath
        ->query('//*[@data-style]') as $node) {
        $style = $node
          ->getAttribute('data-style');
        $caption = $node
          ->getAttribute('data-caption');
        $align = $node
          ->getAttribute('data-align');
        $entityUuid = $node
          ->getAttribute('data-entity-uuid');
        $responsive = ResponsiveImageStyle::load($style) ? true : false;
        $file = \Drupal::entityManager()
          ->loadEntityByUuid('file', $entityUuid);
        if ($file) {
          if ($responsive) {
            $responsiveImage = array(
              '#theme' => 'inline_responsive_image',
              '#image' => array(
                '#type' => 'responsive_image',
                '#responsive_image_style_id' => $style,
                '#uri' => $file
                  ->getFileUri(),
              ),
              '#attributes' => array(
                'class' => array(),
              ),
              '#caption' => !empty($caption) ? $caption : '',
            );
          }
          else {
            $image_style = ImageStyle::load($style);
            $responsiveImage = array(
              '#theme' => 'inline_responsive_image',
              '#image' => array(
                '#type' => 'image_formatter',
                '#style_name' => $style,
                '#url' => $file
                  ->getFileUri(),
              ),
              '#attributes' => array(
                'class' => array(
                  'test' => 'test',
                ),
              ),
              '#caption' => !empty($caption) ? $caption : '',
            );
          }
          $responsiveImage['#attributes']['class'][] = $style;
          if (!empty($align)) {
            $responsiveImage['#attributes']['class'][] = $align;
          }
          $responsiveImageHtml = \Drupal::service('renderer')
            ->render($responsiveImage);
          $fragment = $dom
            ->createDocumentFragment();
          if ($fragment
            ->appendXML($responsiveImageHtml)) {
            $node->parentNode
              ->replaceChild($fragment, $node);
          }
        }
      }
      $result
        ->setProcessedText(Html::serialize($dom));
    }
    return $result;
  }

}

Members