diff --git a/src/Annotation/MetatagTag.php b/src/Annotation/MetatagTag.php index ac50b8f..6ae74b2 100644 --- a/src/Annotation/MetatagTag.php +++ b/src/Annotation/MetatagTag.php @@ -62,4 +62,18 @@ class MetatagTag extends Plugin { */ public $weight; + /** + * True if an image URL needs to be parsed out. + * + * @var boolean + */ + public $image; + + /** + * True if more than one is allowed. + * + * @var boolean + */ + public $multiple; + } diff --git a/src/Plugin/metatag/Tag/AbstractTag.php b/src/Plugin/metatag/Tag/AbstractTag.php index ee92cc8..7e0847e 100644 --- a/src/Plugin/metatag/Tag/AbstractTag.php +++ b/src/Plugin/metatag/Tag/AbstractTag.php @@ -19,7 +19,9 @@ use Drupal\metatag\Annotation\MetatagTag; * description = @Translation("A brief and concise summary of the page's content, preferably 150 characters or less. The description meta tag may be used by search engines to display a snippet about the page in search results."), * name = "abstract", * group = "basic", - * weight = 3 + * weight = 3, + * image = FALSE, + * multiple = FALSE * ) */ class AbstractTag extends MetaNameBase { diff --git a/src/Plugin/metatag/Tag/CanonicalUrl.php b/src/Plugin/metatag/Tag/CanonicalUrl.php index d67e829..95f2045 100644 --- a/src/Plugin/metatag/Tag/CanonicalUrl.php +++ b/src/Plugin/metatag/Tag/CanonicalUrl.php @@ -19,7 +19,9 @@ use Drupal\metatag\Annotation\MetatagTag; * description = @Translation("Preferred page location or URL to help eliminate duplicate content for search engines."), * name = "canonical", * group = "advanced", - * weight = 1 + * weight = 1, + * image = FALSE, + * multiple = FALSE * ) */ class CanonicalUrl extends LinkRelBase { diff --git a/src/Plugin/metatag/Tag/ContentLanguage.php b/src/Plugin/metatag/Tag/ContentLanguage.php index 2fa0010..5b72f88 100644 --- a/src/Plugin/metatag/Tag/ContentLanguage.php +++ b/src/Plugin/metatag/Tag/ContentLanguage.php @@ -19,7 +19,9 @@ use Drupal\metatag\Annotation\MetatagTag; * description = @Translation("A deprecated meta tag for defining this page's two-letter language code(s)."), * name = "content-language", * group = "advanced", - * weight = 1 + * weight = 1, + * image = FALSE, + * multiple = FALSE * ) */ class ContentLanguage extends MetaHttpEquivBase { diff --git a/src/Plugin/metatag/Tag/Description.php b/src/Plugin/metatag/Tag/Description.php index 9cc5f59..8198ba0 100644 --- a/src/Plugin/metatag/Tag/Description.php +++ b/src/Plugin/metatag/Tag/Description.php @@ -19,7 +19,9 @@ use Drupal\metatag\Annotation\MetatagTag; * description = @Translation("A brief and concise summary of the page's content, preferably 150 characters or less. The description meta tag may be used by search engines to display a snippet about the page in search results."), * name = "description", * group = "basic", - * weight = 2 + * weight = 2, + * image = FALSE, + * multiple = FALSE * ) */ class Description extends MetaNameBase { diff --git a/src/Plugin/metatag/Tag/FbAdmins.php b/src/Plugin/metatag/Tag/FbAdmins.php index e6630bc..3ab6d94 100644 --- a/src/Plugin/metatag/Tag/FbAdmins.php +++ b/src/Plugin/metatag/Tag/FbAdmins.php @@ -19,7 +19,9 @@ use Drupal\metatag\Annotation\MetatagTag; * description = @Translation("A comma-separated list of Facebook user IDs of people who are considered administrators or moderators of this page."), * name = "fb:admins", * group = "facebook", - * weight = 1 + * weight = 1, + * image = FALSE, + * multiple = FALSE * ) */ class FbAdmins extends MetaPropertyBase { diff --git a/src/Plugin/metatag/Tag/FbAppId.php b/src/Plugin/metatag/Tag/FbAppId.php index 8a10582..a912add 100644 --- a/src/Plugin/metatag/Tag/FbAppId.php +++ b/src/Plugin/metatag/Tag/FbAppId.php @@ -19,7 +19,9 @@ use Drupal\metatag\Annotation\MetatagTag; * description = @Translation("A comma-separated list of Facebook Platform Application IDs applicable for this site."), * name = "fb:app_id", * group = "facebook", - * weight = 2 + * weight = 2, + * image = FALSE, + * multiple = FALSE * ) */ class FbAppId extends MetaPropertyBase { diff --git a/src/Plugin/metatag/Tag/Generator.php b/src/Plugin/metatag/Tag/Generator.php index eeb0ec6..1b87c45 100644 --- a/src/Plugin/metatag/Tag/Generator.php +++ b/src/Plugin/metatag/Tag/Generator.php @@ -19,7 +19,9 @@ use Drupal\metatag\Annotation\MetatagTag; * description = @Translation("Describes the name and version number of the software or publishing tool used to create the page."), * name = "generator", * group = "advanced", - * weight = 4 + * weight = 4, + * image = FALSE, + * multiple = FALSE * ) */ class Generator extends LinkRelBase { diff --git a/src/Plugin/metatag/Tag/ImageSrc.php b/src/Plugin/metatag/Tag/ImageSrc.php index fa53fb3..9735963 100644 --- a/src/Plugin/metatag/Tag/ImageSrc.php +++ b/src/Plugin/metatag/Tag/ImageSrc.php @@ -19,7 +19,9 @@ use Drupal\metatag\Annotation\MetatagTag; * description = @Translation("An image associated with this page, for use as a thumbnail in social networks and other services."), * name = "image_src", * group = "advanced", - * weight = 4 + * weight = 4, + * image = TRUE, + * multiple = FALSE * ) */ class ImageSrc extends MetaNameBase { diff --git a/src/Plugin/metatag/Tag/Keywords.php b/src/Plugin/metatag/Tag/Keywords.php index d81a746..197dfb1 100644 --- a/src/Plugin/metatag/Tag/Keywords.php +++ b/src/Plugin/metatag/Tag/Keywords.php @@ -19,7 +19,9 @@ use Drupal\metatag\Annotation\MetatagTag; * description = @Translation("A comma-separated list of keywords about the page. This meta tag is not supported by most search engines anymore."), * name = "keywords", * group = "basic", - * weight = 4 + * weight = 4, + * image = FALSE, + * multiple = FALSE * ) */ class Keywords extends MetaNameBase { diff --git a/src/Plugin/metatag/Tag/MetaNameBase.php b/src/Plugin/metatag/Tag/MetaNameBase.php index 7b90e6c..c1950d8 100644 --- a/src/Plugin/metatag/Tag/MetaNameBase.php +++ b/src/Plugin/metatag/Tag/MetaNameBase.php @@ -54,6 +54,20 @@ abstract class MetaNameBase extends PluginBase { protected $group; /** + * True if an image URL needs to be parsed out. + * + * @var boolean + */ + protected $image; + + /** + * True if more than one is allowed. + * + * @var boolean + */ + protected $multiple; + + /** * The value of the metatag in this instance. * * @var mixed @@ -74,6 +88,8 @@ abstract class MetaNameBase extends PluginBase { $this->description = $plugin_definition['description']; $this->group = $plugin_definition['group']; $this->weight = $plugin_definition['weight']; + $this->image = $plugin_definition['image']; + $this->multiple = $plugin_definition['multiple']; } public function id() { @@ -94,6 +110,12 @@ abstract class MetaNameBase extends PluginBase { public function weight() { return $this->weight; } + public function image() { + return $this->image; + } + public function multiple() { + return $this->multiple; + } /** * @return bool @@ -134,11 +156,14 @@ abstract class MetaNameBase extends PluginBase { $element = ''; } else { + // Parse out the image URL, if needed. + $value = $this->parseImageURL(); + $element = array( '#tag' => 'meta', '#attributes' => array( 'name' => $this->name, - 'content' => $this->value(), + 'content' => $value, ) ); } @@ -157,5 +182,31 @@ abstract class MetaNameBase extends PluginBase { public static function validateTag(array &$element, FormStateInterface $form_state) { //@TODO: If there is some common validation, put it here. Otherwise, make it abstract? } + + protected function parseImageURL() { + $value = $this->value(); + + // If this contains embedded image tags, extract the image URLs. + if ($this->image()) { + if (strip_tags($value) != $value) { + if ($this->multiple()) { + $values = explode(',', $value); + } + else { + $values = array($value); + } + foreach ($values as $key => $val) { + $matches = array(); + preg_match('/src="([^"]*)"/', $val, $matches); + if (!empty($matches[1])) { + $values[$key] = $matches[1]; + } + } + $value = implode(',', $values); + } + } + + return $value; + } } diff --git a/src/Plugin/metatag/Tag/MetaPropertyBase.php b/src/Plugin/metatag/Tag/MetaPropertyBase.php index 00d4603..37938c4 100644 --- a/src/Plugin/metatag/Tag/MetaPropertyBase.php +++ b/src/Plugin/metatag/Tag/MetaPropertyBase.php @@ -24,11 +24,14 @@ abstract class MetaPropertyBase extends MetaNameBase { $element = ''; } else { + // Parse out the image URL, if needed. + $value = $this->parseImageURL(); + $element = array( '#tag' => 'meta', '#attributes' => array( 'property' => $this->name, - 'content' => $this->value(), + 'content' => $value, ) ); } diff --git a/src/Plugin/metatag/Tag/NewsKeywords.php b/src/Plugin/metatag/Tag/NewsKeywords.php index eba9d5c..343f84e 100644 --- a/src/Plugin/metatag/Tag/NewsKeywords.php +++ b/src/Plugin/metatag/Tag/NewsKeywords.php @@ -19,7 +19,9 @@ use Drupal\metatag\Annotation\MetatagTag; * description = @Translation("A comma-separated list of keywords about the page. This meta tag is used as an indicator in Google News.", google_news="http://support.google.com/news/publisher/bin/answer.py?hl=en&answer=68297"), * name = "news_keywords", * group = "advanced", - * weight = 2 + * weight = 2, + * image = FALSE, + * multiple = FALSE * ) */ class NewsKeywords extends MetaNameBase { diff --git a/src/Plugin/metatag/Tag/OgDescription.php b/src/Plugin/metatag/Tag/OgDescription.php index 9e65923..825aa44 100644 --- a/src/Plugin/metatag/Tag/OgDescription.php +++ b/src/Plugin/metatag/Tag/OgDescription.php @@ -19,7 +19,9 @@ use Drupal\metatag\Annotation\MetatagTag; * description = @Translation("A one to two sentence description of the content."), * name = "og:description", * group = "open_graph", - * weight = 6 + * weight = 6, + * image = FALSE, + * multiple = FALSE * ) */ class OgDescription extends MetaPropertyBase { diff --git a/src/Plugin/metatag/Tag/OgImage.php b/src/Plugin/metatag/Tag/OgImage.php index 56009eb..bea66f5 100644 --- a/src/Plugin/metatag/Tag/OgImage.php +++ b/src/Plugin/metatag/Tag/OgImage.php @@ -19,7 +19,9 @@ use Drupal\metatag\Annotation\MetatagTag; * description = @Translation("The URL of an image which should represent the content. For best results use an image that is at least 1200 x 630 pixels in size, but at least 600 x 316 pixels is a recommended minimum. Supports PNG, JPEG and GIF formats. Should not be used if og:image:url is used."), * name = "og:image", * group = "open_graph", - * weight = 9 + * weight = 9, + * image = TRUE, + * multiple = TRUE * ) */ class OgImage extends MetaPropertyBase { diff --git a/src/Plugin/metatag/Tag/OgImageSecureUrl.php b/src/Plugin/metatag/Tag/OgImageSecureUrl.php index 6ddfad4..f9ad358 100644 --- a/src/Plugin/metatag/Tag/OgImageSecureUrl.php +++ b/src/Plugin/metatag/Tag/OgImageSecureUrl.php @@ -19,7 +19,9 @@ use Drupal\metatag\Annotation\MetatagTag; * description = @Translation("The secure URL (HTTPS) of an image which should represent the content. The image must be at least 50px by 50px and have a maximum aspect ratio of 3:1. Supports PNG, JPEG and GIF formats. All 'http://' URLs will automatically be converted to 'https://'."), * name = "og:image:secure_url", * group = "open_graph", - * weight = 11 + * weight = 11, + * image = FALSE, + * multiple = FALSE * ) */ class OgImageSecureUrl extends MetaPropertyBase { diff --git a/src/Plugin/metatag/Tag/OgImageUrl.php b/src/Plugin/metatag/Tag/OgImageUrl.php index 53a5d95..4646d53 100644 --- a/src/Plugin/metatag/Tag/OgImageUrl.php +++ b/src/Plugin/metatag/Tag/OgImageUrl.php @@ -19,7 +19,9 @@ use Drupal\metatag\Annotation\MetatagTag; * description = @Translation("A alternative version of og:image and has exactly the same requirements; only one needs to be used."), * name = "og:image:url", * group = "open_graph", - * weight = 10 + * weight = 10, + * image = TRUE, + * multiple = TRUE * ) */ class OgImageUrl extends MetaPropertyBase { diff --git a/src/Plugin/metatag/Tag/OgSiteName.php b/src/Plugin/metatag/Tag/OgSiteName.php index 2e2b580..b8a1741 100644 --- a/src/Plugin/metatag/Tag/OgSiteName.php +++ b/src/Plugin/metatag/Tag/OgSiteName.php @@ -19,7 +19,9 @@ use Drupal\metatag\Annotation\MetatagTag; * description = @Translation("A human-readable name for the site, e.g., IMDb."), * name = "og:site_name", * group = "open_graph", - * weight = 1 + * weight = 1, + * image = FALSE, + * multiple = FALSE * ) */ class OgSiteName extends MetaPropertyBase { diff --git a/src/Plugin/metatag/Tag/OgTitle.php b/src/Plugin/metatag/Tag/OgTitle.php index e43f3cb..cae6e66 100644 --- a/src/Plugin/metatag/Tag/OgTitle.php +++ b/src/Plugin/metatag/Tag/OgTitle.php @@ -19,7 +19,9 @@ use Drupal\metatag\Annotation\MetatagTag; * description = @Translation("The title of the content, e.g., The Rock."), * name = "og:title", * group = "open_graph", - * weight = 4 + * weight = 4, + * image = FALSE, + * multiple = FALSE * ) */ class OgTitle extends MetaPropertyBase { diff --git a/src/Plugin/metatag/Tag/OgType.php b/src/Plugin/metatag/Tag/OgType.php index bef4e90..e2027aa 100644 --- a/src/Plugin/metatag/Tag/OgType.php +++ b/src/Plugin/metatag/Tag/OgType.php @@ -19,7 +19,9 @@ use Drupal\metatag\Annotation\MetatagTag; * description = @Translation("The type of the content, e.g., movie."), * name = "og:type", * group = "open_graph", - * weight = 2 + * weight = 2, + * image = FALSE, + * multiple = FALSE * ) */ class OgType extends MetaPropertyBase { diff --git a/src/Plugin/metatag/Tag/OgUrl.php b/src/Plugin/metatag/Tag/OgUrl.php index 68399a5..27747d6 100644 --- a/src/Plugin/metatag/Tag/OgUrl.php +++ b/src/Plugin/metatag/Tag/OgUrl.php @@ -19,7 +19,9 @@ use Drupal\metatag\Annotation\MetatagTag; * description = @Translation("Preferred page location or URL to help eliminate duplicate content for search engines, e.g., http://www.imdb.com/title/tt0117500/."), * name = "og:url", * group = "open_graph", - * weight = 3 + * weight = 3, + * image = FALSE, + * multiple = FALSE * ) */ class OgUrl extends MetaPropertyBase { diff --git a/src/Plugin/metatag/Tag/OriginalSource.php b/src/Plugin/metatag/Tag/OriginalSource.php index 3a83137..98c298f 100644 --- a/src/Plugin/metatag/Tag/OriginalSource.php +++ b/src/Plugin/metatag/Tag/OriginalSource.php @@ -19,7 +19,9 @@ use Drupal\metatag\Annotation\MetatagTag; * description = @Translation("Used to indicate the URL that broke the story, and can link to either an internal URL or an external source. If the full URL is not known it is acceptable to use a partial URL or just the domain name."), * name = "original-source", * group = "advanced", - * weight = 4 + * weight = 4, + * image = FALSE, + * multiple = FALSE * ) */ class OriginalSource extends MetaNameBase { diff --git a/src/Plugin/metatag/Tag/Rights.php b/src/Plugin/metatag/Tag/Rights.php index a183ce3..a25421a 100644 --- a/src/Plugin/metatag/Tag/Rights.php +++ b/src/Plugin/metatag/Tag/Rights.php @@ -19,7 +19,9 @@ use Drupal\metatag\Annotation\MetatagTag; * description = @Translation("Details about intellectual property, such as copyright or trademarks; does not automatically protect the site's content or intellectual property."), * name = "rights", * group = "advanced", - * weight = 5 + * weight = 5, + * image = FALSE, + * multiple = FALSE * ) */ class Rights extends MetaNameBase { diff --git a/src/Plugin/metatag/Tag/Robots.php b/src/Plugin/metatag/Tag/Robots.php index 2185cc7..4885805 100644 --- a/src/Plugin/metatag/Tag/Robots.php +++ b/src/Plugin/metatag/Tag/Robots.php @@ -19,7 +19,9 @@ use Drupal\metatag\Annotation\MetatagTag; * description = @Translation("Provides search engines with specific directions for what to do when this page is indexed."), * name = "robots", * group = "advanced", - * weight = 1 + * weight = 1, + * image = FALSE, + * multiple = FALSE * ) */ class Robots extends MetaNameBase { diff --git a/src/Plugin/metatag/Tag/Standout.php b/src/Plugin/metatag/Tag/Standout.php index 9c0002b..1983a1c 100644 --- a/src/Plugin/metatag/Tag/Standout.php +++ b/src/Plugin/metatag/Tag/Standout.php @@ -19,7 +19,9 @@ use Drupal\metatag\Annotation\MetatagTag; * description = @Translation("Highlight standout journalism on the web, especially for breaking news; used as an indicator in Google News. Warning: Don't abuse it, to be used a maximum of 7 times per calendar week!", google_news="http://support.google.com/news/publisher/bin/answer.py?hl=en&answer=68297"), * name = "standout", * group = "advanced", - * weight = 3 + * weight = 3, + * image = FALSE, + * multiple = FALSE * ) */ class Standout extends MetaNameBase { diff --git a/src/Plugin/metatag/Tag/Title.php b/src/Plugin/metatag/Tag/Title.php index 7efc1db..a4ca989 100644 --- a/src/Plugin/metatag/Tag/Title.php +++ b/src/Plugin/metatag/Tag/Title.php @@ -19,7 +19,9 @@ use Drupal\metatag\Annotation\MetatagTag; * description = @Translation("The text to display in the title bar of a visitor's web browser when they view this page. This meta tag may also be used as the title of the page when a visitor bookmarks or favorites this page. It is common to append '[site:name]' to the end of this, so the site's name is automatically added."), * name = "title", * group = "basic", - * weight = -1 + * weight = -1, + * image = FALSE, + * multiple = FALSE * ) */ class Title extends MetaNameBase {