View source
<?php
namespace Drupal\insert\Utility;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\FileInterface;
use Drupal\image\Entity\ImageStyle;
class InsertUtility {
public static function isSourceWidget($pluginId, $insertTypes = NULL) {
return in_array($pluginId, static::getSourceWidgets($insertTypes));
}
protected static function getSourceWidgets($insertTypes = NULL) {
if (is_string($insertTypes)) {
$insertTypes = [
$insertTypes,
];
}
$sources = \Drupal::moduleHandler()
->invokeAll('insert_widgets');
$widgets = [];
foreach ($sources as $insertType => $widgetIds) {
if (count($widgetIds) > 0 && ($insertTypes === NULL || in_array($insertType, $insertTypes))) {
$widgets = array_merge($widgets, $widgetIds);
}
}
return $widgets;
}
public static function aggregateStyles($insertType) {
$styles = \Drupal::moduleHandler()
->invokeAll('insert_styles', [
$insertType,
]);
uasort($styles, function ($a, $b) {
$weightA = !$a instanceof ImageStyle && isset($a['weight']) ? $a['weight'] : 0;
$weightB = !$b instanceof ImageStyle && isset($b['weight']) ? $b['weight'] : 0;
if ($weightA === 0 && $weightB === 0) {
$labelA = $a instanceof ImageStyle ? $a
->label() : $a['label'];
$labelB = $b instanceof ImageStyle ? $b
->label() : $b['label'];
return strcasecmp($labelA, $labelB);
}
return $weightA < $weightB ? -1 : 1;
});
return $styles;
}
public static function stylesListToOptions(array $stylesList) {
foreach ($stylesList as $styleName => $style) {
$stylesList[$styleName] = is_array($style) ? $style['label'] : $style
->label();
}
return $stylesList;
}
public static function validateList(array $element, FormStateInterface &$form_state) {
if (array_key_exists('#options', $element) && array_values($element['#value']) == array_keys($element['#options'])) {
$form_state
->setValue('<all>', '<all>');
}
}
public static function isImage($file) {
$image = \Drupal::service('image.factory')
->get($file
->getFileUri());
return $image
->isValid();
}
public static function buildDerivativeUrl(FileInterface $file, $styleName, $absolute = FALSE) {
$style = ImageStyle::load($styleName);
if ($style !== NULL) {
$url = $style
->buildUrl($file
->getFileUri());
if (!$absolute) {
$parsedUrl = parse_url($url);
$url = $parsedUrl['path'];
if (!empty($parsedUrl['query'])) {
$url .= '?' . $parsedUrl['query'];
}
}
return $url;
}
return NULL;
}
public static function addEditorExtraAllowedContent(array &$settings, array $extraAllowedContent) {
$config = \Drupal::config('insert.config');
$text_formats = $config
->get('text_formats');
foreach (array_keys($settings['editor']['formats']) as $text_format_id) {
if (in_array($text_format_id, $text_formats)) {
static::combineEditorExtraAllowedContent($settings['editor']['formats'][$text_format_id]['editorSettings']['extraAllowedContent'], $extraAllowedContent);
}
}
}
protected static function combineEditorExtraAllowedContent(&$extraAllowedContent, array $additionalAllowedContent) {
$additionalAllowedContent = join('; ', $additionalAllowedContent);
if ($extraAllowedContent === NULL) {
$extraAllowedContent = $additionalAllowedContent;
return;
}
$extraAllowedContent .= '; ' . $additionalAllowedContent;
}
public static function addAllowedHtml($value, array $tags, array $attributes) {
$html = str_replace('>', ' />', $value);
$star_protector = '__zqh6vxfbk3cg__';
$html = str_replace('*', $star_protector, $html);
$body_child_nodes = Html::load($html)
->getElementsByTagName('body')
->item(0)->childNodes;
foreach ($body_child_nodes as $node) {
if ($node->nodeType !== XML_ELEMENT_NODE) {
continue;
}
$tag = $node->tagName;
if (array_key_exists($tag, $tags)) {
$tags[$tag] = TRUE;
}
else {
continue;
}
if ($node
->hasAttributes()) {
foreach ($node->attributes as $name => $attribute) {
$name = str_replace($star_protector, '*', $name);
$allowed_attribute_values = preg_split('/\\s+/', str_replace($star_protector, '*', $attribute->value), -1, PREG_SPLIT_NO_EMPTY);
$allowed_attribute_values = array_filter($allowed_attribute_values, function ($value) {
return $value !== '*';
});
if (array_key_exists($name, $attributes[$tag])) {
$attributes[$tag][$name] = empty($allowed_attribute_values);
}
}
}
}
foreach ($tags as $tag => $found_tag) {
if (!$found_tag) {
$value .= ' <' . $tag . '>';
}
foreach ($attributes[$tag] as $name => $found_attribute) {
if ($found_attribute === TRUE) {
continue;
}
elseif ($found_attribute === NULL) {
$value = preg_replace('/<' . $tag . '/', '<' . $tag . ' ' . $name, $value);
}
else {
$value = preg_replace('/(<' . $tag . '[^>]+' . $name . ')(=("|\')[^"\']+("|\'))/', '$1', $value);
}
}
}
return $value;
}
}