SocialEmbedUrlFilter.php in Open Social 8.6
Same filename and directory in other branches
- 8.9 modules/social_features/social_embed/src/Plugin/Filter/SocialEmbedUrlFilter.php
- 8 modules/social_features/social_embed/src/Plugin/Filter/SocialEmbedUrlFilter.php
- 8.2 modules/social_features/social_embed/src/Plugin/Filter/SocialEmbedUrlFilter.php
- 8.3 modules/social_features/social_embed/src/Plugin/Filter/SocialEmbedUrlFilter.php
- 8.4 modules/social_features/social_embed/src/Plugin/Filter/SocialEmbedUrlFilter.php
- 8.5 modules/social_features/social_embed/src/Plugin/Filter/SocialEmbedUrlFilter.php
- 8.7 modules/social_features/social_embed/src/Plugin/Filter/SocialEmbedUrlFilter.php
- 8.8 modules/social_features/social_embed/src/Plugin/Filter/SocialEmbedUrlFilter.php
- 10.3.x modules/social_features/social_embed/src/Plugin/Filter/SocialEmbedUrlFilter.php
- 10.0.x modules/social_features/social_embed/src/Plugin/Filter/SocialEmbedUrlFilter.php
- 10.1.x modules/social_features/social_embed/src/Plugin/Filter/SocialEmbedUrlFilter.php
- 10.2.x modules/social_features/social_embed/src/Plugin/Filter/SocialEmbedUrlFilter.php
Namespace
Drupal\social_embed\Plugin\FilterFile
modules/social_features/social_embed/src/Plugin/Filter/SocialEmbedUrlFilter.phpView source
<?php
namespace Drupal\social_embed\Plugin\Filter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\filter\FilterProcessResult;
use Drupal\url_embed\Plugin\Filter\ConvertUrlToEmbedFilter;
/**
* Provides a filter to display embedded entities based on data attributes.
*
* @Filter(
* id = "social_embed_convert_url",
* title = @Translation("Convert SUPPORTED URLs to URL embeds"),
* description = @Translation("Convert only URLs that are supported to URL embeds."),
* type = Drupal\filter\Plugin\FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
* settings = {
* "url_prefix" = "",
* },
* )
*/
class SocialEmbedUrlFilter extends ConvertUrlToEmbedFilter {
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
return [];
}
/**
* {@inheritdoc}
*/
public function process($text, $langcode) {
// Check for whitelisted URL.
if ($this
->whiteList($text)) {
return parent::process($text, $langcode);
}
// Not whitelisted is return the string as is.
return new FilterProcessResult($text);
}
/**
* Checks if item is on the whitelist.
*
* @param string $text
* The item to check for.
*
* @return bool
* Return if the item is on the whitelist or not.
*/
public function whiteList($text) {
// Fetch allowed patterns.
$patterns = $this
->getPatterns();
// Check if the URL provided is from a whitelisted site.
foreach ($patterns as $pattern) {
// Testing pattern.
$testing_pattern = '/' . $pattern . '/';
// Check if it matches.
if (preg_match($testing_pattern, $text)) {
return TRUE;
}
}
return FALSE;
}
/**
* A list of whitelisted patterns.
*
* @return array
* The list of patterns.
*/
private function getPatterns() {
return [
'facebook.com\\/(.*)\\/videos\\/(.*)',
'facebook.com\\/(.*)\\/photos\\/(.*)',
'facebook.com\\/(.*)\\/posts\\/(.*)',
'flickr.com\\/photos\\/(.*)',
'flic.kr\\/p\\/(.*)',
'instagram.com\\/p\\/(.*)',
'open.spotify.com\\/track\\/(.*)',
'twitter.com\\/(.*)\\/status\\/(.*)',
'vimeo.com\\/\\d{7,9}',
'youtube.com\\/watch[?]v=(.*)',
'youtu.be\\/(.*)',
'ted.com\\/talks\\/(.*)',
];
}
}
Classes
Name | Description |
---|---|
SocialEmbedUrlFilter | Provides a filter to display embedded entities based on data attributes. |