class FilterOEmbed in oEmbed 8
Provides a fallback placeholder filter to use for missing filters.
The filter system uses this filter to replace missing filters (for example, if a filter module has been disabled) that are still part of defined text formats. It returns an empty string.
Plugin annotation
@Filter(
id = "oembed",
title = @Translation("oEmbed filter"),
description = @Translation("Embeds content for oEmbed-enabled web addresses and turns the rest, and e-mail addresses, into clickable links."),
type = Drupal\filter\Plugin\FilterInterface::TYPE_MARKUP_LANGUAGE,
settings = {
"options" = "",
"autoembed" = true
},
weight = -10
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\filter\Plugin\FilterBase implements FilterInterface
- class \Drupal\oembed\Plugin\Filter\FilterOEmbed implements ContainerFactoryPluginInterface
- class \Drupal\filter\Plugin\FilterBase implements FilterInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of FilterOEmbed
File
- src/
Plugin/ Filter/ FilterOEmbed.php, line 37 - Contains \Drupal\filter\Plugin\Filter\FilterOEmbed.
Namespace
Drupal\oembed\Plugin\FilterView source
class FilterOEmbed extends FilterBase implements ContainerFactoryPluginInterface {
const OEMBED_PATTERN_AUTOEMBED = '|^\\s*(https?://[^\\s"]+)\\s*$|im';
const OEMBED_PATTERN_EMBED_SHORTCODE = '/(.?)\\[embed\\b(.*?)\\](.+?)\\[\\/embed\\](.?)/s';
const OEMBED_PATTERN_EMBED_UNWRAP = '/<p>\\s*+(\\[embed\\b.*?\\].+?\\[\\/embed\\])\\s*+<\\/p>/s';
/**
* @var \Bangpound\oEmbed\Consumer
*/
private $consumer;
public function __construct(array $configuration, $plugin_id, $plugin_definition, Consumer $consumer) {
$this->consumer = $consumer;
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* Creates an instance of the plugin.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The container to pull out services used in the plugin.
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
*
* @return static
* Returns an instance of this plugin.
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
/** @var \Bangpound\oEmbed\Consumer $consumer */
$consumer = $container
->get('oembed.consumer');
return new static($configuration, $plugin_id, $plugin_definition, $consumer);
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$form['options'] = array(
'#type' => 'textfield',
'#title' => t('Default oEmbed request options'),
'#default_value' => $this->settings['options'],
'#description' => t('A series of attribute value pairs for the default request options. For example, <em>maxwidth="500"</em>.'),
);
$form['autoembed'] = array(
'#type' => 'checkbox',
'#title' => t('Automatically embed URLs'),
'#default_value' => $this->settings['autoembed'],
'#description' => t('When possible, embed the media content from a URL directly in the input.'),
);
return $form;
}
public function prepare($text, $langcode) {
if ($this->settings['autoembed']) {
$text = preg_replace_callback(self::OEMBED_PATTERN_AUTOEMBED, array(
$this,
'oembed_preg_auto_replace',
), $text);
}
return $text;
}
/**
* Performs the filter processing.
*
* @param string $text
* The text string to be filtered.
* @param string $langcode
* The language code of the text to be filtered.
*
* @return \Drupal\filter\FilterProcessResult
* The filtered text, wrapped in a FilterProcessResult object, and possibly
* with associated assets, cacheability metadata and placeholders.
*
* @see \Drupal\filter\FilterProcessResult
*/
public function process($text, $langcode) {
// Undo auto paragraph around oEmbed shortcodes.
$text = preg_replace(self::OEMBED_PATTERN_EMBED_UNWRAP, '$1', $text);
$text = preg_replace_callback(self::OEMBED_PATTERN_EMBED_SHORTCODE, array(
$this,
'oembed_preg_tag_replace',
), $text);
return new FilterProcessResult($text);
}
public function tips($long = FALSE) {
if ($long) {
return t('Embed content by wrapping a supported URL in [embed] … [/embed]. Set options such as width and height with attributes [embed width="123" height="456"] … [/embed]. Unsupported options will be ignored.');
}
else {
return t('Embed content by wrapping a supported URL in [embed] … [/embed].');
}
}
/**
* PREG replace callback finds [embed] shortcodes, URLs and request options.
* @param $match
* @return string
*/
private function oembed_preg_tag_replace($match) {
// allow [[oembed]] syntax for escaping a tag
if ($match[1] == '[' && $match[4] == ']') {
return substr($match[0], 1, -1);
}
$url = $match[3];
$shortcode_options = !empty($match[2]) ? self::oembed_parse_attr($match[2]) : array();
$options = !empty($this->settings['options']) ? self::oembed_parse_attr($this->settings['options']) : array();
$options = array_merge($options, $shortcode_options);
return $match[1] . $this
->oembed_resolve_link($url, $options) . $match[4];
}
/**
* Retrieve all attributes from the shortcodes tag.
*
* @see shortcode_parse_atts in WordPress 3.1.3.
* @param string $text
* @return array List of attributes and their value.
*/
private static function oembed_parse_attr($text) {
$attributes = array();
$pattern = '/([\\w-]+)\\s*=\\s*"([^"]*)"(?:\\s|$)|([\\w-]+)\\s*=\\s*\'([^\']*)\'(?:\\s|$)|([\\w-]+)\\s*=\\s*([^\\s\'"]+)(?:\\s|$)|"([^"]*)"(?:\\s|$)|(\\S+)(?:\\s|$)/';
$text = preg_replace("/[\\x{00a0}\\x{200b}]+/u", " ", $text);
if (preg_match_all($pattern, $text, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
if (!empty($match[1])) {
$attributes[strtolower($match[1])] = stripcslashes($match[2]);
}
elseif (!empty($match[3])) {
$attributes[strtolower($match[3])] = stripcslashes($match[4]);
}
elseif (!empty($match[5])) {
$attributes[strtolower($match[5])] = stripcslashes($match[6]);
}
elseif (isset($match[7]) && strlen($match[7])) {
$attributes[] = stripcslashes($match[7]);
}
elseif (isset($match[8])) {
$attributes[] = stripcslashes($match[8]);
}
}
// Reject any unclosed HTML elements
foreach ($attributes as &$value) {
if (false !== strpos($value, '<')) {
if (1 !== preg_match('/^[^<]*+(?:<[^>]*+>[^<]*+)*+$/', $value)) {
$value = '';
}
}
}
}
else {
$attributes = ltrim($text);
}
return $attributes;
}
/**
* PREG replace callback finds [embed] shortcodes, URLs and request options.
*
* Override in Drupal system variable `oembed_resolve_link_callback`
*
* @see MediaInternetOEmbedHandler::preSave().
*
* @param string $url
* URL to embed.
* @param array $options
* oEmbed request options.
*
* @return string
* Rendered oEmbed response.
*/
private function oembed_resolve_link($url, $options = array()) {
$url = Html::decodeEntities($url);
$embed = $this->consumer
->get($url, $options);
$return = (string) $embed;
if (empty($return)) {
$return = $url;
}
return new FilterProcessResult($return);
}
/**
* PREG replace callback finds URLs
* @param $match
* @return string
*/
private static function oembed_preg_auto_replace($match) {
return '[embed]' . $match[1] . "[/embed]\n";
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FilterBase:: |
public | property | The name of the provider that owns this filter. | |
FilterBase:: |
public | property | An associative array containing the configured settings of this filter. | |
FilterBase:: |
public | property | A Boolean indicating whether this filter is enabled. | |
FilterBase:: |
public | property | The weight of this filter compared to others in a filter collection. | |
FilterBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
1 |
FilterBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurableInterface:: |
|
FilterBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
FilterBase:: |
public | function |
Returns the administrative description for this filter plugin. Overrides FilterInterface:: |
|
FilterBase:: |
public | function |
Returns HTML allowed by this filter's configuration. Overrides FilterInterface:: |
4 |
FilterBase:: |
public | function |
Returns the administrative label for this filter plugin. Overrides FilterInterface:: |
|
FilterBase:: |
public | function |
Returns the processing type of this filter plugin. Overrides FilterInterface:: |
|
FilterBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
1 |
FilterInterface:: |
constant | HTML tag and attribute restricting filters to prevent XSS attacks. | ||
FilterInterface:: |
constant | Non-HTML markup language filters that generate HTML. | ||
FilterInterface:: |
constant | Irreversible transformation filters. | ||
FilterInterface:: |
constant | Reversible transformation filters. | ||
FilterOEmbed:: |
private | property | ||
FilterOEmbed:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
FilterOEmbed:: |
private static | function | Retrieve all attributes from the shortcodes tag. | |
FilterOEmbed:: |
constant | |||
FilterOEmbed:: |
constant | |||
FilterOEmbed:: |
constant | |||
FilterOEmbed:: |
private static | function | PREG replace callback finds URLs | |
FilterOEmbed:: |
private | function | PREG replace callback finds [embed] shortcodes, URLs and request options. | |
FilterOEmbed:: |
private | function | PREG replace callback finds [embed] shortcodes, URLs and request options. | |
FilterOEmbed:: |
public | function |
Prepares the text for processing. Overrides FilterBase:: |
|
FilterOEmbed:: |
public | function |
Performs the filter processing. Overrides FilterInterface:: |
|
FilterOEmbed:: |
public | function |
Generates a filter's settings form. Overrides FilterBase:: |
|
FilterOEmbed:: |
public | function |
Generates a filter's tip. Overrides FilterBase:: |
|
FilterOEmbed:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides FilterBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |